2017-07-05 15:03:02 +01:00
|
|
|
module DiscourseChat::Provider::SlackProvider
|
|
|
|
class SlackCommandController < DiscourseChat::Provider::HookController
|
|
|
|
requires_provider ::DiscourseChat::Provider::SlackProvider::PROVIDER_NAME
|
|
|
|
|
2017-07-05 23:01:46 +01:00
|
|
|
before_filter :slack_token_valid?, only: :command
|
2017-07-05 15:03:02 +01:00
|
|
|
|
2017-07-05 23:01:46 +01:00
|
|
|
skip_before_filter :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:verify_authenticity_token,
|
|
|
|
:redirect_to_login_if_required,
|
|
|
|
only: :command
|
|
|
|
|
|
|
|
def command
|
|
|
|
text = process_command(params)
|
|
|
|
|
|
|
|
render json: { text: text }
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_command(params)
|
|
|
|
|
|
|
|
tokens = params[:text].split(" ")
|
|
|
|
|
|
|
|
# channel name fix
|
2017-07-13 23:21:15 +01:00
|
|
|
channel_id =
|
2017-07-05 23:01:46 +01:00
|
|
|
case params[:channel_name]
|
|
|
|
when 'directmessage'
|
|
|
|
"@#{params[:user_name]}"
|
|
|
|
when 'privategroup'
|
|
|
|
params[:channel_id]
|
|
|
|
else
|
|
|
|
"##{params[:channel_name]}"
|
|
|
|
end
|
|
|
|
|
2017-07-06 21:42:37 +01:00
|
|
|
provider = DiscourseChat::Provider::SlackProvider::PROVIDER_NAME
|
|
|
|
|
2017-07-13 23:21:15 +01:00
|
|
|
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('identifier',channel_id).first
|
|
|
|
|
|
|
|
# Create channel if doesn't exist
|
|
|
|
channel ||= DiscourseChat::Channel.create!(provider:provider, data:{identifier: channel_id})
|
|
|
|
|
2017-07-19 16:57:33 +01:00
|
|
|
return ::DiscourseChat::Helper.process_command(channel, tokens)
|
2017-07-05 23:01:46 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def slack_token_valid?
|
|
|
|
params.require(:token)
|
|
|
|
|
|
|
|
if SiteSetting.chat_integration_slack_incoming_webhook_token.blank? ||
|
|
|
|
SiteSetting.chat_integration_slack_incoming_webhook_token != params[:token]
|
|
|
|
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
2017-07-05 15:03:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SlackEngine < ::Rails::Engine
|
|
|
|
engine_name DiscourseChat::PLUGIN_NAME+"-slack"
|
|
|
|
isolate_namespace DiscourseChat::Provider::SlackProvider
|
|
|
|
end
|
|
|
|
|
|
|
|
SlackEngine.routes.draw do
|
2017-07-05 23:01:46 +01:00
|
|
|
post "command" => "slack_command#command"
|
2017-07-05 15:03:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|