зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-08-28 15:26:18 +03:00
Сравнить коммиты
16 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
995cde5607 | ||
|
|
9ba5a817cd | ||
|
|
0205891985 | ||
|
|
7393b1a1f3 | ||
|
|
43b519c5e8 | ||
|
|
93ff9851f6 | ||
|
|
7c1f1b5b63 | ||
|
|
028849a617 | ||
|
|
ba1b3c0974 | ||
|
|
f61cc1e536 | ||
|
|
2f7ff9d50e | ||
|
|
aca35a1934 | ||
|
|
5bf98b419b | ||
|
|
8077ef9bcf | ||
|
|
5fef6fb43a | ||
|
|
07be01c07d |
@@ -2,5 +2,8 @@ language: ruby
|
||||
cache: bundler
|
||||
rvm:
|
||||
- 2.2.3
|
||||
env:
|
||||
- RAILS=4
|
||||
- RAILS=5
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
21
CHANGELOG.md
21
CHANGELOG.md
@@ -1,4 +1,23 @@
|
||||
# 0.7.1
|
||||
# 0.8.0
|
||||
|
||||
- Fixed `#reply_with`, now it sets `reply_to_message_id` as it's supposed to.
|
||||
Added `#respond_with` which works the same way, but doesn't set `reply_to_message_id`.
|
||||
Please, replace all occurrences of `reply_with` to `respond_with` to
|
||||
keep it working the old way.
|
||||
- Fixes for Rails 5:
|
||||
- Controller callbacks
|
||||
- Middleware
|
||||
- Setup travis builds
|
||||
|
||||
# 0.7.4
|
||||
|
||||
- Rails 5 support by @dreyks (#4).
|
||||
|
||||
# 0.7.3
|
||||
|
||||
- Fixed issues with poller in production (#3)
|
||||
|
||||
# 0.7.2
|
||||
|
||||
- Bot API 2.1
|
||||
- Fixed possible crashes when payload type is not supported.
|
||||
|
||||
7
Gemfile
7
Gemfile
@@ -1,6 +1,13 @@
|
||||
source 'https://rubygems.org'
|
||||
gemspec
|
||||
|
||||
case ENV['RAILS']
|
||||
when '5'
|
||||
gem 'actionpack', '5.0.0.rc1'
|
||||
when '4'
|
||||
gem 'actionpack', '~> 4.2'
|
||||
end
|
||||
|
||||
group :development do
|
||||
gem 'sdoc', '~> 0.4.1'
|
||||
gem 'pry', '~> 0.10.1'
|
||||
|
||||
27
README.md
27
README.md
@@ -131,9 +131,10 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||
|
||||
# There are `chat` & `from` shortcut methods.
|
||||
response = from ? "Hello #{from['username']}!" : 'Hi there!'
|
||||
# There is `reply_with` helper to set basic fields
|
||||
# like `reply_to_message` & `chat_id`.
|
||||
reply_with :message, text: response
|
||||
# There is `respond_with` helper to set `chat_id` from received message:
|
||||
respond_with :message, text: response
|
||||
# `reply_with` also sets `reply_to_message_id`:
|
||||
reply_with :photo, photo: File.open('party.jpg')
|
||||
end
|
||||
|
||||
private
|
||||
@@ -188,7 +189,7 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||
end
|
||||
|
||||
def read
|
||||
reply_with :message, text: session[:text]
|
||||
respond_with :message, text: session[:text]
|
||||
end
|
||||
|
||||
private
|
||||
@@ -214,23 +215,23 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||
def rename(*)
|
||||
# set context for the next message
|
||||
save_context :rename
|
||||
reply_with :message, text: 'What name do you like?'
|
||||
respond_with :message, text: 'What name do you like?'
|
||||
end
|
||||
|
||||
# register context handlers to handle this context
|
||||
context_handler :rename do |*words|
|
||||
update_name words[0]
|
||||
reply_with :message, text: 'Renamed!'
|
||||
respond_with :message, text: 'Renamed!'
|
||||
end
|
||||
|
||||
# You can do it in other way:
|
||||
def rename(name = nil, *)
|
||||
if name
|
||||
update_name name
|
||||
reply_with :message, text: 'Renamed!'
|
||||
respond_with :message, text: 'Renamed!'
|
||||
else
|
||||
save_context :rename
|
||||
reply_with :message, text: 'What name do you like?'
|
||||
respond_with :message, text: 'What name do you like?'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -383,6 +384,16 @@ To release a new version, update the version number in `version.rb`,
|
||||
and then run `bundle exec rake release`, which will create a git tag for the version,
|
||||
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
||||
|
||||
### Different Rails versions
|
||||
|
||||
To setup development for specific major Rails version use:
|
||||
|
||||
```
|
||||
RAILS=5 bundle install
|
||||
# or
|
||||
RAILS=5 bundle update
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Bug reports and pull requests are welcome on GitHub at https://github.com/telegram-bot-rb/telegram-bot.
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
namespace :telegram do
|
||||
namespace :bot do
|
||||
desc 'Run poller'
|
||||
task poller: :environment do
|
||||
console = ActiveSupport::Logger.new(STDERR)
|
||||
Rails.logger.extend ActiveSupport::Logger.broadcast console
|
||||
desc 'Run poller. It broadcasts Rails.logger to STDOUT in dev like `rails s` do. ' \
|
||||
'Use LOG_TO_STDOUT to enable/disable broadcasting.'
|
||||
task :poller do
|
||||
ENV['BOT_POLLER_MODE'] = 'true'
|
||||
Rake::Task['environment'].invoke
|
||||
if ENV.fetch('LOG_TO_STDOUT') { Rails.env.development? }.present?
|
||||
console = ActiveSupport::Logger.new(STDERR)
|
||||
Rails.logger.extend ActiveSupport::Logger.broadcast console
|
||||
end
|
||||
Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
|
||||
end
|
||||
|
||||
|
||||
@@ -71,11 +71,16 @@ module Telegram
|
||||
answerCallbackQuery
|
||||
answerInlineQuery
|
||||
forwardMessage
|
||||
getChat
|
||||
getChatAdministrators
|
||||
getChatMember
|
||||
getChatMembersCount
|
||||
getFile
|
||||
getMe
|
||||
getUpdates
|
||||
getUserProfilePhotos
|
||||
kickChatMember
|
||||
leaveChat
|
||||
sendAudio
|
||||
sendChatAction
|
||||
sendContact
|
||||
|
||||
@@ -14,9 +14,14 @@ module Telegram
|
||||
# It just tells routes helpers whether to add routed bots to
|
||||
# Bot::UpdatesPoller, so their config will be available by bot key in
|
||||
# Bot::UpdatesPoller.start.
|
||||
#
|
||||
# It's enabled by default in Rails dev environment and `rake telegram:bot:poller`
|
||||
# task. Use `BOT_POLLER_MODE=true` envvar to set it manually.
|
||||
def bot_poller_mode?
|
||||
return @bot_poller_mode if defined?(@bot_poller_mode)
|
||||
Rails.env.development? if defined?(Rails)
|
||||
@bot_poller_mode = ENV.fetch('BOT_POLLER_MODE') do
|
||||
Rails.env.development? if defined?(Rails)
|
||||
end
|
||||
end
|
||||
|
||||
# Hash of bots made with bots_config.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'active_support/concern'
|
||||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
require 'active_support/json'
|
||||
require 'action_dispatch/http/mime_type'
|
||||
require 'action_dispatch/middleware/params_parser'
|
||||
require 'action_dispatch/http/request'
|
||||
|
||||
module Telegram
|
||||
module Bot
|
||||
@@ -13,7 +15,8 @@ module Telegram
|
||||
end
|
||||
|
||||
def call(env)
|
||||
update = env['action_dispatch.request.request_parameters']
|
||||
request = ActionDispatch::Request.new(env)
|
||||
update = request.request_parameters
|
||||
controller.dispatch(bot, update)
|
||||
[200, {}, ['']]
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module Telegram
|
||||
# end
|
||||
#
|
||||
# def help(*)
|
||||
# reply_with :message, text:
|
||||
# respond_with :message, text:
|
||||
# end
|
||||
#
|
||||
# To process plain text messages (without commands) or other updates just
|
||||
@@ -29,7 +29,7 @@ module Telegram
|
||||
# as an argument.
|
||||
#
|
||||
# def message(message)
|
||||
# reply_with :message, text: "Echo: #{message['text']}"
|
||||
# respond_with :message, text: "Echo: #{message['text']}"
|
||||
# end
|
||||
#
|
||||
# def inline_query(query)
|
||||
@@ -63,7 +63,7 @@ module Telegram
|
||||
|
||||
include AbstractController::Callbacks
|
||||
# Redefine callbacks with default terminator.
|
||||
if ActiveSupport.gem_version >= Gem::Version.new('5')
|
||||
if ActiveSupport::VERSION::MAJOR >= 5
|
||||
define_callbacks :process_action,
|
||||
skip_after_callbacks_if_terminated: true
|
||||
else
|
||||
|
||||
@@ -6,7 +6,7 @@ module Telegram
|
||||
module Instrumentation
|
||||
class << self
|
||||
def prepended(base)
|
||||
base.config_accessor :logger
|
||||
base.send :config_accessor, :logger
|
||||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
@@ -35,13 +35,13 @@ module Telegram
|
||||
end
|
||||
end
|
||||
|
||||
def reply_with(type, *)
|
||||
Instrumentation.instrument(:reply_with, type: type) { super }
|
||||
def respond_with(type, *)
|
||||
Instrumentation.instrument(:respond_with, type: type) { super }
|
||||
end
|
||||
|
||||
%i(answer_callback_query answer_inline_query).each do |type|
|
||||
define_method(type) do |*args|
|
||||
Instrumentation.instrument(:reply_with, type: type) { super(*args) }
|
||||
Instrumentation.instrument(:respond_with, type: type) { super(*args) }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -22,8 +22,8 @@ module Telegram
|
||||
end
|
||||
end
|
||||
|
||||
def reply_with(event)
|
||||
info { "Replied with #{event.payload[:type]}" }
|
||||
def respond_with(event)
|
||||
info { "Responded with #{event.payload[:type]}" }
|
||||
end
|
||||
|
||||
def halted_callback(event)
|
||||
|
||||
@@ -2,21 +2,23 @@ module Telegram
|
||||
module Bot
|
||||
class UpdatesController
|
||||
module ReplyHelpers
|
||||
# Helper to call bot's `send_#{type}` method with already set `chat_id` and
|
||||
# `reply_to_message_id`:
|
||||
# Helper to call bot's `send_#{type}` method with already set `chat_id`:
|
||||
#
|
||||
# reply_with :message, text: 'Hello!'
|
||||
# reply_with :message, text: '__Hello!__', parse_mode: :Markdown
|
||||
# reply_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!"
|
||||
def reply_with(type, params)
|
||||
method = "send_#{type}"
|
||||
# respond_with :message, text: 'Hello!'
|
||||
# respond_with :message, text: '__Hello!__', parse_mode: :Markdown
|
||||
# respond_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!"
|
||||
def respond_with(type, params)
|
||||
chat = self.chat
|
||||
chat_id = chat && chat['id'] or raise 'Can not respond_with when chat is not present'
|
||||
bot.public_send("send_#{type}", params.merge(chat_id: chat_id))
|
||||
end
|
||||
|
||||
# Same as respond_with but also sets `reply_to_message_id`.
|
||||
def reply_with(type, params)
|
||||
payload = self.payload
|
||||
params = params.merge(
|
||||
chat_id: (chat && chat['id'] or raise 'Can not reply_with when chat is not present'),
|
||||
reply_to_message: payload && payload['message_id'],
|
||||
)
|
||||
bot.public_send(method, params)
|
||||
message_id = payload && payload['message_id']
|
||||
params = params.merge(reply_to_message_id: message_id) if message_id
|
||||
respond_with(type, params)
|
||||
end
|
||||
|
||||
# Same as reply_with, but for inline queries.
|
||||
|
||||
@@ -77,11 +77,25 @@ module Telegram
|
||||
|
||||
def reload!
|
||||
return yield unless reload
|
||||
ActionDispatch::Reloader.prepare!
|
||||
if controller.is_a?(Class) && controller.name
|
||||
@controller = Object.const_get(controller.name)
|
||||
reloading_code do
|
||||
if controller.is_a?(Class) && controller.name
|
||||
@controller = Object.const_get(controller.name)
|
||||
end
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
if defined?(Rails) && Rails.application.respond_to?(:reloader)
|
||||
def reloading_code
|
||||
Rails.application.reloader.wrap do
|
||||
yield
|
||||
end
|
||||
end
|
||||
else
|
||||
def reloading_code
|
||||
ActionDispatch::Reloader.prepare!
|
||||
yield.tap { ActionDispatch::Reloader.cleanup! }
|
||||
end
|
||||
yield.tap { ActionDispatch::Reloader.cleanup! }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module Telegram
|
||||
module Bot
|
||||
VERSION = '0.7.1'.freeze
|
||||
VERSION = '0.8.0'.freeze
|
||||
|
||||
def self.gem_version
|
||||
Gem::Version.new VERSION
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require 'rack/mock'
|
||||
|
||||
RSpec.describe Telegram::Bot::Middleware do
|
||||
let(:instance) { described_class.new bot, controller }
|
||||
let(:bot) { double(:bot) }
|
||||
@@ -6,10 +8,25 @@ RSpec.describe Telegram::Bot::Middleware do
|
||||
describe '#call' do
|
||||
subject { instance.call(env) }
|
||||
let(:env) { {'action_dispatch.request.request_parameters' => json_body} }
|
||||
let(:json_body) { double(:json_body) }
|
||||
let(:update) { {'message' => {'id' => 1}} }
|
||||
let(:env) do
|
||||
Rack::MockRequest.env_for('/',
|
||||
method: :post,
|
||||
input: JSON.dump(update),
|
||||
'CONTENT_TYPE' => 'application/json',
|
||||
)
|
||||
end
|
||||
|
||||
require 'action_pack/version'
|
||||
if ActionPack::VERSION::MAJOR < 5
|
||||
# Before Rails 5, params are parsed in middleware.
|
||||
# In Rails 5, they are parsed in Request#request_parameters.
|
||||
require 'action_dispatch/middleware/params_parser'
|
||||
let(:instance) { ActionDispatch::ParamsParser.new(super()) }
|
||||
end
|
||||
|
||||
it 'calls dispatch on controller' do
|
||||
expect(controller).to receive(:dispatch).with(bot, json_body)
|
||||
expect(controller).to receive(:dispatch).with(bot, update)
|
||||
subject
|
||||
end
|
||||
|
||||
|
||||
@@ -1,36 +1,49 @@
|
||||
RSpec.describe Telegram::Bot::UpdatesController do
|
||||
include_context 'telegram/bot/updates_controller'
|
||||
let(:params) { {arg: 1, 'other_arg' => 2} }
|
||||
let(:respond_type) { :photo }
|
||||
let(:result) { double(:result) }
|
||||
let(:payload_type) { :message }
|
||||
let(:payload) { {message_id: double(:message_id)} }
|
||||
let(:chat) { {'id' => double(:chat_id)} }
|
||||
|
||||
describe '#reply_with' do
|
||||
subject { controller.reply_with type, params }
|
||||
let(:params) { {arg: 1, 'other_arg' => 2} }
|
||||
let(:type) { :photo }
|
||||
let(:result) { double(:result) }
|
||||
let(:payload_type) { :message }
|
||||
let(:payload) { {message_id: double(:message_id)} }
|
||||
let(:chat) { {'id' => double(:chat_id)} }
|
||||
|
||||
it 'sets chat_id & reply_to_message' do
|
||||
expect(controller).to receive(:chat) { chat }
|
||||
expect(bot).to receive("send_#{type}").with(params.merge(
|
||||
chat_id: chat['id'],
|
||||
reply_to_message: payload[:message_id],
|
||||
)) { result }
|
||||
should eq result
|
||||
end
|
||||
|
||||
shared_examples 'missing chat' do
|
||||
context 'when chat is missing' do
|
||||
let(:payload_type) { :some_type }
|
||||
it { expect { subject }.to raise_error(/chat/) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#respond_with' do
|
||||
subject { controller.respond_with respond_type, params }
|
||||
include_examples 'missing chat'
|
||||
|
||||
it 'sets chat_id & reply_to_message_id' do
|
||||
expect(controller).to receive(:chat) { chat }
|
||||
expect(bot).to receive("send_#{respond_type}").
|
||||
with(params.merge(chat_id: chat['id'])) { result }
|
||||
should eq result
|
||||
end
|
||||
end
|
||||
|
||||
describe '#reply_with' do
|
||||
subject { controller.reply_with respond_type, params }
|
||||
include_examples 'missing chat'
|
||||
|
||||
it 'sets chat_id & reply_to_message_id' do
|
||||
expect(controller).to receive(:chat) { chat }
|
||||
expect(bot).to receive("send_#{respond_type}").with(params.merge(
|
||||
chat_id: chat['id'],
|
||||
reply_to_message_id: payload[:message_id],
|
||||
)) { result }
|
||||
should eq result
|
||||
end
|
||||
|
||||
context 'when update is not set' do
|
||||
let(:update) { {chat: chat} }
|
||||
it 'sets chat_id & reply_to_message' do
|
||||
expect(bot).to receive("send_#{type}").with(params.merge(
|
||||
chat_id: chat['id'],
|
||||
reply_to_message: nil,
|
||||
)) { result }
|
||||
it 'sets chat_id' do
|
||||
expect(bot).to receive("send_#{respond_type}").
|
||||
with(params.merge(chat_id: chat['id'])) { result }
|
||||
should eq result
|
||||
end
|
||||
end
|
||||
|
||||
@@ -225,12 +225,12 @@ RSpec.describe Telegram::Bot::UpdatesController do
|
||||
it { should change(controller, :acted).to true }
|
||||
its(:call) { should eq [nil, nil, args] }
|
||||
|
||||
context 'when callback returns false' do
|
||||
context 'when callback halts chain' do
|
||||
before do
|
||||
controller_class.prepend(Module.new do
|
||||
def hook
|
||||
super
|
||||
false
|
||||
ActiveSupport::VERSION::MAJOR >= 5 ? throw(:abort) : false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
|
||||
|
||||
spec.required_ruby_version = '~> 2.0'
|
||||
|
||||
spec.add_dependency 'activesupport', '~> 4.0'
|
||||
spec.add_dependency 'actionpack', '~> 4.0'
|
||||
spec.add_dependency 'activesupport', '>= 4.0', '< 5.1'
|
||||
spec.add_dependency 'actionpack', '>= 4.0', '< 5.1'
|
||||
spec.add_dependency 'httpclient', '~> 2.7'
|
||||
spec.add_development_dependency 'bundler', '~> 1.11'
|
||||
spec.add_development_dependency 'rake', '~> 10.0'
|
||||
|
||||
Ссылка в новой задаче
Block a user