discourse-chat-integration/lib/discourse_chat/provider/telegram/telegram_command_controller.rb

108 lines
3.3 KiB
Ruby
Raw Normal View History

module DiscourseChat::Provider::TelegramProvider
class TelegramCommandController < DiscourseChat::Provider::HookController
requires_provider ::DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
before_action :telegram_token_valid?, only: :command
skip_before_action :check_xhr,
:preload_json,
:verify_authenticity_token,
:redirect_to_login_if_required,
only: :command
def command
# If it's a new message (telegram also sends hooks for other reasons that we don't care about)
if params.key?('message')
chat_id = params['message']['chat']['id']
message_text = process_command(params['message'])
message = {
chat_id: chat_id,
text: message_text,
parse_mode: "html",
disable_web_page_preview: true,
}
DiscourseChat::Provider::TelegramProvider.sendMessage(message)
2017-08-01 15:53:39 -04:00
elsif params.key?('channel_post') && 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
2017-08-01 15:53:39 -04:00
# Always give telegram a success message, otherwise we'll stop receiving webhooks
data = {
success: true
}
render json: data
end
def process_command(message)
chat_id = params['message']['chat']['id']
provider = DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
2017-08-01 15:53:39 -04:00
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('chat_id', chat_id).first
if channel.nil?
return I18n.t(
"chat_integration.provider.telegram.unknown_chat",
site_title: CGI::escapeHTML(SiteSetting.title),
chat_id: chat_id,
)
end
2017-08-01 15:53:39 -04:00
# If slash commands disabled, send a generic message
if !SiteSetting.chat_integration_telegram_enable_slash_commands
return I18n.t(
"chat_integration.provider.telegram.known_chat",
site_title: CGI::escapeHTML(SiteSetting.title),
chat_id: chat_id,
)
end
tokens = message['text'].split(" ")
2017-07-19 12:28:36 -04:00
tokens[0][0] = '' # Remove the slash from the first token
tokens[0] = tokens[0].split('@')[0] # Remove the bot name from the command (necessary for group chats)
2017-07-19 12:28:36 -04:00
return ::DiscourseChat::Helper.process_command(channel, tokens)
end
def telegram_token_valid?
params.require(:token)
if SiteSetting.chat_integration_telegram_secret.blank? ||
SiteSetting.chat_integration_telegram_secret != params[:token]
raise Discourse::InvalidAccess.new
end
end
end
class TelegramEngine < ::Rails::Engine
2017-08-01 15:53:39 -04:00
engine_name DiscourseChat::PLUGIN_NAME + "-telegram"
isolate_namespace DiscourseChat::Provider::TelegramProvider
end
TelegramEngine.routes.draw do
2017-08-01 15:53:39 -04:00
post "command/:token" => "telegram_command#command"
end
2017-08-01 15:53:39 -04:00
end