mirror of
https://github.com/discourse/discourse-chat-integration.git
synced 2025-07-05 13:42:09 +00:00
Allow sending by API instead of webhooks for slack
This commit is contained in:
parent
3f0462d40c
commit
0074f90f14
@ -17,6 +17,8 @@ plugins:
|
|||||||
default: 400
|
default: 400
|
||||||
chat_slack_icon_url:
|
chat_slack_icon_url:
|
||||||
default: ''
|
default: ''
|
||||||
|
chat_slack_access_token:
|
||||||
|
default: ''
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
######### TELEGRAM SETTINGS ###########
|
######### TELEGRAM SETTINGS ###########
|
||||||
|
@ -57,15 +57,57 @@ module DiscourseChat::Provider::SlackProvider
|
|||||||
message
|
message
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.trigger_notification(post, channel)
|
def self.send_via_api(message)
|
||||||
message = slack_message(post, channel)
|
http = Net::HTTP.new("slack.com", 443)
|
||||||
|
|
||||||
http = Net::HTTP.new("hooks.slack.com", 443)
|
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
|
|
||||||
|
response = nil
|
||||||
|
uri = ""
|
||||||
|
record = DiscourseChat.pstore_get("slack_topic_#{post.topic.id}_#{channel}")
|
||||||
|
|
||||||
|
if (record.present? && ((Time.now.to_i - record[:ts].split('.')[0].to_i)/ 60) < 5 && record[:message][:attachments].length < 5)
|
||||||
|
attachments = record[:message][:attachments]
|
||||||
|
attachments.concat message[:attachments]
|
||||||
|
|
||||||
|
uri = URI("https://slack.com/api/chat.update" +
|
||||||
|
"?token=#{SiteSetting.chat_slack_access_token}" +
|
||||||
|
"&username=#{CGI::escape(record[:message][:username])}" +
|
||||||
|
"&text=#{CGI::escape(record[:message][:text])}" +
|
||||||
|
"&channel=#{record[:channel]}" +
|
||||||
|
"&attachments=#{CGI::escape(attachments.to_json)}" +
|
||||||
|
"&ts=#{record[:ts]}"
|
||||||
|
)
|
||||||
|
else
|
||||||
|
uri = URI("https://slack.com/api/chat.postMessage" +
|
||||||
|
"?token=#{SiteSetting.chat_slack_access_token}" +
|
||||||
|
"&username=#{CGI::escape(message[:username])}" +
|
||||||
|
"&icon_url=#{CGI::escape(message[:icon_url])}" +
|
||||||
|
"&channel=#{ message[:channel].gsub('#', '') }" +
|
||||||
|
"&attachments=#{CGI::escape(message[:attachments].to_json)}"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
response = http.request(Net::HTTP::Post.new(uri))
|
||||||
|
|
||||||
|
DiscourseChat.pstore_set("slack_topic_#{post.topic.id}_#{channel}", JSON.parse(response.body) )
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.send_via_webhook(message)
|
||||||
|
http = Net::HTTP.new("hooks.slack.com", 443)
|
||||||
|
http.use_ssl = true
|
||||||
req = Net::HTTP::Post.new(URI(SiteSetting.slack_outbound_webhook_url), 'Content-Type' =>'application/json')
|
req = Net::HTTP::Post.new(URI(SiteSetting.slack_outbound_webhook_url), 'Content-Type' =>'application/json')
|
||||||
req.body = message.to_json
|
req.body = message.to_json
|
||||||
response = http.request(req)
|
response = http.request(req)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.trigger_notification(post, channel)
|
||||||
|
message = slack_message(post, channel)
|
||||||
|
|
||||||
|
if SiteSetting.chat_slack_access_token.empty?
|
||||||
|
self.send_via_webhook(message)
|
||||||
|
else
|
||||||
|
self.send_via_api(message)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user