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
|
2017-08-03 15:48:33 +01:00
|
|
|
message = process_command(params)
|
2017-07-05 23:01:46 +01:00
|
|
|
|
2017-08-03 15:48:33 +01:00
|
|
|
render json: message
|
2017-07-05 23:01:46 +01:00
|
|
|
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-08-01 20:53:39 +01:00
|
|
|
channel = DiscourseChat::Channel.with_provider(provider).with_data_value('identifier', channel_id).first
|
2017-07-13 23:21:15 +01:00
|
|
|
|
|
|
|
# Create channel if doesn't exist
|
2017-08-01 20:53:39 +01:00
|
|
|
channel ||= DiscourseChat::Channel.create!(provider: provider, data: { identifier: channel_id })
|
2017-07-13 23:21:15 +01:00
|
|
|
|
2017-07-24 16:43:37 +01:00
|
|
|
if tokens[0] == 'post'
|
|
|
|
return process_post_request(channel, tokens, params[:channel_id])
|
|
|
|
end
|
|
|
|
|
2017-08-03 15:48:33 +01:00
|
|
|
return { text: ::DiscourseChat::Helper.process_command(channel, tokens)}
|
2017-08-01 20:53:39 +01:00
|
|
|
|
2017-07-05 23:01:46 +01:00
|
|
|
end
|
|
|
|
|
2017-07-24 16:43:37 +01:00
|
|
|
def process_post_request(channel, tokens, slack_channel_id)
|
|
|
|
if SiteSetting.chat_integration_slack_access_token.empty?
|
|
|
|
return I18n.t("chat_integration.provider.slack.api_required")
|
|
|
|
end
|
|
|
|
|
|
|
|
http = Net::HTTP.new("slack.com", 443)
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
messages_to_load = 10
|
|
|
|
|
|
|
|
if tokens.size > 1
|
|
|
|
begin
|
|
|
|
messages_to_load = Integer(tokens[1], 10)
|
|
|
|
rescue ArgumentError
|
|
|
|
return I18n.t("chat_integration.provider.slack.parse_error")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
error_text = I18n.t("chat_integration.provider.slack.transcript_error")
|
|
|
|
|
|
|
|
# Load the user data (we need this to change user IDs into usernames)
|
|
|
|
req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list'))
|
2017-08-01 20:53:39 +01:00
|
|
|
req.set_form_data(token: SiteSetting.chat_integration_slack_access_token)
|
2017-07-24 16:43:37 +01:00
|
|
|
response = http.request(req)
|
|
|
|
return error_text unless response.kind_of? Net::HTTPSuccess
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
return error_text unless json['ok']
|
2017-08-03 15:48:33 +01:00
|
|
|
raw_users = json
|
2017-07-24 16:43:37 +01:00
|
|
|
|
|
|
|
# Now load the chat message history
|
|
|
|
req = Net::HTTP::Post.new(URI('https://slack.com/api/channels.history'))
|
|
|
|
|
|
|
|
data = {
|
|
|
|
token: SiteSetting.chat_integration_slack_access_token,
|
|
|
|
channel: slack_channel_id,
|
|
|
|
count: messages_to_load
|
|
|
|
}
|
|
|
|
|
|
|
|
req.set_form_data(data)
|
|
|
|
response = http.request(req)
|
|
|
|
return error_text unless response.kind_of? Net::HTTPSuccess
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
return error_text unless json['ok']
|
2017-08-03 15:48:33 +01:00
|
|
|
raw_history = json
|
2017-07-26 19:51:44 +01:00
|
|
|
|
2017-08-03 15:48:33 +01:00
|
|
|
transcript = SlackTranscript.new(raw_history, raw_users, slack_channel_id)
|
2017-07-26 19:51:44 +01:00
|
|
|
|
2017-08-03 15:48:33 +01:00
|
|
|
post_content = transcript.build_transcript
|
2017-07-24 16:43:37 +01:00
|
|
|
|
|
|
|
secret = DiscourseChat::Helper.save_transcript(post_content)
|
|
|
|
|
|
|
|
link = "#{Discourse.base_url}/chat-transcript/#{secret}"
|
|
|
|
|
2017-08-03 15:48:33 +01:00
|
|
|
return { text: "<#{link}|#{I18n.t("chat_integration.provider.slack.post_to_discourse")}>",
|
|
|
|
}
|
2017-07-24 16:43:37 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-05 23:01:46 +01:00
|
|
|
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
|
2017-08-01 20:53:39 +01:00
|
|
|
engine_name DiscourseChat::PLUGIN_NAME + "-slack"
|
2017-07-05 15:03:02 +01:00
|
|
|
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
|