From 0d9b251030a2ffcfb9237c23367b67eebd623682 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 21 Jul 2017 19:12:31 +0100 Subject: [PATCH] =?UTF-8?q?Allow=20telegram=20to=20send=20notifications=20?= =?UTF-8?q?to=20=E2=80=98broadcast=20channels=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../telegram/telegram_command_controller.rb | 17 +++++++++++++++++ .../telegram_command_controller_spec.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/discourse_chat/provider/telegram/telegram_command_controller.rb b/lib/discourse_chat/provider/telegram/telegram_command_controller.rb index bc6cb33..f2a3982 100644 --- a/lib/discourse_chat/provider/telegram/telegram_command_controller.rb +++ b/lib/discourse_chat/provider/telegram/telegram_command_controller.rb @@ -27,6 +27,23 @@ module DiscourseChat::Provider::TelegramProvider DiscourseChat::Provider::TelegramProvider.sendMessage(message) + elsif params.key?('channel_post') and params['channel_post']['text'].include? '/getchatid' + chat_id = params['channel_post']['chat']['id'] + + message_text = I18n.t( + "chat_integration.provider.telegram.unknown_chat", + site_title: CGI::escapeHTML(SiteSetting.title), + chat_id: chat_id, + ) + + message = { + chat_id: chat_id, + text: message_text, + parse_mode: "html", + disable_web_page_preview: true, + } + + DiscourseChat::Provider::TelegramProvider.sendMessage(message) end # Always give telegram a success message, otherwise we'll stop receiving webhooks diff --git a/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb b/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb index 414836f..0ab263f 100644 --- a/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/telegram/telegram_command_controller_spec.rb @@ -101,6 +101,18 @@ describe 'Telegram Command Controller', type: :request do end end end + + it "should respond only to a specific command in a broadcast channel" do + post '/chat-integration/telegram/command/shhh.json', channel_post: {chat: {id:123}, text: "something" } + + expect(response.status).to eq(200) + expect(stub).to have_been_requested.times(0) + + post '/chat-integration/telegram/command/shhh.json', channel_post: {chat: {id:123}, text: "/getchatid" } + + expect(response.status).to eq(200) + expect(stub).to have_been_requested.times(1) + end end end end \ No newline at end of file