telegram-bot 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +3 -0
- data/lib/telegram/bot.rb +19 -0
- data/lib/telegram/bot/client.rb +8 -4
- data/lib/telegram/bot/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bb8d3d1265ef9c71cb7dd500ca18ca98e8bdbc6
|
4
|
+
data.tar.gz: 478eb9dde35d1ef651f1e889b66125017674c21d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9168018578598dc1ba6cefa04695ce7b2f98de74591cc82f301a8440c31fbef66cf5e7a51ec545f3937ef25a68278d573dfbc6aa22cbf54f9f79d2d520629f11
|
7
|
+
data.tar.gz: 7489fc137abd149da5d7dc8819f736363753c3a6aeccc0c5c925d541656f846f03455a60d0c8db4553d7a2e5cfaf4edd5c3c18217dfdd7dd1b6cb48f4cc58270
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -89,6 +89,9 @@ bot.extend Telegram::Bot::Client::TypedResponse
|
|
89
89
|
bot.get_me.class # => Telegram::Bot::Types::User
|
90
90
|
```
|
91
91
|
|
92
|
+
Any API request error will raise `Telegram::Bot::Error` with description in its message.
|
93
|
+
Special `Telegram::Bot::StaleChat` is raised when bot can't post messages to the chat anymore.
|
94
|
+
|
92
95
|
### Controller
|
93
96
|
|
94
97
|
```ruby
|
data/lib/telegram/bot.rb
CHANGED
@@ -7,6 +7,25 @@ module Telegram
|
|
7
7
|
class Error < StandardError; end
|
8
8
|
class NotFound < Error; end
|
9
9
|
|
10
|
+
# Error class for events when chat is not available anymore for bot.
|
11
|
+
# While Telegram has same error codes for different messages and there is no
|
12
|
+
# official docs for this error codes it uses `description` to
|
13
|
+
# check response.
|
14
|
+
class StaleChat < Error
|
15
|
+
DESCRIPTIONS = [
|
16
|
+
'bot was kicked',
|
17
|
+
"can't write to",
|
18
|
+
'group chat is deactivated',
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def match_response?(response)
|
23
|
+
description = response['description'].to_s
|
24
|
+
DESCRIPTIONS.any? { |x| description[x] }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
10
29
|
autoload :Client, 'telegram/bot/client'
|
11
30
|
autoload :ClientStub, 'telegram/bot/client_stub'
|
12
31
|
autoload :Middleware, 'telegram/bot/middleware'
|
data/lib/telegram/bot/client.rb
CHANGED
@@ -34,8 +34,9 @@ module Telegram
|
|
34
34
|
|
35
35
|
# Encodes nested hashes as json.
|
36
36
|
def prepare_body(body)
|
37
|
+
body = body.dup
|
37
38
|
body.each do |k, val|
|
38
|
-
body[k] = val.to_json if val.is_a?(Hash)
|
39
|
+
body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
@@ -67,14 +68,17 @@ module Telegram
|
|
67
68
|
client.debug_dev = nil
|
68
69
|
end
|
69
70
|
|
70
|
-
def request(action, body = {})
|
71
|
+
def request(action, body = {}) # rubocop:disable PerceivedComplexity
|
71
72
|
res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
72
73
|
status = res.status
|
73
74
|
return JSON.parse(res.body) if 300 > status
|
74
75
|
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
75
76
|
err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
|
76
|
-
|
77
|
-
|
77
|
+
if result
|
78
|
+
# NotFound is raised only for valid responses from Telegram
|
79
|
+
raise NotFound, err_msg if 404 == status
|
80
|
+
raise StaleChat, err_msg if StaleChat.match_response?(result)
|
81
|
+
end
|
78
82
|
raise Error, err_msg
|
79
83
|
end
|
80
84
|
|
data/lib/telegram/bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Melentiev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -145,3 +145,4 @@ signing_key:
|
|
145
145
|
specification_version: 4
|
146
146
|
summary: Library for building Telegram Bots with Rails integration
|
147
147
|
test_files: []
|
148
|
+
has_rdoc:
|