1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-04 18:25:51 +03:00

Add server option for client to support local bot API servers

Этот коммит содержится в:
Max Melentiev
2020-11-27 18:47:47 +00:00
родитель f654071c4c
Коммит 4b9eedb5a2
4 изменённых файлов: 38 добавлений и 12 удалений

Просмотреть файл

@@ -4,6 +4,7 @@
- __Breaking change!__ Default route is generated using hashed bot token. - __Breaking change!__ Default route is generated using hashed bot token.
Please reconfigure webhook after update (`rake telegram:bot:set_webhook`). Please reconfigure webhook after update (`rake telegram:bot:set_webhook`).
- Update to Bot API 5.0, add rake tasks for `deleteWebhook`, `close` & `logOut`. - Update to Bot API 5.0, add rake tasks for `deleteWebhook`, `close` & `logOut`.
- Add `server` option for client to support local bot API servers.
# 0.14.4 # 0.14.4

Просмотреть файл

@@ -65,7 +65,11 @@ which is used for `Telegram.bot`.
```ruby ```ruby
Telegram.bots_config = { Telegram.bots_config = {
default: DEFAULT_BOT_TOKEN, default: DEFAULT_BOT_TOKEN,
chat: {token: CHAT_BOT_TOKEN, username: 'chatbot'}, chat: {
token: CHAT_BOT_TOKEN,
username: 'ChatBot', # to support commands with mentions (/help@ChatBot)
server: 'http://local.bot.api.server', # for Local Bot API Server
},
} }
Telegram.bot.get_updates Telegram.bot.get_updates
@@ -87,6 +91,7 @@ development:
bot: bot:
token: TOKEN token: TOKEN
username: SomeBot username: SomeBot
server: http://local.bot.api.server
# For multiple bots in single app use hash of `internal_bot_id => settings` # For multiple bots in single app use hash of `internal_bot_id => settings`
bots: bots:

Просмотреть файл

@@ -5,7 +5,8 @@ require 'httpclient'
module Telegram module Telegram
module Bot module Bot
class Client class Client
URL_TEMPLATE = 'https://api.telegram.org/bot%<token>s/'.freeze SERVER = 'https://api.telegram.org'.freeze
URL_TEMPLATE = '%<server>s/bot%<token>s/'.freeze
autoload :TypedResponse, 'telegram/bot/client/typed_response' autoload :TypedResponse, 'telegram/bot/client/typed_response'
prepend Async prepend Async
@@ -61,11 +62,11 @@ module Telegram
attr_reader :client, :token, :username, :base_uri attr_reader :client, :token, :username, :base_uri
def initialize(token = nil, username = nil, **options) def initialize(token = nil, username = nil, server: SERVER, **options)
@client = HTTPClient.new @client = HTTPClient.new
@token = token || options[:token] @token = token || options[:token]
@username = username || options[:username] @username = username || options[:username]
@base_uri = format(URL_TEMPLATE, token: self.token) @base_uri = format(URL_TEMPLATE, server: server, token: self.token)
end end
def request(action, body = {}) def request(action, body = {})

Просмотреть файл

@@ -95,26 +95,45 @@ RSpec.describe Telegram::Bot::Client do
describe '.new' do describe '.new' do
subject { described_class.new(*args) } subject { described_class.new(*args) }
let(:token) { 'secret' }
let(:username) { 'superbot' }
context 'when multiple args are given' do context 'when multiple args are given' do
let(:args) { %w[secret superbot] } let(:args) { [token, username] }
its(:token) { should eq args[0] } its(:token) { should eq token }
its(:username) { should eq args[1] } its(:username) { should eq username }
its(:base_uri) { should include args[0] } its(:base_uri) { should eq "#{described_class::SERVER}/bot#{token}/" }
end end
context 'when hash is given' do context 'when hash is given' do
let(:args) { [token: 'secret', username: 'superbot'] } let(:args) { [token: 'secret', username: 'superbot'] }
its(:token) { should eq args[0][:token] } its(:token) { should eq token }
its(:username) { should eq args[0][:username] } its(:username) { should eq username }
its(:base_uri) { should include args[0][:token] } its(:base_uri) { should eq "#{described_class::SERVER}/bot#{token}/" }
end
context 'with custom server' do
let(:server) { 'http://my.server' }
let(:args) { [token, username, server: server] }
its(:base_uri) { should eq "#{server}/bot#{token}/" }
context 'and hash options' do
let(:args) { [token: token, username: username, server: server] }
its(:base_uri) { should eq "#{server}/bot#{token}/" }
end
end end
end end
describe '#request' do describe '#request' do
subject { -> { instance.request(action, request_body) } } subject { -> { instance.request(action, request_body) } }
let(:action) { :some_action } let(:action) { :some_action }
let(:url) { "#{format(described_class::URL_TEMPLATE, token: token)}#{action}" } let(:url) do
base_uri = format(described_class::URL_TEMPLATE,
server: described_class::SERVER,
token: token,
)
"#{base_uri}#{action}"
end
let(:request_body) { double(:body) } let(:request_body) { double(:body) }
let(:prepared_body) { double(:prepared_body) } let(:prepared_body) { double(:prepared_body) }
let(:response) { HTTP::Message.new_response(body).tap { |x| x.status = status } } let(:response) { HTTP::Message.new_response(body).tap { |x| x.status = status } }