From 453e0ffcc0649c60cc6fe53b7137e5c88b8a5f6a Mon Sep 17 00:00:00 2001 From: Peter Zignego Date: Wed, 14 Jun 2017 11:18:56 -0400 Subject: [PATCH] Send threaded messages --- Sources/SKRTMAPI/SKRTMAPI.swift | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Sources/SKRTMAPI/SKRTMAPI.swift b/Sources/SKRTMAPI/SKRTMAPI.swift index d96bd26..6fbbc8d 100644 --- a/Sources/SKRTMAPI/SKRTMAPI.swift +++ b/Sources/SKRTMAPI/SKRTMAPI.swift @@ -94,25 +94,38 @@ public final class SKRTMAPI: RTMDelegate { rtm.disconnect() } - public func sendMessage(_ message: String, channelID: String, threadTs: String? = nil) throws { + public func sendMessage(_ message: String, channelID: String) throws { guard connected else { throw SlackError.rtmConnectionError } do { - let string = try format(message: message, channel: channelID, threadTs: threadTs) + let string = try format(message: message, channel: channelID) try rtm.sendMessage(string) } catch let error { throw error } } - private func format(message: String, channel: String, threadTs: String?) throws -> String { + public func sendThreadedMessage(_ message: String, channelID: String, threadTs: String, broadcastReply: Bool = false) throws { + guard connected else { + throw SlackError.rtmConnectionError + } + do { + let string = try format(message: message, channel: channelID, threadTs: threadTs, broadcastReply: broadcastReply) + try rtm.sendMessage(string) + } catch let error { + throw error + } + } + + private func format(message: String, channel: String, threadTs: String? = nil, broadcastReply: Bool = false) throws -> String { let json: [String: Any?] = [ "id": Date().slackTimestamp, "type": "message", "channel": channel, "text": message.slackFormatEscaping, - "thread_ts": threadTs + "thread_ts": threadTs, + "broadcastReply": broadcastReply ] guard let data = try? JSONSerialization.data(withJSONObject: filterNilParameters(json), options: []),