From 6a5e642470d41214719d05aa8889c2f57f913627 Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Sat, 19 May 2018 12:51:05 +0300 Subject: [PATCH] Fix #chat for non-message updates with TypedUpdates enabled Fixes #82 --- CHANGELOG.md | 1 + lib/telegram/bot/updates_controller.rb | 9 ++++- .../updates_controller/typed_update_spec.rb | 35 +++++++++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c89280b..3d61f30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Extracted typed response mappings to telegram-bot-types gem. It now provides definitions for all API v3.5 methods. +- Fix #chat for non-message updates with TypedUpdates enabled # 0.13.0 diff --git a/lib/telegram/bot/updates_controller.rb b/lib/telegram/bot/updates_controller.rb index 453d6b8..d2c749a 100644 --- a/lib/telegram/bot/updates_controller.rb +++ b/lib/telegram/bot/updates_controller.rb @@ -157,7 +157,14 @@ module Telegram # # Can be overriden with `chat` option for #initialize. def chat - @_chat ||= payload.try! { |x| x['chat'] || x['message'] && x['message']['chat'] } + @_chat ||= + if payload + if payload.is_a?(Hash) + payload['chat'] || payload['message'] && payload['message']['chat'] + else + payload.try(:chat) || payload.try(:message).try!(:chat) + end + end end # Accessor to `'from'` field of payload. Can be overriden with `from` option diff --git a/spec/telegram/bot/updates_controller/typed_update_spec.rb b/spec/telegram/bot/updates_controller/typed_update_spec.rb index 70e3a40..878f37c 100644 --- a/spec/telegram/bot/updates_controller/typed_update_spec.rb +++ b/spec/telegram/bot/updates_controller/typed_update_spec.rb @@ -9,17 +9,40 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do context 'when `update` is a virtus model' do subject { controller } - %w[ - message - inline_query - chosen_inline_result - ].each do |type| + unique_types = Telegram::Bot::UpdatesController::PAYLOAD_TYPES - %w[ + edited_message + channel_post + edited_channel_post + ] + unique_types.each do |type| context "with #{type}" do type_class = Telegram::Bot::Types.const_get(type.camelize) let(:payload_type) { type } - let(:payload) { {} } + let(:payload) do + {}.tap do |result| + result[:chat] = chat if type_class.instance_methods.include?(:chat) + result[:from] = from if type_class.instance_methods.include?(:from) + end + end + let(:chat) { {id: 'chat_id'} } + let(:from) { {id: 'from_id'} } its(:payload_type) { should eq payload_type } its(:payload) { should be_instance_of type_class } + + if type_class.instance_methods.include?(:chat) + # Virtus does not support ==. :( + its(:chat) { should be_instance_of Telegram::Bot::Types::Chat } + its('chat.to_hash') { should include chat } + else + its(:chat) { should eq nil } + end + + if type_class.instance_methods.include?(:from) + its(:from) { should be_instance_of Telegram::Bot::Types::User } + its('from.to_hash') { should include from } + else + its(:from) { should eq nil } + end end end end