| 1 | package slack |
| 2 | |
| 3 | // OutgoingMessage is used for the realtime API, and seems incomplete. |
| 4 | type OutgoingMessage struct { |
| 5 | ID int `json:"id"` |
| 6 | // channel ID |
| 7 | Channel string `json:"channel,omitempty"` |
| 8 | Text string `json:"text,omitempty"` |
| 9 | Type string `json:"type,omitempty"` |
| 10 | ThreadTimestamp string `json:"thread_ts,omitempty"` |
| 11 | ThreadBroadcast bool `json:"reply_broadcast,omitempty"` |
| 12 | IDs []string `json:"ids,omitempty"` |
| 13 | } |
| 14 | |
| 15 | // Message is an auxiliary type to allow us to have a message containing sub messages |
| 16 | type Message struct { |
| 17 | Msg |
| 18 | SubMessage *Msg `json:"message,omitempty"` |
| 19 | PreviousMessage *Msg `json:"previous_message,omitempty"` |
| 20 | } |
| 21 | |
| 22 | // Msg SubTypes (https://api.slack.com/events/message) |
| 23 | const ( |
| 24 | MsgSubTypeBotMessage = "bot_message" // [Events API, RTM] A message was posted by an integration |
| 25 | MsgSubTypeMeMessage = "me_message" // [Events API, RTM] A /me message was sent |
| 26 | MsgSubTypeMessageChanged = "message_changed" // [Events API, RTM] A message was changed |
| 27 | MsgSubTypeMessageDeleted = "message_deleted" // [Events API, RTM] A message was deleted |
| 28 | MsgSubTypeMessageReplied = "message_replied" // [Events API, RTM] A message thread received a reply |
| 29 | MsgSubTypeReplyBroadcast = "reply_broadcast" // @Deprecated (No longer served) A message thread's reply was broadcast to a channel |
| 30 | MsgSubTypeThreadBroadcast = "thread_broadcast" // [Events API, RTM] A message thread's reply was broadcast to a channel |
| 31 | MsgSubTypeChannelJoin = "channel_join" // [Events API, RTM] A member joined a channel |
| 32 | MsgSubTypeChannelLeave = "channel_leave" // [Events API, RTM] A member left a channel |
| 33 | MsgSubTypeChannelTopic = "channel_topic" // [Events API, RTM] A channel topic was updated |
| 34 | MsgSubTypeChannelPurpose = "channel_purpose" // [Events API, RTM] A channel purpose was updated |
| 35 | MsgSubTypeChannelName = "channel_name" // [Events API, RTM] A channel was renamed |
| 36 | MsgSubTypeChannelArchive = "channel_archive" // [Events API, RTM] A channel was archived |
| 37 | MsgSubTypeChannelUnarchive = "channel_unarchive" // [Events API, RTM] A channel was unarchived |
| 38 | MsgSubTypeGroupJoin = "group_join" // [RTM] A member joined a group |
| 39 | MsgSubTypeGroupLeave = "group_leave" // [RTM] A member left a group |
| 40 | MsgSubTypeGroupTopic = "group_topic" // [RTM] A group topic was updated |
| 41 | MsgSubTypeGroupPurpose = "group_purpose" // [RTM] A group purpose was updated |
| 42 | MsgSubTypeGroupName = "group_name" // [RTM] A group was renamed |
| 43 | MsgSubTypeGroupArchive = "group_archive" // [RTM] A group was archived |
| 44 | MsgSubTypeGroupUnarchive = "group_unarchive" // [RTM] A group was unarchived |
| 45 | MsgSubTypeFileShare = "file_share" // [Events API, RTM] A file was shared into a channel |
| 46 | MsgSubTypeFileComment = "file_comment" // [RTM] A comment was added to a file |
| 47 | MsgSubTypeGileMention = "file_mention" // [RTM] A file was mentioned in a channel |
| 48 | MsgSubTypePinnedItem = "pinned_item" // [RTM] An item was pinned in a channel |
| 49 | MsgSubTypeUnpinnedItem = "unpinned_item" // [RTM] An item was unpinned from a channel |
| 50 | MsgSubTypeEkmAccessDenied = "ekm_access_denied" // [Events API, RTM] Message content redacted due to Enterprise Key Management (EKM) |
| 51 | MsgSubTypeChannelPostingPermissions = "channel_posting_permissions" // [Events API, RTM] The posting permissions for a channel changed |
| 52 | ) |
| 53 | |
| 54 | // Msg contains information about a slack message |
| 55 | type Msg struct { |
| 56 | // Basic Message |
| 57 | ClientMsgID string `json:"client_msg_id,omitempty"` |
| 58 | Type string `json:"type,omitempty"` |
| 59 | Channel string `json:"channel,omitempty"` |
| 60 | User string `json:"user,omitempty"` |
| 61 | Text string `json:"text,omitempty"` |
| 62 | Timestamp string `json:"ts,omitempty"` |
| 63 | ThreadTimestamp string `json:"thread_ts,omitempty"` |
| 64 | IsStarred bool `json:"is_starred,omitempty"` |
| 65 | PinnedTo []string `json:"pinned_to,omitempty"` |
| 66 | Attachments []Attachment `json:"attachments,omitempty"` |
| 67 | Edited *Edited `json:"edited,omitempty"` |
| 68 | LastRead string `json:"last_read,omitempty"` |
| 69 | Subscribed bool `json:"subscribed,omitempty"` |
| 70 | UnreadCount int `json:"unread_count,omitempty"` |
| 71 | |
| 72 | // Message Subtypes |
| 73 | SubType string `json:"subtype,omitempty"` |
| 74 | |
| 75 | // Hidden Subtypes |
| 76 | Hidden bool `json:"hidden,omitempty"` // message_changed, message_deleted, unpinned_item |
| 77 | DeletedTimestamp string `json:"deleted_ts,omitempty"` // message_deleted |
| 78 | EventTimestamp string `json:"event_ts,omitempty"` |
| 79 | |
| 80 | // bot_message (https://api.slack.com/events/message/bot_message) |
| 81 | BotID string `json:"bot_id,omitempty"` |
| 82 | Username string `json:"username,omitempty"` |
| 83 | Icons *Icon `json:"icons,omitempty"` |
| 84 | BotProfile *BotProfile `json:"bot_profile,omitempty"` |
| 85 | |
| 86 | // channel_join, group_join |
| 87 | Inviter string `json:"inviter,omitempty"` |
| 88 | |
| 89 | // channel_topic, group_topic |
| 90 | Topic string `json:"topic,omitempty"` |
| 91 | |
| 92 | // channel_purpose, group_purpose |
| 93 | Purpose string `json:"purpose,omitempty"` |
| 94 | |
| 95 | // channel_name, group_name |
| 96 | Name string `json:"name,omitempty"` |
| 97 | OldName string `json:"old_name,omitempty"` |
| 98 | |
| 99 | // channel_archive, group_archive |
| 100 | Members []string `json:"members,omitempty"` |
| 101 | |
| 102 | // channels.replies, groups.replies, im.replies, mpim.replies |
| 103 | ReplyCount int `json:"reply_count,omitempty"` |
| 104 | Replies []Reply `json:"replies,omitempty"` |
| 105 | ParentUserId string `json:"parent_user_id,omitempty"` |
| 106 | LatestReply string `json:"latest_reply,omitempty"` |
| 107 | |
| 108 | // file_share, file_comment, file_mention |
| 109 | Files []File `json:"files,omitempty"` |
| 110 | |
| 111 | // file_share |
| 112 | Upload bool `json:"upload,omitempty"` |
| 113 | |
| 114 | // file_comment |
| 115 | Comment *Comment `json:"comment,omitempty"` |
| 116 | |
| 117 | // pinned_item |
| 118 | ItemType string `json:"item_type,omitempty"` |
| 119 | |
| 120 | // https://api.slack.com/rtm |
| 121 | ReplyTo int `json:"reply_to,omitempty"` |
| 122 | Team string `json:"team,omitempty"` |
| 123 | |
| 124 | // reactions |
| 125 | Reactions []ItemReaction `json:"reactions,omitempty"` |
| 126 | |
| 127 | // slash commands and interactive messages |
| 128 | ResponseType string `json:"response_type,omitempty"` |
| 129 | ReplaceOriginal bool `json:"replace_original"` |
| 130 | DeleteOriginal bool `json:"delete_original"` |
| 131 | |
| 132 | // metadata |
| 133 | Metadata SlackMetadata `json:"metadata,omitempty"` |
| 134 | |
| 135 | // Block type Message |
| 136 | Blocks Blocks `json:"blocks,omitempty"` |
| 137 | // permalink |
| 138 | Permalink string `json:"permalink,omitempty"` |
| 139 | } |
| 140 | |
| 141 | const ( |
| 142 | // ResponseTypeInChannel in channel response for slash commands. |
| 143 | ResponseTypeInChannel = "in_channel" |
| 144 | // ResponseTypeEphemeral ephemeral response for slash commands. |
| 145 | ResponseTypeEphemeral = "ephemeral" |
| 146 | ) |
| 147 | |
| 148 | // ScheduledMessage contains information about a slack scheduled message |
| 149 | type ScheduledMessage struct { |
| 150 | ID string `json:"id"` |
| 151 | Channel string `json:"channel_id"` |
| 152 | PostAt int `json:"post_at"` |
| 153 | DateCreated int `json:"date_created"` |
| 154 | Text string `json:"text"` |
| 155 | } |
| 156 | |
| 157 | // Icon is used for bot messages |
| 158 | type Icon struct { |
| 159 | IconURL string `json:"icon_url,omitempty"` |
| 160 | IconEmoji string `json:"icon_emoji,omitempty"` |
| 161 | } |
| 162 | |
| 163 | // BotProfile contains information about a bot |
| 164 | type BotProfile struct { |
| 165 | AppID string `json:"app_id,omitempty"` |
| 166 | Deleted bool `json:"deleted,omitempty"` |
| 167 | Icons *Icons `json:"icons,omitempty"` |
| 168 | ID string `json:"id,omitempty"` |
| 169 | Name string `json:"name,omitempty"` |
| 170 | TeamID string `json:"team_id,omitempty"` |
| 171 | Updated int64 `json:"updated,omitempty"` |
| 172 | } |
| 173 | |
| 174 | // Edited indicates that a message has been edited. |
| 175 | type Edited struct { |
| 176 | User string `json:"user,omitempty"` |
| 177 | Timestamp string `json:"ts,omitempty"` |
| 178 | } |
| 179 | |
| 180 | // Reply contains information about a reply for a thread |
| 181 | type Reply struct { |
| 182 | User string `json:"user,omitempty"` |
| 183 | Timestamp string `json:"ts,omitempty"` |
| 184 | } |
| 185 | |
| 186 | // Event contains the event type |
| 187 | type Event struct { |
| 188 | Type string `json:"type,omitempty"` |
| 189 | } |
| 190 | |
| 191 | // Ping contains information about a Ping Event |
| 192 | type Ping struct { |
| 193 | ID int `json:"id"` |
| 194 | Type string `json:"type"` |
| 195 | Timestamp int64 `json:"timestamp"` |
| 196 | } |
| 197 | |
| 198 | // Pong contains information about a Pong Event |
| 199 | type Pong struct { |
| 200 | Type string `json:"type"` |
| 201 | ReplyTo int `json:"reply_to"` |
| 202 | Timestamp int64 `json:"timestamp"` |
| 203 | } |
| 204 | |
| 205 | // NewOutgoingMessage prepares an OutgoingMessage that the user can |
| 206 | // use to send a message. Use this function to properly set the |
| 207 | // messageID. |
| 208 | func (rtm *RTM) NewOutgoingMessage(text string, channelID string, options ...RTMsgOption) *OutgoingMessage { |
| 209 | id := rtm.idGen.Next() |
| 210 | msg := OutgoingMessage{ |
| 211 | ID: id, |
| 212 | Type: "message", |
| 213 | Channel: channelID, |
| 214 | Text: text, |
| 215 | } |
| 216 | for _, option := range options { |
| 217 | option(&msg) |
| 218 | } |
| 219 | return &msg |
| 220 | } |
| 221 | |
| 222 | // NewSubscribeUserPresence prepares an OutgoingMessage that the user can |
| 223 | // use to subscribe presence events for the specified users. |
| 224 | func (rtm *RTM) NewSubscribeUserPresence(ids []string) *OutgoingMessage { |
| 225 | return &OutgoingMessage{ |
| 226 | Type: "presence_sub", |
| 227 | IDs: ids, |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // NewTypingMessage prepares an OutgoingMessage that the user can |
| 232 | // use to send as a typing indicator. Use this function to properly set the |
| 233 | // messageID. |
| 234 | func (rtm *RTM) NewTypingMessage(channelID string) *OutgoingMessage { |
| 235 | id := rtm.idGen.Next() |
| 236 | return &OutgoingMessage{ |
| 237 | ID: id, |
| 238 | Type: "typing", |
| 239 | Channel: channelID, |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // RTMsgOption allows configuration of various options available for sending an RTM message |
| 244 | type RTMsgOption func(*OutgoingMessage) |
| 245 | |
| 246 | // RTMsgOptionTS sets thead timestamp of an outgoing message in order to respond to a thread |
| 247 | func RTMsgOptionTS(threadTimestamp string) RTMsgOption { |
| 248 | return func(msg *OutgoingMessage) { |
| 249 | msg.ThreadTimestamp = threadTimestamp |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // RTMsgOptionBroadcast sets broadcast reply to channel to "true" |
| 254 | func RTMsgOptionBroadcast() RTMsgOption { |
| 255 | return func(msg *OutgoingMessage) { |
| 256 | msg.ThreadBroadcast = true |
| 257 | } |
| 258 | } |