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