From c68fde5d2bbb92cad24a35ff61586453d67264f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Mon, 18 Jul 2022 17:44:26 +0200 Subject: [PATCH] =?UTF-8?q?FIX:=20Don=E2=80=99t=20process=20commands=20whe?= =?UTF-8?q?n=20'text'=20is=20missing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch concerns the Telegram integration. Currently, we always try to process commands when we receive a hook from Telegram. To do so we rely on the `text` parameters from a Telegram message but the API documentation tells us this parameters is actually optional. It means sometimes it’s not present in the payload we receive but we still try to access it resulting in a crash. This patch addresses the issue by simply returning early from the `#process_command` method when `text` is missing from the payload since we don’t have anything to process then. --- .../provider/telegram/telegram_command_controller.rb | 2 ++ .../telegram/telegram_command_controller_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/discourse_chat_integration/provider/telegram/telegram_command_controller.rb b/lib/discourse_chat_integration/provider/telegram/telegram_command_controller.rb index b5d1287..fe01d9a 100644 --- a/lib/discourse_chat_integration/provider/telegram/telegram_command_controller.rb +++ b/lib/discourse_chat_integration/provider/telegram/telegram_command_controller.rb @@ -58,6 +58,8 @@ module DiscourseChatIntegration::Provider::TelegramProvider end def process_command(message) + return unless message['text'] # No command to be processed + chat_id = params['message']['chat']['id'] provider = DiscourseChatIntegration::Provider::TelegramProvider::PROVIDER_NAME diff --git a/spec/lib/discourse_chat_integration/provider/telegram/telegram_command_controller_spec.rb b/spec/lib/discourse_chat_integration/provider/telegram/telegram_command_controller_spec.rb index e4124df..fe0cbfb 100644 --- a/spec/lib/discourse_chat_integration/provider/telegram/telegram_command_controller_spec.rb +++ b/spec/lib/discourse_chat_integration/provider/telegram/telegram_command_controller_spec.rb @@ -135,6 +135,18 @@ describe 'Telegram Command Controller', type: :request do expect(response.status).to eq(200) expect(stub).to have_been_requested.times(1) end + + context "when 'text' is missing" do + it "does not break" do + post '/chat-integration/telegram/command/shhh.json', params: { + message: { chat: { id: 123 } } + } + + expect(response).to have_http_status :ok + expect(DiscourseChatIntegration::Rule.count).to eq(0) + expect(DiscourseChatIntegration::Channel.count).to eq(1) + end + end end end end