2017-07-05 10:03:02 -04:00
|
|
|
module DiscourseChat::Provider::TelegramProvider
|
|
|
|
class TelegramCommandController < DiscourseChat::Provider::HookController
|
|
|
|
requires_provider ::DiscourseChat::Provider::TelegramProvider::PROVIDER_NAME
|
|
|
|
|
2017-09-03 22:44:32 -04:00
|
|
|
before_action :telegram_token_valid?, only: :command
|
2017-07-05 10:03:02 -04:00
|
|
|
|
2017-09-03 22:44:32 -04:00
|
|
|
skip_before_action :check_xhr,
|
2017-07-19 11:28:02 -04:00
|
|
|
: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')
|
2017-07-21 14:12:31 -04:00
|
|
|
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)
|
2017-07-19 11:28:02 -04:00
|
|
|
end
|
|
|
|
|
2017-08-01 15:53:39 -04:00
|
|
|
# Always give telegram a success message, otherwise we'll stop receiving webhooks
|
2017-07-19 11:28:02 -04:00
|
|
|
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
|
2017-07-19 11:28:02 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-19 11:28:02 -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
|
2017-07-21 12:55:33 -04:00
|
|
|
tokens[0] = tokens[0].split('@')[0] # Remove the bot name from the command (necessary for group chats)
|
2017-07-19 11:28:02 -04:00
|
|
|
|
2017-07-19 12:28:36 -04:00
|
|
|
return ::DiscourseChat::Helper.process_command(channel, tokens)
|
2017-07-19 11:28:02 -04:00
|
|
|
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
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TelegramEngine < ::Rails::Engine
|
2017-08-01 15:53:39 -04:00
|
|
|
engine_name DiscourseChat::PLUGIN_NAME + "-telegram"
|
2017-07-05 10:03:02 -04:00
|
|
|
isolate_namespace DiscourseChat::Provider::TelegramProvider
|
|
|
|
end
|
|
|
|
|
|
|
|
TelegramEngine.routes.draw do
|
2017-08-01 15:53:39 -04:00
|
|
|
post "command/:token" => "telegram_command#command"
|
2017-07-05 10:03:02 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|