| 1 | package bslack |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "time" |
| 10 | |
| 11 | lru "github.com/hashicorp/golang-lru" |
| 12 | "github.com/matterbridge-org/matterbridge/bridge" |
| 13 | "github.com/matterbridge-org/matterbridge/bridge/config" |
| 14 | "github.com/matterbridge-org/matterbridge/bridge/helper" |
| 15 | "github.com/matterbridge-org/matterbridge/matterhook" |
| 16 | "github.com/rs/xid" |
| 17 | "github.com/slack-go/slack" |
| 18 | ) |
| 19 | |
| 20 | type Bslack struct { |
| 21 | sync.RWMutex |
| 22 | *bridge.Config |
| 23 | |
| 24 | mh *matterhook.Client |
| 25 | sc *slack.Client |
| 26 | rtm *slack.RTM |
| 27 | si *slack.Info |
| 28 | |
| 29 | cache *lru.Cache |
| 30 | uuid string |
| 31 | useChannelID bool |
| 32 | |
| 33 | channels *channels |
| 34 | users *users |
| 35 | legacy bool |
| 36 | } |
| 37 | |
| 38 | const ( |
| 39 | sHello = "hello" |
| 40 | sChannelJoin = "channel_join" |
| 41 | sChannelLeave = "channel_leave" |
| 42 | sChannelJoined = "channel_joined" |
| 43 | sMemberJoined = "member_joined_channel" |
| 44 | sMessageChanged = "message_changed" |
| 45 | sMessageDeleted = "message_deleted" |
| 46 | sSlackAttachment = "slack_attachment" |
| 47 | sPinnedItem = "pinned_item" |
| 48 | sUnpinnedItem = "unpinned_item" |
| 49 | sChannelTopic = "channel_topic" |
| 50 | sChannelPurpose = "channel_purpose" |
| 51 | sFileComment = "file_comment" |
| 52 | sMeMessage = "me_message" |
| 53 | sUserTyping = "user_typing" |
| 54 | sLatencyReport = "latency_report" |
| 55 | sSystemUser = "system" |
| 56 | sSlackBotUser = "slackbot" |
| 57 | cfileDownloadChannel = "file_download_channel" |
| 58 | |
| 59 | tokenConfig = "Token" |
| 60 | incomingWebhookConfig = "WebhookBindAddress" |
| 61 | outgoingWebhookConfig = "WebhookURL" |
| 62 | skipTLSConfig = "SkipTLSVerify" |
| 63 | useNickPrefixConfig = "PrefixMessagesWithNick" |
| 64 | editDisableConfig = "EditDisable" |
| 65 | editSuffixConfig = "EditSuffix" |
| 66 | iconURLConfig = "iconurl" |
| 67 | noSendJoinConfig = "nosendjoinpart" |
| 68 | messageLength = 3000 |
| 69 | ) |
| 70 | |
| 71 | func New(cfg *bridge.Config) bridge.Bridger { |
| 72 | // Print a deprecation warning for legacy non-bot tokens (#527). |
| 73 | token := cfg.GetString(tokenConfig) |
| 74 | if token != "" && !strings.HasPrefix(token, "xoxb") { |
| 75 | cfg.Log.Warn("Non-bot token detected. It is STRONGLY recommended to use a proper bot-token instead.") |
| 76 | cfg.Log.Warn("Legacy tokens may be deprecated by Slack at short notice. See the Matterbridge GitHub wiki for a migration guide.") |
| 77 | cfg.Log.Warn("See https://github.com/42wim/matterbridge/wiki/Slack-bot-setup") |
| 78 | return NewLegacy(cfg) |
| 79 | } |
| 80 | return newBridge(cfg) |
| 81 | } |
| 82 | |
| 83 | func newBridge(cfg *bridge.Config) *Bslack { |
| 84 | newCache, err := lru.New(5000) |
| 85 | if err != nil { |
| 86 | cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err) |
| 87 | } |
| 88 | b := &Bslack{ |
| 89 | Config: cfg, |
| 90 | uuid: xid.New().String(), |
| 91 | cache: newCache, |
| 92 | } |
| 93 | return b |
| 94 | } |
| 95 | |
| 96 | func (b *Bslack) Command(cmd string) string { |
| 97 | return "" |
| 98 | } |
| 99 | |
| 100 | func (b *Bslack) Connect() error { |
| 101 | b.RLock() |
| 102 | defer b.RUnlock() |
| 103 | |
| 104 | if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" { |
| 105 | return errors.New("no connection method found: WebhookBindAddress, WebhookURL or Token need to be configured") |
| 106 | } |
| 107 | |
| 108 | // If we have a token we use the Slack websocket-based RTM for both sending and receiving. |
| 109 | if token := b.GetString(tokenConfig); token != "" { |
| 110 | b.Log.Info("Connecting using token") |
| 111 | |
| 112 | b.sc = slack.New(token, slack.OptionDebug(b.GetBool("Debug"))) |
| 113 | |
| 114 | b.channels = newChannelManager(b.Log, b.sc) |
| 115 | b.users = newUserManager(b.Log, b.sc) |
| 116 | |
| 117 | b.rtm = b.sc.NewRTM() |
| 118 | go b.rtm.ManageConnection() |
| 119 | go b.handleSlack() |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // In absence of a token we fall back to incoming and outgoing Webhooks. |
| 124 | b.mh = matterhook.New( |
| 125 | "", |
| 126 | matterhook.Config{ |
| 127 | InsecureSkipVerify: b.GetBool("SkipTLSVerify"), |
| 128 | DisableServer: true, |
| 129 | }, |
| 130 | ) |
| 131 | if b.GetString(outgoingWebhookConfig) != "" { |
| 132 | b.Log.Info("Using specified webhook for outgoing messages.") |
| 133 | b.mh.Url = b.GetString(outgoingWebhookConfig) |
| 134 | } |
| 135 | if b.GetString(incomingWebhookConfig) != "" { |
| 136 | b.Log.Info("Setting up local webhook for incoming messages.") |
| 137 | b.mh.BindAddress = b.GetString(incomingWebhookConfig) |
| 138 | b.mh.DisableServer = false |
| 139 | go b.handleSlack() |
| 140 | } |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | func (b *Bslack) Disconnect() error { |
| 145 | return b.rtm.Disconnect() |
| 146 | } |
| 147 | |
| 148 | // JoinChannel only acts as a verification method that checks whether Matterbridge's |
| 149 | // Slack integration is already member of the channel. This is because Slack does not |
| 150 | // allow apps or bots to join channels themselves and they need to be invited |
| 151 | // manually by a user. |
| 152 | func (b *Bslack) JoinChannel(channel config.ChannelInfo) error { |
| 153 | // We can only join a channel through the Slack API. |
| 154 | if b.sc == nil { |
| 155 | return nil |
| 156 | } |
| 157 | |
| 158 | // try to join a channel when in legacy |
| 159 | if b.legacy { |
| 160 | _, _, _, err := b.sc.JoinConversation(channel.Name) |
| 161 | if err != nil { |
| 162 | switch err.Error() { |
| 163 | case "name_taken", "restricted_action": |
| 164 | case "default": |
| 165 | return err |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | b.channels.populateChannels(false) |
| 171 | |
| 172 | channelInfo, err := b.channels.getChannel(channel.Name) |
| 173 | if err != nil { |
| 174 | return fmt.Errorf("could not join channel: %#v", err) |
| 175 | } |
| 176 | |
| 177 | if strings.HasPrefix(channel.Name, "ID:") { |
| 178 | b.useChannelID = true |
| 179 | channel.Name = channelInfo.Name |
| 180 | } |
| 181 | |
| 182 | // we can't join a channel unless we are using legacy tokens #651 |
| 183 | if !channelInfo.IsMember && !b.legacy { |
| 184 | return fmt.Errorf("slack integration that matterbridge is using is not member of channel '%s', please add it manually", channelInfo.Name) |
| 185 | } |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | func (b *Bslack) Reload(cfg *bridge.Config) (string, error) { |
| 190 | return "", nil |
| 191 | } |
| 192 | |
| 193 | func (b *Bslack) Send(msg config.Message) (string, error) { |
| 194 | // Too noisy to log like other events |
| 195 | if msg.Event != config.EventUserTyping { |
| 196 | b.Log.Debugf("=> Receiving %#v", msg) |
| 197 | } |
| 198 | |
| 199 | msg.Text = helper.ClipMessage(msg.Text, messageLength, b.GetString("MessageClipped")) |
| 200 | msg.Text = b.replaceCodeFence(msg.Text) |
| 201 | |
| 202 | // Make a action /me of the message |
| 203 | if msg.Event == config.EventUserAction { |
| 204 | msg.Text = "_" + msg.Text + "_" |
| 205 | } |
| 206 | |
| 207 | // Use webhook to send the message |
| 208 | if b.GetString(outgoingWebhookConfig) != "" && b.GetString(tokenConfig) == "" { |
| 209 | return "", b.sendWebhook(msg) |
| 210 | } |
| 211 | return b.sendRTM(msg) |
| 212 | } |
| 213 | |
| 214 | // sendWebhook uses the configured WebhookURL to send the message |
| 215 | func (b *Bslack) sendWebhook(msg config.Message) error { |
| 216 | // Skip events. |
| 217 | if msg.Event != "" { |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | if b.GetBool(useNickPrefixConfig) { |
| 222 | msg.Text = msg.Username + msg.Text |
| 223 | } |
| 224 | |
| 225 | if msg.Extra != nil { |
| 226 | // This sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE. |
| 227 | for _, rmsg := range helper.HandleExtra(&msg, b.General) { |
| 228 | rmsg := rmsg // scopelint |
| 229 | iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig)) |
| 230 | matterMessage := matterhook.OMessage{ |
| 231 | IconURL: iconURL, |
| 232 | Channel: msg.Channel, |
| 233 | UserName: rmsg.Username, |
| 234 | Text: rmsg.Text, |
| 235 | } |
| 236 | if err := b.mh.Send(matterMessage); err != nil { |
| 237 | b.Log.Errorf("Failed to send message: %v", err) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Webhook doesn't support file uploads, so we add the URL manually. |
| 242 | for _, f := range msg.Extra["file"] { |
| 243 | fi, ok := f.(config.FileInfo) |
| 244 | if !ok { |
| 245 | b.Log.Errorf("Received a file with unexpected content: %#v", f) |
| 246 | continue |
| 247 | } |
| 248 | if fi.URL != "" { |
| 249 | msg.Text += " " + fi.URL |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // If we have native slack_attachments add them. |
| 255 | var attachs []slack.Attachment |
| 256 | for _, attach := range msg.Extra[sSlackAttachment] { |
| 257 | attachs = append(attachs, attach.([]slack.Attachment)...) |
| 258 | } |
| 259 | |
| 260 | iconURL := config.GetIconURL(&msg, b.GetString(iconURLConfig)) |
| 261 | matterMessage := matterhook.OMessage{ |
| 262 | IconURL: iconURL, |
| 263 | Attachments: attachs, |
| 264 | Channel: msg.Channel, |
| 265 | UserName: msg.Username, |
| 266 | Text: msg.Text, |
| 267 | } |
| 268 | if msg.Avatar != "" { |
| 269 | matterMessage.IconURL = msg.Avatar |
| 270 | } |
| 271 | if err := b.mh.Send(matterMessage); err != nil { |
| 272 | b.Log.Errorf("Failed to send message via webhook: %#v", err) |
| 273 | return err |
| 274 | } |
| 275 | return nil |
| 276 | } |
| 277 | |
| 278 | func (b *Bslack) sendRTM(msg config.Message) (string, error) { |
| 279 | // Handle channelmember messages. |
| 280 | if handled := b.handleGetChannelMembers(&msg); handled { |
| 281 | return "", nil |
| 282 | } |
| 283 | |
| 284 | channelInfo, err := b.channels.getChannel(msg.Channel) |
| 285 | if err != nil { |
| 286 | return "", fmt.Errorf("could not send message: %v", err) |
| 287 | } |
| 288 | if msg.Event == config.EventUserTyping { |
| 289 | if b.GetBool("ShowUserTyping") { |
| 290 | b.rtm.SendMessage(b.rtm.NewTypingMessage(channelInfo.ID)) |
| 291 | } |
| 292 | return "", nil |
| 293 | } |
| 294 | |
| 295 | var handled bool |
| 296 | |
| 297 | // Handle topic/purpose updates. |
| 298 | if handled, err = b.handleTopicOrPurpose(&msg, channelInfo); handled { |
| 299 | return "", err |
| 300 | } |
| 301 | |
| 302 | // Handle prefix hint for unthreaded messages. |
| 303 | if msg.ParentNotFound() { |
| 304 | msg.ParentID = "" |
| 305 | msg.Text = fmt.Sprintf("[thread]: %s", msg.Text) |
| 306 | } |
| 307 | |
| 308 | // Handle message deletions. |
| 309 | if handled, err = b.deleteMessage(&msg, channelInfo); handled { |
| 310 | return msg.ID, err |
| 311 | } |
| 312 | |
| 313 | // Prepend nickname if configured. |
| 314 | if b.GetBool(useNickPrefixConfig) { |
| 315 | msg.Text = msg.Username + msg.Text |
| 316 | } |
| 317 | |
| 318 | // Handle message edits. |
| 319 | if handled, err = b.editMessage(&msg, channelInfo); handled { |
| 320 | return msg.ID, err |
| 321 | } |
| 322 | |
| 323 | // Upload a file if it exists. |
| 324 | if len(msg.Extra) > 0 { |
| 325 | extraMsgs := helper.HandleExtra(&msg, b.General) |
| 326 | for i := range extraMsgs { |
| 327 | rmsg := &extraMsgs[i] |
| 328 | rmsg.Text = rmsg.Username + rmsg.Text |
| 329 | _, err = b.postMessage(rmsg, channelInfo) |
| 330 | if err != nil { |
| 331 | b.Log.Error(err) |
| 332 | } |
| 333 | } |
| 334 | // Upload files if necessary (from Slack, Telegram or Mattermost). |
| 335 | return b.uploadFile(&msg, channelInfo.ID) |
| 336 | } |
| 337 | |
| 338 | // Post message. |
| 339 | return b.postMessage(&msg, channelInfo) |
| 340 | } |
| 341 | |
| 342 | func (b *Bslack) updateTopicOrPurpose(msg *config.Message, channelInfo *slack.Channel) error { |
| 343 | var updateFunc func(channelID string, value string) (*slack.Channel, error) |
| 344 | |
| 345 | incomingChangeType, text := b.extractTopicOrPurpose(msg.Text) |
| 346 | switch incomingChangeType { |
| 347 | case "topic": |
| 348 | updateFunc = b.rtm.SetTopicOfConversation |
| 349 | case "purpose": |
| 350 | updateFunc = b.rtm.SetPurposeOfConversation |
| 351 | default: |
| 352 | b.Log.Errorf("Unhandled type received from extractTopicOrPurpose: %s", incomingChangeType) |
| 353 | return nil |
| 354 | } |
| 355 | for { |
| 356 | _, err := updateFunc(channelInfo.ID, text) |
| 357 | if err == nil { |
| 358 | return nil |
| 359 | } |
| 360 | if err = handleRateLimit(b.Log, err); err != nil { |
| 361 | return err |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // handles updating topic/purpose and determining whether to further propagate update messages. |
| 367 | func (b *Bslack) handleTopicOrPurpose(msg *config.Message, channelInfo *slack.Channel) (bool, error) { |
| 368 | if msg.Event != config.EventTopicChange { |
| 369 | return false, nil |
| 370 | } |
| 371 | |
| 372 | if b.GetBool("SyncTopic") { |
| 373 | return true, b.updateTopicOrPurpose(msg, channelInfo) |
| 374 | } |
| 375 | |
| 376 | // Pass along to normal message handlers. |
| 377 | if b.GetBool("ShowTopicChange") { |
| 378 | return false, nil |
| 379 | } |
| 380 | |
| 381 | // Swallow message as handled no-op. |
| 382 | return true, nil |
| 383 | } |
| 384 | |
| 385 | func (b *Bslack) deleteMessage(msg *config.Message, channelInfo *slack.Channel) (bool, error) { |
| 386 | if msg.Event != config.EventMsgDelete { |
| 387 | return false, nil |
| 388 | } |
| 389 | |
| 390 | // Some protocols echo deletes, but with an empty ID. |
| 391 | if msg.ID == "" { |
| 392 | return true, nil |
| 393 | } |
| 394 | |
| 395 | for { |
| 396 | _, _, err := b.rtm.DeleteMessage(channelInfo.ID, msg.ID) |
| 397 | if err == nil { |
| 398 | return true, nil |
| 399 | } |
| 400 | |
| 401 | if err = handleRateLimit(b.Log, err); err != nil { |
| 402 | b.Log.Errorf("Failed to delete user message from Slack: %#v", err) |
| 403 | return true, err |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | func (b *Bslack) editMessage(msg *config.Message, channelInfo *slack.Channel) (bool, error) { |
| 409 | if msg.ID == "" { |
| 410 | return false, nil |
| 411 | } |
| 412 | messageOptions := b.prepareMessageOptions(msg) |
| 413 | for { |
| 414 | _, _, _, err := b.rtm.UpdateMessage(channelInfo.ID, msg.ID, messageOptions...) |
| 415 | if err == nil { |
| 416 | return true, nil |
| 417 | } |
| 418 | |
| 419 | if err = handleRateLimit(b.Log, err); err != nil { |
| 420 | b.Log.Errorf("Failed to edit user message on Slack: %#v", err) |
| 421 | return true, err |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | func (b *Bslack) postMessage(msg *config.Message, channelInfo *slack.Channel) (string, error) { |
| 427 | // don't post empty messages |
| 428 | if msg.Text == "" { |
| 429 | return "", nil |
| 430 | } |
| 431 | messageOptions := b.prepareMessageOptions(msg) |
| 432 | for { |
| 433 | _, id, err := b.rtm.PostMessage(channelInfo.ID, messageOptions...) |
| 434 | if err == nil { |
| 435 | return id, nil |
| 436 | } |
| 437 | |
| 438 | if err = handleRateLimit(b.Log, err); err != nil { |
| 439 | b.Log.Errorf("Failed to sent user message to Slack: %#v", err) |
| 440 | return "", err |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // uploadFile handles native upload of files |
| 446 | func (b *Bslack) uploadFile(msg *config.Message, channelID string) (string, error) { |
| 447 | var messageID string |
| 448 | for _, f := range msg.Extra["file"] { |
| 449 | fi, ok := f.(config.FileInfo) |
| 450 | if !ok { |
| 451 | b.Log.Errorf("Received a file with unexpected content: %#v", f) |
| 452 | continue |
| 453 | } |
| 454 | if msg.Text == fi.Comment { |
| 455 | msg.Text = "" |
| 456 | } |
| 457 | // Because the result of the UploadFile is slower than the MessageEvent from slack |
| 458 | // we can't match on the file ID yet, so we have to match on the filename too. |
| 459 | ts := time.Now() |
| 460 | b.Log.Debugf("Adding file %s to cache at %s with timestamp", fi.Name, ts.String()) |
| 461 | b.cache.Add("filename"+fi.Name, ts) |
| 462 | initialComment := fmt.Sprintf("File from %s", msg.Username) |
| 463 | if fi.Comment != "" { |
| 464 | initialComment += fmt.Sprintf(" with comment: %s", fi.Comment) |
| 465 | } |
| 466 | res, err := b.sc.UploadFile(slack.FileUploadParameters{ |
| 467 | Reader: bytes.NewReader(*fi.Data), |
| 468 | Filename: fi.Name, |
| 469 | Channels: []string{channelID}, |
| 470 | InitialComment: initialComment, |
| 471 | ThreadTimestamp: msg.ParentID, |
| 472 | }) |
| 473 | if err != nil { |
| 474 | b.Log.Errorf("uploadfile %#v", err) |
| 475 | return "", err |
| 476 | } |
| 477 | if res.ID != "" { |
| 478 | b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String()) |
| 479 | b.cache.Add("file"+res.ID, ts) |
| 480 | |
| 481 | // search for message id by uploaded file in private/public channels, get thread timestamp from uploaded file |
| 482 | if v, ok := res.Shares.Private[channelID]; ok && len(v) > 0 { |
| 483 | messageID = v[0].Ts |
| 484 | } |
| 485 | if v, ok := res.Shares.Public[channelID]; ok && len(v) > 0 { |
| 486 | messageID = v[0].Ts |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | return messageID, nil |
| 491 | } |
| 492 | |
| 493 | func (b *Bslack) prepareMessageOptions(msg *config.Message) []slack.MsgOption { |
| 494 | params := slack.NewPostMessageParameters() |
| 495 | if b.GetBool(useNickPrefixConfig) { |
| 496 | params.AsUser = true |
| 497 | } |
| 498 | params.Username = msg.Username |
| 499 | params.LinkNames = 1 // replace mentions |
| 500 | params.IconURL = config.GetIconURL(msg, b.GetString(iconURLConfig)) |
| 501 | params.ThreadTimestamp = msg.ParentID |
| 502 | if msg.Avatar != "" { |
| 503 | params.IconURL = msg.Avatar |
| 504 | } |
| 505 | |
| 506 | var attachments []slack.Attachment |
| 507 | // add file attachments |
| 508 | attachments = append(attachments, b.createAttach(msg.Extra)...) |
| 509 | // add slack attachments (from another slack bridge) |
| 510 | if msg.Extra != nil { |
| 511 | for _, attach := range msg.Extra[sSlackAttachment] { |
| 512 | attachments = append(attachments, attach.([]slack.Attachment)...) |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | var opts []slack.MsgOption |
| 517 | opts = append(opts, |
| 518 | // provide regular text field (fallback used in Slack notifications, etc.) |
| 519 | slack.MsgOptionText(msg.Text, false), |
| 520 | |
| 521 | // add a callback ID so we can see we created it |
| 522 | slack.MsgOptionBlocks(slack.NewSectionBlock( |
| 523 | slack.NewTextBlockObject(slack.MarkdownType, msg.Text, false, false), |
| 524 | nil, nil, |
| 525 | slack.SectionBlockOptionBlockID("matterbridge_"+b.uuid), |
| 526 | )), |
| 527 | |
| 528 | slack.MsgOptionEnableLinkUnfurl(), |
| 529 | ) |
| 530 | opts = append(opts, slack.MsgOptionAttachments(attachments...)) |
| 531 | opts = append(opts, slack.MsgOptionPostMessageParameters(params)) |
| 532 | return opts |
| 533 | } |
| 534 | |
| 535 | func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment { |
| 536 | var attachements []slack.Attachment |
| 537 | for _, v := range extra["attachments"] { |
| 538 | entry := v.(map[string]interface{}) |
| 539 | s := slack.Attachment{ |
| 540 | Fallback: extractStringField(entry, "fallback"), |
| 541 | Color: extractStringField(entry, "color"), |
| 542 | Pretext: extractStringField(entry, "pretext"), |
| 543 | AuthorName: extractStringField(entry, "author_name"), |
| 544 | AuthorLink: extractStringField(entry, "author_link"), |
| 545 | AuthorIcon: extractStringField(entry, "author_icon"), |
| 546 | Title: extractStringField(entry, "title"), |
| 547 | TitleLink: extractStringField(entry, "title_link"), |
| 548 | Text: extractStringField(entry, "text"), |
| 549 | ImageURL: extractStringField(entry, "image_url"), |
| 550 | ThumbURL: extractStringField(entry, "thumb_url"), |
| 551 | Footer: extractStringField(entry, "footer"), |
| 552 | FooterIcon: extractStringField(entry, "footer_icon"), |
| 553 | } |
| 554 | attachements = append(attachements, s) |
| 555 | } |
| 556 | return attachements |
| 557 | } |
| 558 | |
| 559 | func extractStringField(data map[string]interface{}, field string) string { |
| 560 | if rawValue, found := data[field]; found { |
| 561 | if value, ok := rawValue.(string); ok { |
| 562 | return value |
| 563 | } |
| 564 | } |
| 565 | return "" |
| 566 | } |
| 567 | |