From 89943286549ede547fbcb971e3dc754b85ef7f35 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Tue, 17 May 2016 10:49:47 +0300 Subject: [PATCH] Basic actions are called with most useful params instead of payload --- CHANGELOG.md | 3 +++ README.md | 12 ++++++++-- lib/telegram/bot/updates_controller.rb | 23 +++++++++++------- spec/telegram/bot/updates_controller_spec.rb | 25 ++++++++++++++++---- 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 194188f..936a6fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 8673137..b90138d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index 99ea2fd..8bf5a4c 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -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. diff --git a/spec/telegram/bot/updates_controller_spec.rb b/spec/telegram/bot/updates_controller_spec.rb index e8202e8..f1269ec 100644 --- a/spec/telegram/bot/updates_controller_spec.rb +++ b/spec/telegram/bot/updates_controller_spec.rb @@ -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