diff --git a/config/settings.yml b/config/settings.yml index 35f8966..350c56a 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -39,6 +39,7 @@ plugins: chat_integration_telegram_enable_slash_commands: default: true chat_integration_telegram_secret: + default: '' hidden: true ####################################### diff --git a/lib/discourse_chat/provider/slack/slack_command_controller.rb b/lib/discourse_chat/provider/slack/slack_command_controller.rb index 2b60478..479b063 100644 --- a/lib/discourse_chat/provider/slack/slack_command_controller.rb +++ b/lib/discourse_chat/provider/slack/slack_command_controller.rb @@ -42,7 +42,7 @@ module DiscourseChat::Provider::SlackProvider return process_post_request(channel, tokens, params[:channel_id]) end - return { text: ::DiscourseChat::Helper.process_command(channel, tokens)} + return { text: ::DiscourseChat::Helper.process_command(channel, tokens) } end @@ -51,56 +51,21 @@ module DiscourseChat::Provider::SlackProvider 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") + return { text: I18n.t("chat_integration.provider.slack.parse_error") } end end - error_text = I18n.t("chat_integration.provider.slack.transcript_error") + transcript = SlackTranscript.load_transcript(slack_channel_id, messages_to_load) - # 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')) - req.set_form_data(token: SiteSetting.chat_integration_slack_access_token) - response = http.request(req) - return error_text unless response.kind_of? Net::HTTPSuccess - json = JSON.parse(response.body) - return error_text unless json['ok'] - raw_users = json + return { text: I18n.t("chat_integration.provider.slack.transcript_error") } unless transcript - # 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'] - raw_history = json - - transcript = SlackTranscript.new(raw_history, raw_users, slack_channel_id) - - post_content = transcript.build_transcript - - secret = DiscourseChat::Helper.save_transcript(post_content) - - link = "#{Discourse.base_url}/chat-transcript/#{secret}" - - return { text: "<#{link}|#{I18n.t("chat_integration.provider.slack.post_to_discourse")}>", - } + return transcript.build_slack_ui end diff --git a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb b/lib/discourse_chat/provider/slack/slack_transcript_helper.rb index 9cc1b59..4f5c439 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript_helper.rb @@ -122,6 +122,54 @@ module DiscourseChat::Provider::SlackProvider return post_content end + def build_slack_ui + post_content = build_transcript + secret = DiscourseChat::Helper.save_transcript(post_content) + link = "#{Discourse.base_url}/chat-transcript/#{secret}" + + return { text: "<#{link}|#{I18n.t("chat_integration.provider.slack.post_to_discourse")}>", + } + end + + def self.load_user_data + http = Net::HTTP.new("slack.com", 443) + http.use_ssl = true + + req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list')) + req.set_form_data(token: SiteSetting.chat_integration_slack_access_token) + response = http.request(req) + return false unless response.kind_of? Net::HTTPSuccess + json = JSON.parse(response.body) + return false unless json['ok'] + return json + end + + def self.load_chat_history(slack_channel_id, messages_to_load) + http = Net::HTTP.new("slack.com", 443) + http.use_ssl = true + + 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 false unless response.kind_of? Net::HTTPSuccess + json = JSON.parse(response.body) + return false unless json['ok'] + return json + end + + def self.load_transcript(slack_channel_id, messages_to_load) + return false unless raw_users = self.load_user_data + return false unless raw_history = self.load_chat_history(slack_channel_id, messages_to_load) + + self.new(raw_history, raw_users, slack_channel_id) + end end end