From fc4138c5d0e3f0ac6d93c56d584326342b5387c4 Mon Sep 17 00:00:00 2001 From: Jaison Erick Date: Sun, 23 Aug 2015 14:58:29 -0300 Subject: [PATCH 1/2] Support for Slack postMessages --- lib/lita/adapters/slack.rb | 21 +++++++++++++++++---- lib/lita/adapters/slack/api.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/lita/adapters/slack.rb b/lib/lita/adapters/slack.rb index 73af4b9..2b63058 100644 --- a/lib/lita/adapters/slack.rb +++ b/lib/lita/adapters/slack.rb @@ -15,10 +15,9 @@ def run rtm_connection.run end - def send_messages(target, strings) - return unless rtm_connection - - rtm_connection.send_messages(channel_for(target), strings) + def send_messages(target, messages) + send_string_messages(target, messages) + send_complex_messages(target, messages) end def set_topic(target, topic) @@ -45,6 +44,20 @@ def channel_for(target) target.room end end + + def send_string_messages(target, messages) + return unless rtm_connection + + strings = messages.select { |msg| !msg.respond_to?(:to_slack) } + rtm_connection.send_messages(channel_for(target), strings) + end + + def send_complex_messages(target, messages) + messages = messages.select { |msg| msg.respond_to?(:to_slack) } + messages.each do |message| + API.new(config).post_message(channel_for(target), message.to_slack) + end + end end # Register Slack adapter to Lita diff --git a/lib/lita/adapters/slack/api.rb b/lib/lita/adapters/slack/api.rb index b01b0b8..4182cfe 100644 --- a/lib/lita/adapters/slack/api.rb +++ b/lib/lita/adapters/slack/api.rb @@ -24,6 +24,18 @@ def set_topic(channel, topic) call_api("channels.setTopic", channel: channel, topic: topic) end + def post_message(channel, params) + raise "Slack message requires a text param" unless params[:text] + + if params[:attachments] + params[:attachments] = JSON.dump(params[:attachments]) + end + + call_api('chat.postMessage', + { as_user: true, + channel: channel }.merge(params)) + end + def rtm_start response_data = call_api("rtm.start") From 6901ebc46ffd3d81f0c726df04987254449904b0 Mon Sep 17 00:00:00 2001 From: Eric Richardson Date: Thu, 27 Aug 2015 15:47:57 -0400 Subject: [PATCH 2/2] Tweak how we decide what method should be used to send a message Use the API to send messages that a) aren't strings (typically an object with attachments, etc) or b) match `\<.*\>` (link formatting) --- lib/lita/adapters/slack.rb | 25 +++++++++++++++++++------ lib/lita/adapters/slack/api.rb | 16 +++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/lita/adapters/slack.rb b/lib/lita/adapters/slack.rb index 2b63058..5c8fd58 100644 --- a/lib/lita/adapters/slack.rb +++ b/lib/lita/adapters/slack.rb @@ -16,8 +16,23 @@ def run end def send_messages(target, messages) - send_string_messages(target, messages) - send_complex_messages(target, messages) + # split messages between RTM and API + # RTM gets strings that don't match <> + # API gets everything else + + string_messages = [] + complex_messages = [] + + messages.each do |m| + if m.is_a?(String) && !m.match(/\<.*\>/) + string_messages << m + else + complex_messages << m + end + end + + send_string_messages(target,string_messages) if string_messages.any? + send_complex_messages(target,complex_messages) if complex_messages.any? end def set_topic(target, topic) @@ -48,14 +63,12 @@ def channel_for(target) def send_string_messages(target, messages) return unless rtm_connection - strings = messages.select { |msg| !msg.respond_to?(:to_slack) } - rtm_connection.send_messages(channel_for(target), strings) + rtm_connection.send_messages(channel_for(target), messages) end def send_complex_messages(target, messages) - messages = messages.select { |msg| msg.respond_to?(:to_slack) } messages.each do |message| - API.new(config).post_message(channel_for(target), message.to_slack) + API.new(config).post_message(channel_for(target), message) end end end diff --git a/lib/lita/adapters/slack/api.rb b/lib/lita/adapters/slack/api.rb index 4182cfe..9fafd8d 100644 --- a/lib/lita/adapters/slack/api.rb +++ b/lib/lita/adapters/slack/api.rb @@ -24,16 +24,22 @@ def set_topic(channel, topic) call_api("channels.setTopic", channel: channel, topic: topic) end - def post_message(channel, params) - raise "Slack message requires a text param" unless params[:text] + def post_message(channel, message) + if message.is_a?(String) + message = {text:message} + else + raise "Slack message requires a text param" unless message[:text] + + if params[:attachments] + params[:attachments] = JSON.dump(message[:attachments]) + end - if params[:attachments] - params[:attachments] = JSON.dump(params[:attachments]) end + call_api('chat.postMessage', { as_user: true, - channel: channel }.merge(params)) + channel: channel }.merge(message)) end def rtm_start