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

Basic actions are called with most useful params instead of payload

Этот коммит содержится в:
Max Melentiev
2016-05-17 10:49:47 +03:00
родитель 4593a10dec
Коммит 8994328654
4 изменённых файлов: 48 добавлений и 15 удалений

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

@@ -2,6 +2,9 @@
- New Bot API methods. - New Bot API methods.
- Helpers for inline keyboards. - Helpers for inline keyboards.
- Changed action methods signature
- `#inline_query(payload) -> #inline_query(query, offset)`
- `#chosen_inline_result(payload)` -> `#chosen_inline_result(result_id, query)`
# 0.6.0 # 0.6.0

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

@@ -102,7 +102,8 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
# use callbacks like in any other controllers # use callbacks like in any other controllers
around_action :with_locale around_action :with_locale
# Every update can have one of: message, inline_query & chosen_inline_result. # Every update can have one of: message, inline_query, chosen_inline_result,
# callback_query.
# Define method with same name to respond to this updates. # Define method with same name to respond to this updates.
def message(message) def message(message)
# message can be also accessed via instance method # message can be also accessed via instance method
@@ -110,6 +111,13 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
# store_message(message['text']) # store_message(message['text'])
end end
# This basic methods receives commonly used params:
#
# message(payload)
# inline_query(query, offset)
# chosen_inline_result(result_id, query)
# callback_query(data)
# Define public methods to respond to commands. # Define public methods to respond to commands.
# Command arguments will be parsed and passed to the method. # Command arguments will be parsed and passed to the method.
# Be sure to use splat args and default values to not get errors when # Be sure to use splat args and default values to not get errors when

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

@@ -158,16 +158,16 @@ module Telegram
end end
# Calculates action name and args for payload. # Calculates action name and args for payload.
# If payload is a message with command, then returned action is an # Uses `action_for_#{payload_type}` methods.
# action for this command. Otherwise it's the same as payload type. # If this method doesn't return anything
# it uses fallback with action same as payload type.
# Returns array `[is_command?, action, args]`. # Returns array `[is_command?, action, args]`.
def action_for_payload def action_for_payload
case payload_type send("action_for_#{payload_type}") || [false, payload_type, [payload]]
when 'message' then action_for_message
when 'callback_query' then action_for_callback_query
end || [false, payload_type, [payload]]
end end
# If payload is a message with command, then returned action is an
# action for this command.
# Separate method, so it can be easily overriden (ex. MessageContext). # Separate method, so it can be easily overriden (ex. MessageContext).
def action_for_message def action_for_message
cmd, args = self.class.command_from_text(payload['text'], bot_username) cmd, args = self.class.command_from_text(payload['text'], bot_username)
@@ -175,9 +175,16 @@ module Telegram
[true, cmd, args] if cmd [true, cmd, args] if cmd
end end
# Same purpose as #action_for_message. def action_for_inline_query
[false, payload_type, [payload['query'], payload['offset']]]
end
def action_for_chosen_inline_result
[false, payload_type, [payload['result_id'], payload['query']]]
end
def action_for_callback_query def action_for_callback_query
[false, payload_type, [payload]] [false, payload_type, [payload['data']]]
end end
# Silently ignore unsupported messages. # Silently ignore unsupported messages.

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

@@ -80,11 +80,26 @@ RSpec.describe Telegram::Bot::UpdatesController do
describe '#action_for_payload' do describe '#action_for_payload' do
subject { controller.action_for_payload } subject { controller.action_for_payload }
(described_class::PAYLOAD_TYPES - %w(message)).each do |type| def stub_payload(*fields)
context "when payload is #{type}" do Hash[fields.map { |x| [x, double(x)] }]
let(:payload_type) { type } end
it { should eq [false, type, [payload]] }
end context 'when payload is inline_query' do
let(:payload_type) { 'inline_query' }
let(:payload) { stub_payload(:id, :from, :location, :query, :offset) }
it { should eq [false, payload_type, payload.values_at(:query, :offset)] }
end
context 'when payload is chosen_inline_result' do
let(:payload_type) { 'chosen_inline_result' }
let(:payload) { stub_payload(:result_id, :from, :location, :inline_message_id, :query) }
it { should eq [false, payload_type, payload.values_at(:result_id, :query)] }
end
context 'when payload is callback_query' do
let(:payload_type) { 'callback_query' }
let(:payload) { stub_payload(:id, :from, :message, :inline_message_id, :data) }
it { should eq [false, payload_type, payload.values_at(:data)] }
end end
context 'when payload is message' do context 'when payload is message' do