telegrammer 0.4.1 → 0.5.0
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/telegrammer.rb +7 -0
- data/lib/telegrammer/api_response.rb +7 -3
- data/lib/telegrammer/bot.rb +34 -10
- data/lib/telegrammer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93c3f40e3cac5da380be28df9f3e8764fb391c48
|
4
|
+
data.tar.gz: 6e3d816aa9669bc7efe93d773025f68fa4886e85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e6543b008d751c428095168dade2d35696030dc1c3255145bfc66fd3c7ad69de63c83b39bbc5634998429b0fa268bb5ffa536f984e2c9ff60fe098c0c3cedb9
|
7
|
+
data.tar.gz: 1905d67b7f5454d079ae4188711d9d98a818cd0689b3debb364c686bd68573f895f3ab2de4f159ef85c3dcb08df837eebb6a316a04e08b5c97950036f4c0aba3
|
data/CHANGELOG.md
CHANGED
data/lib/telegrammer.rb
CHANGED
@@ -56,5 +56,12 @@ module Telegrammer
|
|
56
56
|
super("Telegram API Service unavailable (HTTP error code #{status_code})")
|
57
57
|
end
|
58
58
|
end
|
59
|
+
|
60
|
+
# Error returned when HTTPClient raise a timeout (?)
|
61
|
+
class TimeoutError < StandardError
|
62
|
+
def initialize(message)
|
63
|
+
super("Timeout reached. Message: #{message}")
|
64
|
+
end
|
65
|
+
end
|
59
66
|
end
|
60
67
|
end
|
@@ -4,7 +4,7 @@ module Telegrammer
|
|
4
4
|
attr_reader :result
|
5
5
|
attr_reader :success
|
6
6
|
|
7
|
-
def initialize(response)
|
7
|
+
def initialize(response,fail_silently = false)
|
8
8
|
if response.status < 500
|
9
9
|
@body = response.body
|
10
10
|
|
@@ -14,10 +14,14 @@ module Telegrammer
|
|
14
14
|
if @success
|
15
15
|
@result = data['result']
|
16
16
|
else
|
17
|
-
|
17
|
+
if !fail_silently
|
18
|
+
fail Telegrammer::Errors::BadRequestError, data['error_code'], data['description']
|
19
|
+
end
|
18
20
|
end
|
19
21
|
else
|
20
|
-
|
22
|
+
if !fail_silently
|
23
|
+
fail Telegrammer::Errors::ServiceUnavailableError, response.status
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
|
data/lib/telegrammer/bot.rb
CHANGED
@@ -18,6 +18,7 @@ module Telegrammer
|
|
18
18
|
@api_token = api_token
|
19
19
|
@offset = 0
|
20
20
|
@timeout = 60
|
21
|
+
@fail_silently = false
|
21
22
|
@connection = HTTPClient.new
|
22
23
|
|
23
24
|
@me = get_me
|
@@ -25,6 +26,11 @@ module Telegrammer
|
|
25
26
|
|
26
27
|
# Get incoming updates using long polling
|
27
28
|
#
|
29
|
+
# @param [Hash] opts Options when getting updates
|
30
|
+
# @option params [Integer] :fail_silently Optional. Ignore every Connection Error. Default: false
|
31
|
+
# @option params [Integer] :offset Optional. Sequential number of the first photo to be returned. By default, all photos are returned.
|
32
|
+
# @option params [Integer] :timeout Optional. Timeout in minutes. 0 for short polling, Default: 60.
|
33
|
+
#
|
28
34
|
# @example
|
29
35
|
# bot = Telegrammer::Bot.new('[YOUR TELEGRAM TOKEN]')
|
30
36
|
#
|
@@ -36,11 +42,23 @@ module Telegrammer
|
|
36
42
|
# # To learn more about commands, see https://core.telegram.org/bots#commands
|
37
43
|
# end
|
38
44
|
#
|
45
|
+
# bot.get_updates({fail_silently:true, timeout:20}) do |message|
|
46
|
+
# puts "In chat #{message.chat.id}, @#{message.from.username} said: #{message.text}"
|
47
|
+
# bot.send_message(chat_id: message.chat.id, text: "You said: #{message.text}")
|
48
|
+
#
|
49
|
+
# # Here you can also process message text to detect user commands
|
50
|
+
# # To learn more about commands, see https://core.telegram.org/bots#commands
|
51
|
+
# end
|
52
|
+
#
|
39
53
|
# @raise [Telegrammer::Errors::BadRequestError] if something goes wrong in the Telegram API servers with the params received by the operation
|
40
54
|
# @raise [Telegrammer::Errors::ServiceUnavailableError] if Telegram servers are down
|
41
|
-
|
55
|
+
# @raise [Telegrammer::Errors::TimeoutError] if HTTPClient connection goes timeout
|
56
|
+
def get_updates(opts={}, &_block)
|
42
57
|
loop do
|
43
|
-
|
58
|
+
if opts[:fail_silently]
|
59
|
+
@fail_silently = true
|
60
|
+
end
|
61
|
+
response = api_request('getUpdates', { offset: opts[:offset] || @offset, timeout: opts[:timeout] || @timeout }, nil)
|
44
62
|
|
45
63
|
response.result.each do |raw_update|
|
46
64
|
update = Telegrammer::DataTypes::Update.new(raw_update)
|
@@ -440,14 +458,20 @@ module Telegrammer
|
|
440
458
|
end
|
441
459
|
end
|
442
460
|
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
461
|
+
begin
|
462
|
+
response = @connection.post(
|
463
|
+
"#{API_ENDPOINT}/#{api_uri}",
|
464
|
+
validated_params,
|
465
|
+
'User-Agent' => "Telegrammer/#{Telegrammer::VERSION}",
|
466
|
+
'Accept' => 'application/json'
|
467
|
+
)
|
468
|
+
|
469
|
+
ApiResponse.new(response,@fail_silently)
|
470
|
+
rescue HTTPClient::ReceiveTimeoutError => e
|
471
|
+
if !@fail_silently
|
472
|
+
fail Telegrammer::Errors::TimeoutError, e.to_s
|
473
|
+
end
|
474
|
+
end
|
451
475
|
end
|
452
476
|
|
453
477
|
def send_something(object_kind, params, extra_params_validation = {})
|
data/lib/telegrammer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegrammer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luis Mayoral
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|