Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Allow Link formatting via chat.postMessage #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions lib/lita/adapters/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ def run
rtm_connection.run
end

def send_messages(target, strings)
return unless rtm_connection
def send_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

rtm_connection.send_messages(channel_for(target), strings)
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)
Expand All @@ -45,6 +59,18 @@ def channel_for(target)
target.room
end
end

def send_string_messages(target, messages)
return unless rtm_connection

rtm_connection.send_messages(channel_for(target), messages)
end

def send_complex_messages(target, messages)
messages.each do |message|
API.new(config).post_message(channel_for(target), message)
end
end
end

# Register Slack adapter to Lita
Expand Down
18 changes: 18 additions & 0 deletions lib/lita/adapters/slack/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ def set_topic(channel, topic)
call_api("channels.setTopic", channel: channel, topic: topic)
end

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

end


call_api('chat.postMessage',
{ as_user: true,
channel: channel }.merge(message))
end

def rtm_start
response_data = call_api("rtm.start")

Expand Down