зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 09:46:17 +03:00
Сравнить коммиты
3 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
62475965e9 | ||
|
|
d7a20a745c | ||
|
|
1963533e33 |
@@ -1,3 +1,8 @@
|
|||||||
|
# 0.6.0
|
||||||
|
|
||||||
|
- StaleChat error.
|
||||||
|
- Encode arrays as json in request body.
|
||||||
|
|
||||||
# 0.5.0
|
# 0.5.0
|
||||||
|
|
||||||
- MessageContext.
|
- MessageContext.
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ bot.extend Telegram::Bot::Client::TypedResponse
|
|||||||
bot.get_me.class # => Telegram::Bot::Types::User
|
bot.get_me.class # => Telegram::Bot::Types::User
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Any API request error will raise `Telegram::Bot::Error` with description in its message.
|
||||||
|
Special `Telegram::Bot::StaleChat` is raised when bot can't post messages to the chat anymore.
|
||||||
|
|
||||||
### Controller
|
### Controller
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|||||||
@@ -7,6 +7,25 @@ module Telegram
|
|||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
class NotFound < Error; end
|
class NotFound < Error; end
|
||||||
|
|
||||||
|
# Error class for events when chat is not available anymore for bot.
|
||||||
|
# While Telegram has same error codes for different messages and there is no
|
||||||
|
# official docs for this error codes it uses `description` to
|
||||||
|
# check response.
|
||||||
|
class StaleChat < Error
|
||||||
|
DESCRIPTIONS = [
|
||||||
|
'bot was kicked',
|
||||||
|
"can't write to",
|
||||||
|
'group chat is deactivated',
|
||||||
|
].freeze
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def match_response?(response)
|
||||||
|
description = response['description'].to_s
|
||||||
|
DESCRIPTIONS.any? { |x| description[x] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
autoload :Client, 'telegram/bot/client'
|
autoload :Client, 'telegram/bot/client'
|
||||||
autoload :ClientStub, 'telegram/bot/client_stub'
|
autoload :ClientStub, 'telegram/bot/client_stub'
|
||||||
autoload :Middleware, 'telegram/bot/middleware'
|
autoload :Middleware, 'telegram/bot/middleware'
|
||||||
|
|||||||
@@ -34,8 +34,9 @@ module Telegram
|
|||||||
|
|
||||||
# Encodes nested hashes as json.
|
# Encodes nested hashes as json.
|
||||||
def prepare_body(body)
|
def prepare_body(body)
|
||||||
|
body = body.dup
|
||||||
body.each do |k, val|
|
body.each do |k, val|
|
||||||
body[k] = val.to_json if val.is_a?(Hash)
|
body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -67,14 +68,17 @@ module Telegram
|
|||||||
client.debug_dev = nil
|
client.debug_dev = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def request(action, body = {})
|
def request(action, body = {}) # rubocop:disable PerceivedComplexity
|
||||||
res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
||||||
status = res.status
|
status = res.status
|
||||||
return JSON.parse(res.body) if 300 > status
|
return JSON.parse(res.body) if 300 > status
|
||||||
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
||||||
err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
|
err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
|
||||||
# NotFound is raised only for valid responses from Telegram
|
if result
|
||||||
raise NotFound, err_msg if 404 == status && result
|
# NotFound is raised only for valid responses from Telegram
|
||||||
|
raise NotFound, err_msg if 404 == status
|
||||||
|
raise StaleChat, err_msg if StaleChat.match_response?(result)
|
||||||
|
end
|
||||||
raise Error, err_msg
|
raise Error, err_msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
VERSION = '0.5.0'.freeze
|
VERSION = '0.6.0'.freeze
|
||||||
|
|
||||||
def self.gem_version
|
def self.gem_version
|
||||||
Gem::Version.new VERSION
|
Gem::Version.new VERSION
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
subject { described_class.prepare_body(input) }
|
subject { described_class.prepare_body(input) }
|
||||||
|
|
||||||
context 'when plain hash is given' do
|
context 'when plain hash is given' do
|
||||||
let(:input) { {a: 1, b: '2', c: [1, 2]} }
|
let(:input) { {a: 1, b: '2', c: nil} }
|
||||||
it { should eq input }
|
it { should eq input }
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
|
|
||||||
it 'encodes nested hashes to json' do
|
it 'encodes nested hashes to json' do
|
||||||
expected = input.dup
|
expected = input.dup
|
||||||
%i(d e).each { |x| expected[x] = expected[x].to_json }
|
%i(c d e).each { |x| expected[x] = expected[x].to_json }
|
||||||
should eq expected
|
should eq expected
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,4 +2,22 @@ RSpec.describe Telegram::Bot do
|
|||||||
it 'has a version number' do
|
it 'has a version number' do
|
||||||
expect(described_class::VERSION).not_to be nil
|
expect(described_class::VERSION).not_to be nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe described_class::StaleChat do
|
||||||
|
describe '.match_response?' do
|
||||||
|
subject { ->(val) { described_class.match_response?(val) } }
|
||||||
|
|
||||||
|
it 'returns true for specific errors' do
|
||||||
|
expect(subject.call({})).to eq false
|
||||||
|
expect(subject.call('description' => 'test')).to eq false
|
||||||
|
expect(subject.call('description' => 'Error: bot was kicked from')).to eq true
|
||||||
|
expect(subject.call(
|
||||||
|
'description' => "Forbidden: can't write to private chat with deleted user"
|
||||||
|
)).to eq true
|
||||||
|
expect(subject.call(
|
||||||
|
'description' => 'Bad request: group chat is deactivated'
|
||||||
|
)).to eq true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user