1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-04 10:15:50 +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.
- 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

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

@@ -17,7 +17,7 @@ Package contains:
- Poller with automatic source-reloader for development env.
- Rake tasks to update webhook urls.
Here is sample [telegram_bot_app](https://github.com/telegram-bot-rb/telegram_bot_app).
Here is sample [telegram_bot_app](https://github.com/telegram-bot-rb/telegram_bot_app).
Run it on your local machine in 1 minute!
## Installation
@@ -102,7 +102,8 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
# use callbacks like in any other controllers
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.
def message(message)
# message can be also accessed via instance method
@@ -110,6 +111,13 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
# store_message(message['text'])
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.
# Command arguments will be parsed and passed to the method.
# Be sure to use splat args and default values to not get errors when

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

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

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

@@ -80,11 +80,26 @@ RSpec.describe Telegram::Bot::UpdatesController do
describe '#action_for_payload' do
subject { controller.action_for_payload }
(described_class::PAYLOAD_TYPES - %w(message)).each do |type|
context "when payload is #{type}" do
let(:payload_type) { type }
it { should eq [false, type, [payload]] }
end
def stub_payload(*fields)
Hash[fields.map { |x| [x, double(x)] }]
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
context 'when payload is message' do