More modularisation for slack transcript generation

This commit is contained in:
David Taylor 2017-08-03 17:24:49 +01:00
parent 4d811ed83e
commit cfaef26e5d
3 changed files with 54 additions and 40 deletions

View File

@ -39,6 +39,7 @@ plugins:
chat_integration_telegram_enable_slash_commands: chat_integration_telegram_enable_slash_commands:
default: true default: true
chat_integration_telegram_secret: chat_integration_telegram_secret:
default: ''
hidden: true hidden: true
####################################### #######################################

View File

@ -42,7 +42,7 @@ module DiscourseChat::Provider::SlackProvider
return process_post_request(channel, tokens, params[:channel_id]) return process_post_request(channel, tokens, params[:channel_id])
end end
return { text: ::DiscourseChat::Helper.process_command(channel, tokens)} return { text: ::DiscourseChat::Helper.process_command(channel, tokens) }
end end
@ -51,56 +51,21 @@ module DiscourseChat::Provider::SlackProvider
return I18n.t("chat_integration.provider.slack.api_required") return I18n.t("chat_integration.provider.slack.api_required")
end end
http = Net::HTTP.new("slack.com", 443)
http.use_ssl = true
messages_to_load = 10 messages_to_load = 10
if tokens.size > 1 if tokens.size > 1
begin begin
messages_to_load = Integer(tokens[1], 10) messages_to_load = Integer(tokens[1], 10)
rescue ArgumentError rescue ArgumentError
return I18n.t("chat_integration.provider.slack.parse_error") return { text: I18n.t("chat_integration.provider.slack.parse_error") }
end end
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) return { text: I18n.t("chat_integration.provider.slack.transcript_error") } unless transcript
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
# Now load the chat message history return transcript.build_slack_ui
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")}>",
}
end end

View File

@ -122,6 +122,54 @@ module DiscourseChat::Provider::SlackProvider
return post_content return post_content
end 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
end end