FIX: Ensure telegram bot only responds to slash-commands (#94)

Co-authored-by: Froggy C <me@froggyc.eu>
This commit is contained in:
Aljaž Srebrnič 2021-12-01 23:19:50 +01:00 committed by GitHub
parent 369ca14711
commit ae0389ca89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 11 deletions

View File

@ -20,14 +20,16 @@ module DiscourseChatIntegration::Provider::TelegramProvider
message_text = process_command(params['message']) message_text = process_command(params['message'])
message = { if message_text.present?
chat_id: chat_id, message = {
text: message_text, chat_id: chat_id,
parse_mode: "html", text: message_text,
disable_web_page_preview: true, parse_mode: "html",
} disable_web_page_preview: true,
}
DiscourseChatIntegration::Provider::TelegramProvider.sendMessage(message) DiscourseChatIntegration::Provider::TelegramProvider.sendMessage(message)
end
elsif params.dig('channel_post', 'text')&.include?('/getchatid') elsif params.dig('channel_post', 'text')&.include?('/getchatid')
chat_id = params['channel_post']['chat']['id'] chat_id = params['channel_post']['chat']['id']
@ -62,10 +64,15 @@ module DiscourseChatIntegration::Provider::TelegramProvider
channel = DiscourseChatIntegration::Channel.with_provider(provider).with_data_value('chat_id', chat_id).first channel = DiscourseChatIntegration::Channel.with_provider(provider).with_data_value('chat_id', chat_id).first
text_key = "unknown_chat" if channel.nil? text_key = if channel.nil?
# If slash commands disabled, send a generic message "unknown_chat"
text_key = "known_chat" if !SiteSetting.chat_integration_telegram_enable_slash_commands elsif !SiteSetting.chat_integration_telegram_enable_slash_commands || !message['text'].start_with?('/')
text_key = "help" if message['text'].blank? "silent"
else
""
end
return "" if text_key == "silent"
if text_key.present? if text_key.present?
return I18n.t( return I18n.t(