telegram-bot-ruby 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0f238cefd205500522307dccb130ef0993bfd8a
4
- data.tar.gz: 115bc4c3bb59e94f67842810ab95ed1f7d21aae5
3
+ metadata.gz: ef037a3e5735ef90b436b1ef4929273ae596cf14
4
+ data.tar.gz: 5f1c00b66ae94a56bcf6b55de44030ea5abf34e9
5
5
  SHA512:
6
- metadata.gz: 7419c4a6c6ca6fec3c8132f7734307ccf10640ffb2d6e58b3e019ad11577b29485c5a02a3b9cf6ed603daae39fd0fbccb573e7fcf5ec70c53800639041c11032
7
- data.tar.gz: 66fdc42963a0e8847b187eaaab4307383cf9b378ae470d2bb59dc8c403b1adbda21a4c7229e668212b8cd9d890d2a07d27265514429808a8bb3a6b8f4e9ffd25
6
+ metadata.gz: 4f6242809b6c31ab7ff030ebb2d76a10165416d996a523a409f737511d33c42de0c597d9a57725b20816dd31f973ba3a5f85c02971c668814ec4ce085fa064c7
7
+ data.tar.gz: cb4aa63e3eb65406959405f914dc09912449273adc9fd6131d06340e705e3d01d548f88247a8346efe3c94812dc745f281793aac165c28d73932e0ddcba9b2c6
data/README.md CHANGED
@@ -85,6 +85,46 @@ bot.listen do |message|
85
85
  end
86
86
  ```
87
87
 
88
+ ## Logging
89
+
90
+ By default, bot doesn't log anything (uses `NullLoger`). You can change this behavior and provide your own logger class. See example below:
91
+
92
+ ```ruby
93
+ Telegram::Bot::Client.run(token, logger: Logger.new($stdout)) do |bot|
94
+ bot.logger.info('Bot has been started')
95
+ bot.listen do |message|
96
+ # ...
97
+ end
98
+ end
99
+ ```
100
+
101
+ ## Botan.io support
102
+
103
+ Gem provides support of [Botan.io](http://botan.io/) analytics out of box. All you need is to obtain a token (follow the instructions from http://botan.io/). To track events you're interested in just call `#track` method. See example below:
104
+
105
+ ```ruby
106
+ require 'telegram/bot'
107
+ require 'telegram/bot/botan' # Botan.io extension isn't loaded by default, so make sure you required it.
108
+
109
+ token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
110
+
111
+ Telegram::Bot::Client.run(token) do |bot|
112
+ bot.enable_botan!('YOUR_BOTAN_TOKEN')
113
+ bot.listen do |message|
114
+ case message.text
115
+ when '/start'
116
+ bot.track('Started', message.from.id, type_of_chat: message.chat.class.name)
117
+ # ...
118
+ end
119
+ end
120
+ end
121
+ ```
122
+
123
+ `#track` method accepts 3 arguments:
124
+ - name of event (required)
125
+ - Telegram's user id (required)
126
+ - hash of additional properties (optional)
127
+
88
128
  ## Contributing
89
129
 
90
130
  1. Fork it
data/lib/telegram/bot.rb CHANGED
@@ -5,5 +5,6 @@ require 'virtus'
5
5
  require 'telegram/bot/types'
6
6
  require 'telegram/bot/exceptions'
7
7
  require 'telegram/bot/api'
8
+ require 'telegram/bot/null_logger'
8
9
  require 'telegram/bot/client'
9
10
  require 'telegram/bot/version'
@@ -0,0 +1,21 @@
1
+ require 'telegram/bot/botan/api'
2
+
3
+ module Telegram
4
+ module Bot
5
+ module Botan
6
+ attr_reader :botan
7
+
8
+ def enable_botan!(token)
9
+ @botan ||= Botan::Api.new(token)
10
+ end
11
+
12
+ def track(*args)
13
+ botan.track(*args) if defined?(botan)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ if defined?(Telegram::Bot::Client)
20
+ Telegram::Bot::Client.send(:include, Telegram::Bot::Botan)
21
+ end
@@ -0,0 +1,23 @@
1
+ module Telegram
2
+ module Bot
3
+ module Botan
4
+ class Api
5
+ include HTTParty
6
+
7
+ attr_reader :token
8
+
9
+ base_uri 'https://api.botan.io'
10
+
11
+ def initialize(token)
12
+ @token = token
13
+ end
14
+
15
+ def track(event, uid, properties = {})
16
+ self.class.post('/track',
17
+ query: { token: token, name: event, uid: uid },
18
+ body: properties.to_json)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,16 +1,18 @@
1
1
  module Telegram
2
2
  module Bot
3
3
  class Client
4
- attr_reader :api, :offset, :timeout
4
+ attr_reader :api, :offset, :timeout, :logger
5
5
 
6
6
  def self.run(*args, &block)
7
7
  new(*args).run(&block)
8
8
  end
9
9
 
10
- def initialize(token, timeout = 20)
10
+ def initialize(token, h = {})
11
+ options = default_options.merge(h)
11
12
  @api = Api.new(token)
12
- @offset = 0
13
- @timeout = timeout
13
+ @offset = options[:offset]
14
+ @timeout = options[:timeout]
15
+ @logger = options[:logger]
14
16
  end
15
17
 
16
18
  def run
@@ -25,12 +27,29 @@ module Telegram
25
27
  response['result'].each do |data|
26
28
  update = Types::Update.new(data)
27
29
  @offset = update.update_id.next
30
+ log_incoming_message(update.message)
28
31
  yield update.message
29
32
  end
30
33
  end
31
34
  rescue Net::ReadTimeout
32
35
  retry
33
36
  end
37
+
38
+ private
39
+
40
+ def default_options
41
+ { offset: 0, timeout: 20, logger: NullLogger.new }
42
+ end
43
+
44
+ def log_incoming_message(message)
45
+ logger.info(
46
+ format(
47
+ 'Incoming message: text="%s" uid=%i',
48
+ message.text,
49
+ message.from.id
50
+ )
51
+ )
52
+ end
34
53
  end
35
54
  end
36
55
  end
@@ -0,0 +1,11 @@
1
+ module Telegram
2
+ module Bot
3
+ class NullLogger < Logger
4
+ def initialize(*)
5
+ end
6
+
7
+ def add(*)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.2.6'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Tipugin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-31 00:00:00.000000000 Z
11
+ date: 2015-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httmultiparty
@@ -126,10 +126,13 @@ files:
126
126
  - examples/bot.rb
127
127
  - lib/telegram/bot.rb
128
128
  - lib/telegram/bot/api.rb
129
+ - lib/telegram/bot/botan.rb
130
+ - lib/telegram/bot/botan/api.rb
129
131
  - lib/telegram/bot/client.rb
130
132
  - lib/telegram/bot/exceptions.rb
131
133
  - lib/telegram/bot/exceptions/base.rb
132
134
  - lib/telegram/bot/exceptions/response_error.rb
135
+ - lib/telegram/bot/null_logger.rb
133
136
  - lib/telegram/bot/types.rb
134
137
  - lib/telegram/bot/types/audio.rb
135
138
  - lib/telegram/bot/types/base.rb