telegram-rabbit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e71952c2da6a5bd91938db5e139cdf297f82428c
4
+ data.tar.gz: 74667dd47c132aac1c156e0ec65bfd5ced280580
5
+ SHA512:
6
+ metadata.gz: 3e56b62c18161a8e051925106e5a3e62e8d3459c08b30b2f19f4c628917c6e0091ac2c6d7aa34642d00dd05dd1bf0de3393db70fbe0d6ccbed9be135f2d2e265
7
+ data.tar.gz: 4cc990862b62d617f2d812b5d83d21d0fb7b7b20513c7c42b3a8bc73893a8f4ed463acd79f996f33a0ab89a3ab6da802bd320ccde13ae4c6c4aac34d76b9f392
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Telegram::Rabbit
2
+
3
+ ## Work in progress
4
+
5
+ RabbitMQ wrapper for [`telegram-bot-ruby`](https://github.com/atipugin/telegram-bot-ruby). Runs bot client and communicates with other apps via rabbitmq.
6
+
7
+ Use [`telegram-rails`](https://github.com/govorov/telegram-rails) for communication with rails applications.
8
+
9
+ ## Usage
10
+
11
+ Create file with something like this:
12
+
13
+ ```
14
+ require 'telegram/rabbit'
15
+
16
+ options = {
17
+ queue_namespace: :my_super_app,
18
+ bot: {
19
+ #give a name to your bot
20
+ name: :main,
21
+ #specify a token
22
+ token: "your_token_goes_here"
23
+ }
24
+ #everithing from :bunny will be passed to Bunny constructor
25
+ bunny: {
26
+ ...
27
+ }
28
+ }
29
+
30
+ Telegram::Rabbit::ClientAdapter.new(options).start
31
+
32
+ ```
33
+
34
+ And run it.
35
+
36
+ ## TODO
37
+
38
+ * Error handling
39
+ * Graceful shutdown
40
+ * Documentation
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "telegram/rabbit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "telegram/rabbit/version"
2
+ require "telegram/rabbit/client_adapter"
3
+
4
+
5
+ module Telegram
6
+ module Rabbit
7
+ end
8
+ end
@@ -0,0 +1,103 @@
1
+ require 'logger'
2
+ require 'bunny'
3
+ require 'telegram/bot'
4
+
5
+
6
+ module Telegram
7
+ module Rabbit
8
+ class ClientAdapter
9
+
10
+ attr_accessor :client
11
+
12
+
13
+ def initialize opts={}
14
+ @options = default_options.merge(opts)
15
+ @logger = get_option :logger_instance
16
+ @logger.level = get_option :log_level
17
+ end
18
+
19
+
20
+ def start
21
+ logger.debug "Starting ClientAdapter"
22
+ @connection = Bunny.new(get_option :bunny).start
23
+ at_exit { @connection.close }
24
+
25
+ #queues
26
+ send_queue_name = "#{queue_prefix}.messages"
27
+ receive_queue_name = "#{queue_prefix}.commands"
28
+
29
+ @channel = @connection.create_channel
30
+ @send_queue = @channel.queue send_queue_name
31
+ @receive_queue = @channel.queue receive_queue_name
32
+
33
+ logger.debug "Created queues:"
34
+ logger.debug " send: #{send_queue_name}"
35
+ logger.debug " receive: #{receive_queue_name}"
36
+
37
+ @receive_queue.subscribe do |info,metadata,raw_data|
38
+ data = Marshal.load(raw_data)
39
+ command, payload = extract_command(data)
40
+ logger.debug "Received command: #{command}"
41
+ logger.debug payload.inspect
42
+ exec_api_command command, payload
43
+ end
44
+
45
+ logger.debug "Starting bot client"
46
+
47
+ @client = client || Telegram::Bot::Client.new(get_option :bot, :token)
48
+ @client.run do |bot|
49
+ logger.debug "Bot client started"
50
+ bot.listen do |message|
51
+ logger.debug "Received message:"
52
+ logger.debug message.inspect
53
+ send_to_app message
54
+ end
55
+ end
56
+
57
+ end
58
+
59
+
60
+ private
61
+
62
+ def send_to_app message
63
+ payload = Marshal.dump(message)
64
+ @channel.default_exchange.publish payload, routing_key: @send_queue.name
65
+ end
66
+
67
+
68
+ def logger
69
+ @logger
70
+ end
71
+
72
+
73
+ def extract_command payload
74
+ [ payload[:command], payload[:payload] ]
75
+ end
76
+
77
+
78
+ def exec_api_command command, payload
79
+ # WIP error handling - send errors back
80
+ @client.api.send command, payload
81
+ end
82
+
83
+
84
+ def queue_prefix
85
+ "#{get_option :queue_namespace}.#{get_option :bot, :name}"
86
+ end
87
+
88
+
89
+ def get_option *args
90
+ (@options || {}).dig *args
91
+ end
92
+
93
+
94
+ def default_options
95
+ {
96
+ log_level: Logger::DEBUG,
97
+ logger_instance: Logger.new(STDOUT)
98
+ }
99
+ end
100
+
101
+ end
102
+ end # Rabbit
103
+ end # Telegram
@@ -0,0 +1,5 @@
1
+ module Telegram
2
+ module Rabbit
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telegram-rabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stanislav E. Govorov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: telegram-bot-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bunny
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - govorov.st@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - bin/console
78
+ - bin/setup
79
+ - lib/telegram/rabbit.rb
80
+ - lib/telegram/rabbit/client_adapter.rb
81
+ - lib/telegram/rabbit/version.rb
82
+ homepage:
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.6.11
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: RabbitMQ wrapper for telegram-bot-ruby
106
+ test_files: []