2016-09-05 16:34:37 +02:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2018-11-07 21:35:59 +01:00
|
|
|
"bytes"
|
2017-07-15 16:49:47 +02:00
|
|
|
"errors"
|
2018-10-13 01:02:14 +02:00
|
|
|
"fmt"
|
2016-09-05 16:34:37 +02:00
|
|
|
"strings"
|
2018-03-04 23:52:14 +01:00
|
|
|
"sync"
|
2018-11-07 20:36:50 +01:00
|
|
|
"time"
|
2018-03-22 22:28:27 +01:00
|
|
|
|
|
|
|
"github.com/42wim/matterbridge/bridge"
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
2018-08-18 00:12:05 +02:00
|
|
|
"github.com/hashicorp/golang-lru"
|
2018-03-22 22:28:27 +01:00
|
|
|
"github.com/nlopes/slack"
|
2018-05-27 21:50:00 +02:00
|
|
|
"github.com/rs/xid"
|
2016-09-05 16:34:37 +02:00
|
|
|
)
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
type Bslack struct {
|
2018-10-16 20:34:09 +02:00
|
|
|
sync.RWMutex
|
|
|
|
*bridge.Config
|
|
|
|
|
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
|
|
|
rtm *slack.RTM
|
|
|
|
si *slack.Info
|
|
|
|
|
2018-08-18 00:12:05 +02:00
|
|
|
cache *lru.Cache
|
2018-07-13 23:23:11 +02:00
|
|
|
uuid string
|
2018-10-16 20:34:09 +02:00
|
|
|
useChannelID bool
|
|
|
|
|
|
|
|
users map[string]*slack.User
|
|
|
|
usersMutex sync.RWMutex
|
|
|
|
|
|
|
|
channelsByID map[string]*slack.Channel
|
|
|
|
channelsByName map[string]*slack.Channel
|
|
|
|
channelsMutex sync.RWMutex
|
2018-11-07 20:36:50 +01:00
|
|
|
|
|
|
|
refreshInProgress bool
|
|
|
|
earliestChannelRefresh time.Time
|
|
|
|
earliestUserRefresh time.Time
|
|
|
|
refreshMutex sync.Mutex
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:02:14 +02:00
|
|
|
const (
|
|
|
|
sChannelJoin = "channel_join"
|
|
|
|
sChannelLeave = "channel_leave"
|
2018-11-07 21:35:59 +01:00
|
|
|
sChannelJoined = "channel_joined"
|
|
|
|
sMemberJoined = "member_joined_channel"
|
2018-11-25 11:08:57 +01:00
|
|
|
sMessageChanged = "message_changed"
|
2018-10-13 01:02:14 +02:00
|
|
|
sMessageDeleted = "message_deleted"
|
|
|
|
sSlackAttachment = "slack_attachment"
|
|
|
|
sPinnedItem = "pinned_item"
|
|
|
|
sUnpinnedItem = "unpinned_item"
|
|
|
|
sChannelTopic = "channel_topic"
|
|
|
|
sChannelPurpose = "channel_purpose"
|
|
|
|
sFileComment = "file_comment"
|
|
|
|
sMeMessage = "me_message"
|
|
|
|
sUserTyping = "user_typing"
|
|
|
|
sLatencyReport = "latency_report"
|
|
|
|
sSystemUser = "system"
|
2018-11-07 21:35:59 +01:00
|
|
|
sSlackBotUser = "slackbot"
|
2018-10-13 01:02:14 +02:00
|
|
|
|
|
|
|
tokenConfig = "Token"
|
|
|
|
incomingWebhookConfig = "WebhookBindAddress"
|
|
|
|
outgoingWebhookConfig = "WebhookURL"
|
|
|
|
skipTLSConfig = "SkipTLSVerify"
|
|
|
|
useNickPrefixConfig = "PrefixMessagesWithNick"
|
|
|
|
editDisableConfig = "EditDisable"
|
|
|
|
editSuffixConfig = "EditSuffix"
|
|
|
|
iconURLConfig = "iconurl"
|
|
|
|
noSendJoinConfig = "nosendjoinpart"
|
|
|
|
)
|
2016-09-05 16:34:37 +02:00
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2018-11-13 20:51:19 +01:00
|
|
|
// Print a deprecation warning for legacy non-bot tokens (#527).
|
|
|
|
token := cfg.GetString(tokenConfig)
|
|
|
|
if token != "" && !strings.HasPrefix(token, "xoxb") {
|
2018-12-02 23:09:21 +01:00
|
|
|
cfg.Log.Warn("Non-bot token detected. It is STRONGLY recommended to use a proper bot-token instead.")
|
|
|
|
cfg.Log.Warn("Legacy tokens may be deprecated by Slack at short notice. See the Matterbridge GitHub wiki for a migration guide.")
|
|
|
|
cfg.Log.Warn("See https://github.com/42wim/matterbridge/wiki/Slack-bot-setup")
|
2018-11-13 20:51:19 +01:00
|
|
|
return NewLegacy(cfg)
|
|
|
|
}
|
|
|
|
return newBridge(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBridge(cfg *bridge.Config) *Bslack {
|
2018-10-13 01:02:14 +02:00
|
|
|
newCache, err := lru.New(5000)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
|
|
|
|
}
|
|
|
|
b := &Bslack{
|
2018-11-07 20:36:50 +01:00
|
|
|
Config: cfg,
|
|
|
|
uuid: xid.New().String(),
|
|
|
|
cache: newCache,
|
|
|
|
users: map[string]*slack.User{},
|
|
|
|
channelsByID: map[string]*slack.Channel{},
|
|
|
|
channelsByName: map[string]*slack.Channel{},
|
|
|
|
earliestChannelRefresh: time.Now(),
|
|
|
|
earliestUserRefresh: time.Now(),
|
2018-10-13 01:02:14 +02:00
|
|
|
}
|
2018-08-18 00:12:05 +02:00
|
|
|
return b
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) Command(cmd string) string {
|
2016-09-05 16:34:37 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) Connect() error {
|
2018-03-04 23:52:14 +01:00
|
|
|
b.RLock()
|
|
|
|
defer b.RUnlock()
|
2018-11-13 20:51:19 +01:00
|
|
|
|
|
|
|
if b.GetString(incomingWebhookConfig) == "" && b.GetString(outgoingWebhookConfig) == "" && b.GetString(tokenConfig) == "" {
|
|
|
|
return errors.New("no connection method found: WebhookBindAddress, WebhookURL or Token need to be configured")
|
2017-07-15 16:49:47 +02:00
|
|
|
}
|
2018-11-13 20:51:19 +01:00
|
|
|
|
|
|
|
// If we have a token we use the Slack websocket-based RTM for both sending and receiving.
|
|
|
|
if token := b.GetString(tokenConfig); token != "" {
|
|
|
|
b.Log.Info("Connecting using token")
|
|
|
|
b.sc = slack.New(token)
|
2016-10-25 23:29:32 +02:00
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
2017-07-15 16:49:47 +02:00
|
|
|
go b.handleSlack()
|
2018-11-13 20:51:19 +01:00
|
|
|
return nil
|
2017-07-15 16:49:47 +02:00
|
|
|
}
|
2018-11-13 20:51:19 +01:00
|
|
|
|
|
|
|
// In absence of a token we fall back to incoming and outgoing Webhooks.
|
|
|
|
b.mh = matterhook.New(
|
|
|
|
"",
|
|
|
|
matterhook.Config{
|
|
|
|
InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
|
|
|
DisableServer: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if b.GetString(outgoingWebhookConfig) != "" {
|
|
|
|
b.Log.Info("Using specified webhook for outgoing messages.")
|
|
|
|
b.mh.Url = b.GetString(outgoingWebhookConfig)
|
|
|
|
}
|
|
|
|
if b.GetString(incomingWebhookConfig) != "" {
|
|
|
|
b.Log.Info("Setting up local webhook for incoming messages.")
|
|
|
|
b.mh.BindAddress = b.GetString(incomingWebhookConfig)
|
|
|
|
b.mh.DisableServer = false
|
|
|
|
go b.handleSlack()
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:12:02 +01:00
|
|
|
func (b *Bslack) Disconnect() error {
|
2018-03-04 23:52:14 +01:00
|
|
|
return b.rtm.Disconnect()
|
2017-02-14 21:12:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 20:34:09 +02:00
|
|
|
// JoinChannel only acts as a verification method that checks whether Matterbridge's
|
|
|
|
// Slack integration is already member of the channel. This is because Slack does not
|
|
|
|
// allow apps or bots to join channels themselves and they need to be invited
|
|
|
|
// manually by a user.
|
2017-08-12 14:51:41 +02:00
|
|
|
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
2018-11-07 17:25:00 +01:00
|
|
|
// We can only join a channel through the Slack API.
|
|
|
|
if b.sc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-15 23:11:03 +01:00
|
|
|
b.populateChannels(false)
|
2018-10-16 20:34:09 +02:00
|
|
|
|
|
|
|
channelInfo, err := b.getChannel(channel.Name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not join channel: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(channel.Name, "ID:") {
|
2018-10-13 01:02:14 +02:00
|
|
|
b.useChannelID = true
|
2018-10-16 20:34:09 +02:00
|
|
|
channel.Name = channelInfo.Name
|
2018-07-13 23:23:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 20:34:09 +02:00
|
|
|
if !channelInfo.IsMember {
|
|
|
|
return fmt.Errorf("slack integration that matterbridge is using is not member of channel '%s', please add it manually", channelInfo.Name)
|
2016-09-18 19:21:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-22 19:43:57 +02:00
|
|
|
func (b *Bslack) Reload(cfg *bridge.Config) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:59:37 +02:00
|
|
|
func (b *Bslack) Send(msg config.Message) (string, error) {
|
2018-11-08 20:45:40 +01:00
|
|
|
// Too noisy to log like other events
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event != config.EventUserTyping {
|
2018-11-08 20:45:40 +01:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
|
|
|
}
|
2018-02-24 23:35:53 +01:00
|
|
|
|
2019-01-04 20:32:58 +01:00
|
|
|
msg.Text = b.replaceCodeFence(msg.Text)
|
|
|
|
|
2018-02-24 23:35:53 +01:00
|
|
|
// Make a action /me of the message
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventUserAction {
|
2017-07-30 17:48:23 +02:00
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2018-02-24 23:35:53 +01:00
|
|
|
|
|
|
|
// Use webhook to send the message
|
2018-10-13 01:02:14 +02:00
|
|
|
if b.GetString(outgoingWebhookConfig) != "" {
|
2018-12-08 17:04:10 +01:00
|
|
|
return "", b.sendWebhook(msg)
|
2018-02-24 23:35:53 +01:00
|
|
|
}
|
2018-10-22 19:43:57 +02:00
|
|
|
return b.sendRTM(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendWebhook uses the configured WebhookURL to send the message
|
2018-12-08 17:04:10 +01:00
|
|
|
func (b *Bslack) sendWebhook(msg config.Message) error {
|
2018-11-12 15:58:00 +01:00
|
|
|
// Skip events.
|
2018-10-22 19:43:57 +02:00
|
|
|
if msg.Event != "" {
|
2018-12-08 17:04:10 +01:00
|
|
|
return nil
|
2018-10-22 19:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Extra != nil {
|
2018-11-12 15:58:00 +01:00
|
|
|
// This sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE.
|
2018-10-22 19:43:57 +02:00
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-11-08 22:29:34 +01:00
|
|
|
rmsg := rmsg // scopelint
|
2018-10-22 19:43:57 +02:00
|
|
|
iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig))
|
|
|
|
matterMessage := matterhook.OMessage{
|
|
|
|
IconURL: iconURL,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
UserName: rmsg.Username,
|
|
|
|
Text: rmsg.Text,
|
|
|
|
}
|
|
|
|
if err := b.mh.Send(matterMessage); err != nil {
|
|
|
|
b.Log.Errorf("Failed to send message: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-02-24 23:35:53 +01:00
|
|
|
|
2018-11-12 15:58:00 +01:00
|
|
|
// Webhook doesn't support file uploads, so we add the URL manually.
|
2018-10-22 19:43:57 +02:00
|
|
|
for _, f := range msg.Extra["file"] {
|
2018-11-12 15:58:00 +01:00
|
|
|
fi, ok := f.(config.FileInfo)
|
|
|
|
if !ok {
|
|
|
|
b.Log.Errorf("Received a file with unexpected content: %#v", f)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-22 19:43:57 +02:00
|
|
|
if fi.URL != "" {
|
|
|
|
msg.Text += " " + fi.URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 15:58:00 +01:00
|
|
|
// If we have native slack_attachments add them.
|
2018-10-22 19:43:57 +02:00
|
|
|
var attachs []slack.Attachment
|
|
|
|
for _, attach := range msg.Extra[sSlackAttachment] {
|
|
|
|
attachs = append(attachs, attach.([]slack.Attachment)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
iconURL := config.GetIconURL(&msg, b.GetString(iconURLConfig))
|
|
|
|
matterMessage := matterhook.OMessage{
|
|
|
|
IconURL: iconURL,
|
|
|
|
Attachments: attachs,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
UserName: msg.Username,
|
|
|
|
Text: msg.Text,
|
|
|
|
}
|
|
|
|
if msg.Avatar != "" {
|
|
|
|
matterMessage.IconURL = msg.Avatar
|
|
|
|
}
|
2018-11-12 15:58:00 +01:00
|
|
|
if err := b.mh.Send(matterMessage); err != nil {
|
|
|
|
b.Log.Errorf("Failed to send message via webhook: %#v", err)
|
2018-12-08 17:04:10 +01:00
|
|
|
return err
|
2018-10-22 19:43:57 +02:00
|
|
|
}
|
2018-12-08 17:04:10 +01:00
|
|
|
return nil
|
2018-10-22 19:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) sendRTM(msg config.Message) (string, error) {
|
2018-10-13 01:02:14 +02:00
|
|
|
channelInfo, err := b.getChannel(msg.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not send message: %v", err)
|
|
|
|
}
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventUserTyping {
|
2018-11-08 20:45:40 +01:00
|
|
|
if b.GetBool("ShowUserTyping") {
|
|
|
|
b.rtm.SendMessage(b.rtm.NewTypingMessage(channelInfo.ID))
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
2018-02-24 23:35:53 +01:00
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
var handled bool
|
2018-11-26 10:47:04 +01:00
|
|
|
|
|
|
|
// Handle topic/purpose updates.
|
|
|
|
if handled, err = b.handleTopicOrPurpose(&msg, channelInfo); handled {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle message deletions.
|
2018-11-10 22:09:41 +01:00
|
|
|
if handled, err = b.deleteMessage(&msg, channelInfo); handled {
|
|
|
|
return msg.ID, err
|
2018-02-24 23:35:53 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
// Prepend nickname if configured.
|
2018-10-13 01:02:14 +02:00
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
2018-02-24 23:35:53 +01:00
|
|
|
msg.Text = msg.Username + msg.Text
|
|
|
|
}
|
2018-02-03 01:11:11 +01:00
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
// Handle message edits.
|
|
|
|
if handled, err = b.editMessage(&msg, channelInfo); handled {
|
|
|
|
return msg.ID, err
|
2016-10-08 21:57:03 +02:00
|
|
|
}
|
2018-02-24 23:35:53 +01:00
|
|
|
|
2018-11-07 21:35:59 +01:00
|
|
|
// Upload a file if it exists.
|
2017-11-03 23:10:16 +01:00
|
|
|
if msg.Extra != nil {
|
2018-12-01 19:55:35 +01:00
|
|
|
extraMsgs := helper.HandleExtra(&msg, b.General)
|
|
|
|
for i := range extraMsgs {
|
|
|
|
rmsg := &extraMsgs[i]
|
|
|
|
rmsg.Text = rmsg.Username + rmsg.Text
|
|
|
|
_, err = b.postMessage(rmsg, channelInfo)
|
2018-10-13 01:02:14 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Log.Error(err)
|
|
|
|
}
|
2017-11-03 23:10:16 +01:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
// Upload files if necessary (from Slack, Telegram or Mattermost).
|
2018-11-10 22:09:41 +01:00
|
|
|
b.uploadFile(&msg, channelInfo.ID)
|
2017-11-03 23:10:16 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
// Post message.
|
2018-12-01 19:55:35 +01:00
|
|
|
return b.postMessage(&msg, channelInfo)
|
2018-11-10 22:09:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 17:04:10 +01:00
|
|
|
func (b *Bslack) updateTopicOrPurpose(msg *config.Message, channelInfo *slack.Channel) error {
|
2018-11-26 10:47:04 +01:00
|
|
|
var updateFunc func(channelID string, value string) (*slack.Channel, error)
|
|
|
|
|
|
|
|
incomingChangeType, text := b.extractTopicOrPurpose(msg.Text)
|
|
|
|
switch incomingChangeType {
|
|
|
|
case "topic":
|
|
|
|
updateFunc = b.rtm.SetTopicOfConversation
|
|
|
|
case "purpose":
|
|
|
|
updateFunc = b.rtm.SetPurposeOfConversation
|
|
|
|
default:
|
|
|
|
b.Log.Errorf("Unhandled type received from extractTopicOrPurpose: %s", incomingChangeType)
|
2018-12-08 17:04:10 +01:00
|
|
|
return nil
|
2018-11-26 10:47:04 +01:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
_, err := updateFunc(channelInfo.ID, text)
|
|
|
|
if err == nil {
|
2018-12-08 17:04:10 +01:00
|
|
|
return nil
|
2018-11-26 10:47:04 +01:00
|
|
|
}
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
2018-12-08 17:04:10 +01:00
|
|
|
return err
|
2018-11-26 10:47:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handles updating topic/purpose and determining whether to further propagate update messages.
|
|
|
|
func (b *Bslack) handleTopicOrPurpose(msg *config.Message, channelInfo *slack.Channel) (bool, error) {
|
|
|
|
if msg.Event != config.EventTopicChange {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.GetBool("SyncTopic") {
|
2018-12-08 17:04:10 +01:00
|
|
|
return true, b.updateTopicOrPurpose(msg, channelInfo)
|
2018-11-26 10:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass along to normal message handlers.
|
|
|
|
if b.GetBool("ShowTopicChange") {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swallow message as handled no-op.
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
func (b *Bslack) deleteMessage(msg *config.Message, channelInfo *slack.Channel) (bool, error) {
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event != config.EventMsgDelete {
|
2018-11-10 22:09:41 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some protocols echo deletes, but with an empty ID.
|
|
|
|
if msg.ID == "" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2018-11-19 21:28:23 +01:00
|
|
|
_, _, err := b.rtm.DeleteMessage(channelInfo.ID, msg.ID)
|
2018-11-10 22:09:41 +01:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Failed to delete user message from Slack: %#v", err)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bslack) editMessage(msg *config.Message, channelInfo *slack.Channel) (bool, error) {
|
|
|
|
if msg.ID == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-12-01 19:55:35 +01:00
|
|
|
messageOptions := b.prepareMessageOptions(msg)
|
2018-11-10 22:09:41 +01:00
|
|
|
for {
|
2018-12-01 19:55:35 +01:00
|
|
|
messageOptions = append(messageOptions, slack.MsgOptionText(msg.Text, false))
|
|
|
|
_, _, _, err := b.rtm.UpdateMessage(channelInfo.ID, msg.ID, messageOptions...)
|
2018-11-10 22:09:41 +01:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Failed to edit user message on Slack: %#v", err)
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 19:55:35 +01:00
|
|
|
func (b *Bslack) postMessage(msg *config.Message, channelInfo *slack.Channel) (string, error) {
|
2018-11-25 00:34:14 +01:00
|
|
|
// don't post empty messages
|
|
|
|
if msg.Text == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
2018-12-01 19:55:35 +01:00
|
|
|
messageOptions := b.prepareMessageOptions(msg)
|
|
|
|
messageOptions = append(messageOptions, slack.MsgOptionText(msg.Text, false))
|
2018-11-10 22:09:41 +01:00
|
|
|
for {
|
2018-12-01 19:55:35 +01:00
|
|
|
_, id, err := b.rtm.PostMessage(channelInfo.ID, messageOptions...)
|
2018-11-10 22:09:41 +01:00
|
|
|
if err == nil {
|
2018-11-19 21:28:23 +01:00
|
|
|
return id, nil
|
2018-11-10 22:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.handleRateLimit(err); err != nil {
|
|
|
|
b.Log.Errorf("Failed to sent user message to Slack: %#v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2017-08-28 20:29:02 +02:00
|
|
|
}
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 22:09:41 +01:00
|
|
|
// uploadFile handles native upload of files
|
|
|
|
func (b *Bslack) uploadFile(msg *config.Message, channelID string) {
|
2018-11-07 21:35:59 +01:00
|
|
|
for _, f := range msg.Extra["file"] {
|
2018-11-12 15:58:00 +01:00
|
|
|
fi, ok := f.(config.FileInfo)
|
|
|
|
if !ok {
|
|
|
|
b.Log.Errorf("Received a file with unexpected content: %#v", f)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-07 21:35:59 +01:00
|
|
|
if msg.Text == fi.Comment {
|
|
|
|
msg.Text = ""
|
|
|
|
}
|
|
|
|
// Because the result of the UploadFile is slower than the MessageEvent from slack
|
|
|
|
// we can't match on the file ID yet, so we have to match on the filename too.
|
|
|
|
ts := time.Now()
|
|
|
|
b.Log.Debugf("Adding file %s to cache at %s with timestamp", fi.Name, ts.String())
|
2018-11-14 21:00:21 +01:00
|
|
|
b.cache.Add("filename"+fi.Name, ts)
|
2018-11-25 00:23:42 +01:00
|
|
|
initialComment := fmt.Sprintf("File from %s", msg.Username)
|
|
|
|
if fi.Comment != "" {
|
|
|
|
initialComment += fmt.Sprintf("with comment: %s", fi.Comment)
|
|
|
|
}
|
2018-11-07 21:35:59 +01:00
|
|
|
res, err := b.sc.UploadFile(slack.FileUploadParameters{
|
2018-11-25 00:23:42 +01:00
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
Filename: fi.Name,
|
|
|
|
Channels: []string{channelID},
|
|
|
|
InitialComment: initialComment,
|
|
|
|
ThreadTimestamp: msg.ParentID,
|
2018-11-07 21:35:59 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("uploadfile %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if res.ID != "" {
|
|
|
|
b.Log.Debugf("Adding file ID %s to cache with timestamp %s", res.ID, ts.String())
|
2018-11-14 21:00:21 +01:00
|
|
|
b.cache.Add("file"+res.ID, ts)
|
2018-11-07 21:35:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 19:55:35 +01:00
|
|
|
func (b *Bslack) prepareMessageOptions(msg *config.Message) []slack.MsgOption {
|
2018-10-22 19:43:57 +02:00
|
|
|
params := slack.NewPostMessageParameters()
|
|
|
|
if b.GetBool(useNickPrefixConfig) {
|
|
|
|
params.AsUser = true
|
|
|
|
}
|
|
|
|
params.Username = msg.Username
|
|
|
|
params.LinkNames = 1 // replace mentions
|
|
|
|
params.IconURL = config.GetIconURL(msg, b.GetString(iconURLConfig))
|
2018-11-19 21:28:23 +01:00
|
|
|
params.ThreadTimestamp = msg.ParentID
|
2018-10-22 19:43:57 +02:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
params.IconURL = msg.Avatar
|
|
|
|
}
|
2018-12-01 19:55:35 +01:00
|
|
|
|
|
|
|
var attachments []slack.Attachment
|
2018-10-22 19:43:57 +02:00
|
|
|
// add a callback ID so we can see we created it
|
2018-12-01 19:55:35 +01:00
|
|
|
attachments = append(attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid})
|
2018-10-22 19:43:57 +02:00
|
|
|
// add file attachments
|
2018-12-01 19:55:35 +01:00
|
|
|
attachments = append(attachments, b.createAttach(msg.Extra)...)
|
2018-10-22 19:43:57 +02:00
|
|
|
// add slack attachments (from another slack bridge)
|
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, attach := range msg.Extra[sSlackAttachment] {
|
2018-12-01 19:55:35 +01:00
|
|
|
attachments = append(attachments, attach.([]slack.Attachment)...)
|
2018-10-22 19:43:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-01 19:55:35 +01:00
|
|
|
|
|
|
|
var opts []slack.MsgOption
|
|
|
|
opts = append(opts, slack.MsgOptionAttachments(attachments...))
|
|
|
|
opts = append(opts, slack.MsgOptionPostMessageParameters(params))
|
|
|
|
return opts
|
2018-03-04 23:52:14 +01:00
|
|
|
}
|
|
|
|
|
2017-09-21 22:35:21 +02:00
|
|
|
func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment {
|
2018-10-13 01:02:14 +02:00
|
|
|
var attachements []slack.Attachment
|
2017-09-21 22:35:21 +02:00
|
|
|
for _, v := range extra["attachments"] {
|
2017-09-19 00:04:27 +02:00
|
|
|
entry := v.(map[string]interface{})
|
2018-10-13 01:02:14 +02:00
|
|
|
s := slack.Attachment{
|
|
|
|
Fallback: extractStringField(entry, "fallback"),
|
|
|
|
Color: extractStringField(entry, "color"),
|
|
|
|
Pretext: extractStringField(entry, "pretext"),
|
|
|
|
AuthorName: extractStringField(entry, "author_name"),
|
|
|
|
AuthorLink: extractStringField(entry, "author_link"),
|
|
|
|
AuthorIcon: extractStringField(entry, "author_icon"),
|
|
|
|
Title: extractStringField(entry, "title"),
|
|
|
|
TitleLink: extractStringField(entry, "title_link"),
|
|
|
|
Text: extractStringField(entry, "text"),
|
|
|
|
ImageURL: extractStringField(entry, "image_url"),
|
|
|
|
ThumbURL: extractStringField(entry, "thumb_url"),
|
|
|
|
Footer: extractStringField(entry, "footer"),
|
|
|
|
FooterIcon: extractStringField(entry, "footer_icon"),
|
|
|
|
}
|
|
|
|
attachements = append(attachements, s)
|
|
|
|
}
|
|
|
|
return attachements
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractStringField(data map[string]interface{}, field string) string {
|
|
|
|
if rawValue, found := data[field]; found {
|
|
|
|
if value, ok := rawValue.(string); ok {
|
|
|
|
return value
|
|
|
|
}
|
2017-09-18 23:51:27 +02:00
|
|
|
}
|
2018-10-13 01:02:14 +02:00
|
|
|
return ""
|
2017-09-18 23:51:27 +02:00
|
|
|
}
|