зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-08-28 15:26:18 +03:00
Add server option for client to support local bot API servers
Этот коммит содержится в:
@@ -4,6 +4,7 @@
|
||||
- __Breaking change!__ Default route is generated using hashed bot token.
|
||||
Please reconfigure webhook after update (`rake telegram:bot:set_webhook`).
|
||||
- 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
|
||||
|
||||
|
||||
@@ -65,7 +65,11 @@ which is used for `Telegram.bot`.
|
||||
```ruby
|
||||
Telegram.bots_config = {
|
||||
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
|
||||
@@ -87,6 +91,7 @@ development:
|
||||
bot:
|
||||
token: TOKEN
|
||||
username: SomeBot
|
||||
server: http://local.bot.api.server
|
||||
|
||||
# For multiple bots in single app use hash of `internal_bot_id => settings`
|
||||
bots:
|
||||
|
||||
@@ -5,7 +5,8 @@ require 'httpclient'
|
||||
module Telegram
|
||||
module Bot
|
||||
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'
|
||||
prepend Async
|
||||
@@ -61,11 +62,11 @@ module Telegram
|
||||
|
||||
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
|
||||
@token = token || options[:token]
|
||||
@username = username || options[:username]
|
||||
@base_uri = format(URL_TEMPLATE, token: self.token)
|
||||
@base_uri = format(URL_TEMPLATE, server: server, token: self.token)
|
||||
end
|
||||
|
||||
def request(action, body = {})
|
||||
|
||||
@@ -95,26 +95,45 @@ RSpec.describe Telegram::Bot::Client do
|
||||
|
||||
describe '.new' do
|
||||
subject { described_class.new(*args) }
|
||||
let(:token) { 'secret' }
|
||||
let(:username) { 'superbot' }
|
||||
|
||||
context 'when multiple args are given' do
|
||||
let(:args) { %w[secret superbot] }
|
||||
its(:token) { should eq args[0] }
|
||||
its(:username) { should eq args[1] }
|
||||
its(:base_uri) { should include args[0] }
|
||||
let(:args) { [token, username] }
|
||||
its(:token) { should eq token }
|
||||
its(:username) { should eq username }
|
||||
its(:base_uri) { should eq "#{described_class::SERVER}/bot#{token}/" }
|
||||
end
|
||||
|
||||
context 'when hash is given' do
|
||||
let(:args) { [token: 'secret', username: 'superbot'] }
|
||||
its(:token) { should eq args[0][:token] }
|
||||
its(:username) { should eq args[0][:username] }
|
||||
its(:base_uri) { should include args[0][:token] }
|
||||
its(:token) { should eq token }
|
||||
its(:username) { should eq username }
|
||||
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
|
||||
|
||||
describe '#request' do
|
||||
subject { -> { instance.request(action, request_body) } }
|
||||
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(:prepared_body) { double(:prepared_body) }
|
||||
let(:response) { HTTP::Message.new_response(body).tap { |x| x.status = status } }
|
||||
|
||||
Ссылка в новой задаче
Block a user