1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-08-28 15:26:18 +03:00

Сравнить коммиты

..

12 Коммитов

Автор SHA1 Сообщение Дата
Max Melentiev
995cde5607 v0.8.0 2016-06-02 21:36:05 +03:00
Max Melentiev
9ba5a817cd Fixed doubled instrumentation for reply_with 2016-06-02 21:19:27 +03:00
Max Melentiev
0205891985 Controller#respond_with to reply without reply_to_message_id 2016-06-02 21:07:15 +03:00
printercu
7393b1a1f3 Merge pull request #7 from telegram-bot-rb/rails5
Rails5 compatibility
2016-06-02 20:34:09 +03:00
Max Melentiev
43b519c5e8 fixed controller's callbacks for rails5 2016-06-02 20:30:35 +03:00
Max Melentiev
93ff9851f6 Add rails 5 to travis matrix 2016-06-02 19:48:19 +03:00
Max Melentiev
7c1f1b5b63 Middleware is rails5-compatible 2016-06-02 19:38:29 +03:00
printercu
028849a617 Merge pull request #5 from dreyks/reply_to_message_id
Fix: reply_with sets reply_to_message_id instead of reply_to_message
2016-06-01 23:22:32 +03:00
Roman Usherenko
ba1b3c0974 fix reply_to_message_id 2016-06-01 15:18:29 +03:00
Max Melentiev
f61cc1e536 v0.7.4 2016-05-31 17:44:24 +03:00
printercu
2f7ff9d50e Merge pull request #4 from dreyks/rails5
Rails5 compatibility
2016-05-31 17:34:31 +03:00
Roman Usherenko
aca35a1934 Rails5 compatibility 2016-05-31 17:12:36 +03:00
15 изменённых файлов: 150 добавлений и 65 удалений

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

@@ -2,5 +2,8 @@ language: ruby
cache: bundler
rvm:
- 2.2.3
env:
- RAILS=4
- RAILS=5
notifications:
email: false

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

@@ -1,3 +1,18 @@
# 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)

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

@@ -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'

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

@@ -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,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.3'.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'