Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Telegram.bots_config = {
token: CHAT_BOT_TOKEN,
username: 'ChatBot', # to support commands with mentions (/help@ChatBot)
server: 'http://local.bot.api.server', # for Local Bot API Server
webhook_token: 'webhook_secret', # optional, to authorize incoming requests
},
}

Expand All @@ -118,6 +119,7 @@ development:
token: TOKEN
username: SomeBot
server: http://local.bot.api.server
webhook_token: 'webhook_secret'

# For multiple bots in single app use hash of `internal_bot_id => settings`
bots:
Expand Down
3 changes: 2 additions & 1 deletion lib/telegram/bot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ def error_for_response(response)
end
end

attr_reader :client, :token, :username, :base_uri
attr_reader :client, :token, :username, :webhook_token, :base_uri

def initialize(token = nil, username = nil, server: SERVER, **options)
@client = HTTPClient.new
@token = token || options[:token]
@username = username || options[:username]
@webhook_token = options[:webhook_token]
@base_uri = format(URL_TEMPLATE, server: server, token: self.token)
end

Expand Down
14 changes: 12 additions & 2 deletions lib/telegram/bot/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ def initialize(bot, controller)
def call(env)
request = ActionDispatch::Request.new(env)
update = request.request_parameters
controller.dispatch(bot, update, request)
[200, {}, ['']]
if webhook_token_matches?(bot, request)
controller.dispatch(bot, update, request)
[200, {}, ['']]
else
[403, {}, ['Forbidden']]
end
end

def inspect
"#<#{self.class.name}(#{controller&.name})>"
end

private

def webhook_token_matches?(bot, request)
request.headers['X-Telegram-Bot-Api-Secret-Token'] == bot.webhook_token
end
end
end
end
1 change: 1 addition & 0 deletions lib/telegram/bot/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def set_webhook
certificate: cert,
ip_address: ENV.fetch('IP_ADDRESS', nil),
drop_pending_updates: drop_pending_updates,
secret_token: bot.webhook_token,
)
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/telegram/bot/config_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
chat: {
token: 'chat_token',
username: 'Chat',
webhook_token: 'webhook_secret_token',
},
other_chat: {
'token' => 'other_chat_token',
Expand Down Expand Up @@ -43,6 +44,7 @@
its(:id) { should eq :chat }
its(:token) { should eq config[:chat][:token] }
its(:username) { should eq config[:chat][:username] }
its(:webhook_token) { should eq config[:chat][:webhook_token] }
end

context 'configured by hash with stringified keys' do
Expand Down
2 changes: 1 addition & 1 deletion spec/telegram/bot/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe Telegram::Bot::Middleware do
let(:instance) { described_class.new bot, controller }
let(:bot) { double(:bot) }
let(:bot) { double(:bot, webhook_token: nil) }
let(:controller) { double(:controller, dispatch: :dispatch_result) }

describe '#call' do
Expand Down
1 change: 1 addition & 0 deletions spec/telegram/bot/routes_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def assert_route(bot, controller, path: nil, **expected_options) # rubocop:disab
expect(middleware.controller).to eq(controller)
expect(middleware.bot.token).to eq(bot.token)
expect(middleware.bot.username).to eq(bot.username)
expect(middleware.bot.webhook_token).to eq(bot.webhook_token)
expect(actual_options).to include(expected_options)
end
yield
Expand Down