2016-09-05 16:34:37 +02:00
|
|
|
package bslack
|
|
|
|
|
|
|
|
import (
|
2017-09-21 22:35:21 +02:00
|
|
|
"bytes"
|
2017-07-15 16:49:47 +02:00
|
|
|
"errors"
|
2016-10-08 21:57:03 +02:00
|
|
|
"fmt"
|
2016-09-05 16:34:37 +02:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
|
|
|
"github.com/42wim/matterbridge/matterhook"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2018-01-08 22:41:58 +01:00
|
|
|
"github.com/nlopes/slack"
|
2017-07-09 14:26:35 +02:00
|
|
|
"html"
|
2017-09-21 22:35:21 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2016-11-20 22:40:09 +01:00
|
|
|
"regexp"
|
2016-09-05 16:34:37 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MMMessage struct {
|
|
|
|
Text string
|
|
|
|
Channel string
|
|
|
|
Username string
|
2017-06-18 15:44:54 +02:00
|
|
|
UserID string
|
2016-11-04 00:05:15 +01:00
|
|
|
Raw *slack.MessageEvent
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
type Bslack struct {
|
2016-09-20 00:13:57 +02:00
|
|
|
mh *matterhook.Client
|
|
|
|
sc *slack.Client
|
2016-09-05 16:34:37 +02:00
|
|
|
rtm *slack.RTM
|
|
|
|
Plus bool
|
2016-11-06 00:46:32 +01:00
|
|
|
Users []slack.User
|
2016-11-04 00:05:15 +01:00
|
|
|
si *slack.Info
|
2016-09-05 16:34:37 +02:00
|
|
|
channels []slack.Channel
|
2017-12-19 23:15:03 +01:00
|
|
|
*config.BridgeConfig
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var flog *log.Entry
|
2016-09-18 19:21:15 +02:00
|
|
|
var protocol = "slack"
|
2016-09-05 16:34:37 +02:00
|
|
|
|
|
|
|
func init() {
|
2016-09-18 19:21:15 +02:00
|
|
|
flog = log.WithFields(log.Fields{"module": protocol})
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 23:15:03 +01:00
|
|
|
func New(cfg *config.BridgeConfig) *Bslack {
|
|
|
|
return &Bslack{BridgeConfig: cfg}
|
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 {
|
2017-07-15 16:49:47 +02:00
|
|
|
if b.Config.WebhookBindAddress != "" {
|
|
|
|
if b.Config.WebhookURL != "" {
|
|
|
|
flog.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
|
|
|
b.mh = matterhook.New(b.Config.WebhookURL,
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
BindAddress: b.Config.WebhookBindAddress})
|
|
|
|
} else if b.Config.Token != "" {
|
|
|
|
flog.Info("Connecting using token (sending)")
|
|
|
|
b.sc = slack.New(b.Config.Token)
|
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
|
|
|
flog.Info("Connecting using webhookbindaddress (receiving)")
|
|
|
|
b.mh = matterhook.New(b.Config.WebhookURL,
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
BindAddress: b.Config.WebhookBindAddress})
|
|
|
|
} else {
|
|
|
|
flog.Info("Connecting using webhookbindaddress (receiving)")
|
|
|
|
b.mh = matterhook.New(b.Config.WebhookURL,
|
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
BindAddress: b.Config.WebhookBindAddress})
|
|
|
|
}
|
|
|
|
go b.handleSlack()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.Config.WebhookURL != "" {
|
|
|
|
flog.Info("Connecting using webhookurl (sending)")
|
2017-06-24 19:36:10 +02:00
|
|
|
b.mh = matterhook.New(b.Config.WebhookURL,
|
2017-07-15 16:49:47 +02:00
|
|
|
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
|
|
|
DisableServer: true})
|
|
|
|
if b.Config.Token != "" {
|
|
|
|
flog.Info("Connecting using token (receiving)")
|
|
|
|
b.sc = slack.New(b.Config.Token)
|
|
|
|
b.rtm = b.sc.NewRTM()
|
|
|
|
go b.rtm.ManageConnection()
|
|
|
|
go b.handleSlack()
|
|
|
|
}
|
|
|
|
} else if b.Config.Token != "" {
|
|
|
|
flog.Info("Connecting using token (sending and receiving)")
|
2016-09-18 19:21:15 +02:00
|
|
|
b.sc = slack.New(b.Config.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()
|
|
|
|
}
|
|
|
|
if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Token == "" {
|
|
|
|
return errors.New("No connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured.")
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-14 21:12:02 +01:00
|
|
|
func (b *Bslack) Disconnect() error {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:51:41 +02:00
|
|
|
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
2016-09-20 12:20:44 +02:00
|
|
|
// we can only join channels using the API
|
2018-01-02 14:31:44 +01:00
|
|
|
if b.sc != nil {
|
2017-03-27 20:15:05 +02:00
|
|
|
if strings.HasPrefix(b.Config.Token, "xoxb") {
|
|
|
|
// TODO check if bot has already joined channel
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-12 14:51:41 +02:00
|
|
|
_, err := b.sc.JoinChannel(channel.Name)
|
2016-09-30 23:15:35 +02:00
|
|
|
if err != nil {
|
2017-04-17 18:01:24 +02:00
|
|
|
if err.Error() != "name_taken" {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-20 12:20:44 +02:00
|
|
|
}
|
2016-09-18 19:21:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-27 22:59:37 +02:00
|
|
|
func (b *Bslack) Send(msg config.Message) (string, error) {
|
2016-09-20 00:13:57 +02:00
|
|
|
flog.Debugf("Receiving %#v", msg)
|
2017-07-30 17:48:23 +02:00
|
|
|
if msg.Event == config.EVENT_USER_ACTION {
|
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2016-11-13 23:06:37 +01:00
|
|
|
nick := msg.Username
|
2016-11-05 01:11:28 +01:00
|
|
|
message := msg.Text
|
|
|
|
channel := msg.Channel
|
2016-09-18 19:21:15 +02:00
|
|
|
if b.Config.PrefixMessagesWithNick {
|
2016-09-05 16:34:37 +02:00
|
|
|
message = nick + " " + message
|
|
|
|
}
|
2017-06-24 19:36:10 +02:00
|
|
|
if b.Config.WebhookURL != "" {
|
2016-09-18 19:21:15 +02:00
|
|
|
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
2016-09-05 16:34:37 +02:00
|
|
|
matterMessage.Channel = channel
|
|
|
|
matterMessage.UserName = nick
|
2016-11-05 01:11:28 +01:00
|
|
|
matterMessage.Type = ""
|
2016-09-05 16:34:37 +02:00
|
|
|
matterMessage.Text = message
|
|
|
|
err := b.mh.Send(matterMessage)
|
|
|
|
if err != nil {
|
|
|
|
flog.Info(err)
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", err
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", nil
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
2016-10-08 21:57:03 +02:00
|
|
|
schannel, err := b.getChannelByName(channel)
|
|
|
|
if err != nil {
|
2017-08-27 22:59:37 +02:00
|
|
|
return "", err
|
2016-10-08 21:57:03 +02:00
|
|
|
}
|
2016-11-05 01:11:28 +01:00
|
|
|
np := slack.NewPostMessageParameters()
|
2017-07-14 00:35:01 +02:00
|
|
|
if b.Config.PrefixMessagesWithNick {
|
2016-11-05 01:11:28 +01:00
|
|
|
np.AsUser = true
|
|
|
|
}
|
|
|
|
np.Username = nick
|
2017-12-19 23:15:03 +01:00
|
|
|
np.IconURL = config.GetIconURL(&msg, &b.Config)
|
2016-11-06 00:46:32 +01:00
|
|
|
if msg.Avatar != "" {
|
|
|
|
np.IconURL = msg.Avatar
|
|
|
|
}
|
2017-07-17 21:28:31 +02:00
|
|
|
np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge"})
|
2017-09-18 23:51:27 +02:00
|
|
|
np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...)
|
|
|
|
|
2017-08-29 23:34:50 +02:00
|
|
|
// replace mentions
|
|
|
|
np.LinkNames = 1
|
|
|
|
|
2017-09-11 22:45:15 +02:00
|
|
|
if msg.Event == config.EVENT_MSG_DELETE {
|
|
|
|
// some protocols echo deletes, but with empty ID
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
// we get a "slack <ID>", split it
|
|
|
|
ts := strings.Fields(msg.ID)
|
|
|
|
b.sc.DeleteMessage(schannel.ID, ts[1])
|
|
|
|
return "", nil
|
|
|
|
}
|
2017-08-28 20:29:02 +02:00
|
|
|
// if we have no ID it means we're creating a new message, not updating an existing one
|
|
|
|
if msg.ID != "" {
|
|
|
|
ts := strings.Fields(msg.ID)
|
|
|
|
b.sc.UpdateMessage(schannel.ID, ts[1], message)
|
|
|
|
return "", nil
|
|
|
|
}
|
2017-11-03 23:10:16 +01:00
|
|
|
|
|
|
|
if msg.Extra != nil {
|
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
var err error
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
_, err = b.sc.UploadFile(slack.FileUploadParameters{
|
2017-11-13 00:20:31 +01:00
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
Filename: fi.Name,
|
|
|
|
Channels: []string{schannel.ID},
|
|
|
|
InitialComment: fi.Comment,
|
2017-11-03 23:10:16 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("uploadfile %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:29:02 +02:00
|
|
|
_, id, err := b.sc.PostMessage(schannel.ID, message, np)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "slack " + id, nil
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2016-11-06 00:46:32 +01:00
|
|
|
func (b *Bslack) getAvatar(user string) string {
|
|
|
|
var avatar string
|
|
|
|
if b.Users != nil {
|
|
|
|
for _, u := range b.Users {
|
|
|
|
if user == u.Name {
|
|
|
|
return u.Profile.Image48
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return avatar
|
|
|
|
}
|
|
|
|
|
2016-10-08 21:57:03 +02:00
|
|
|
func (b *Bslack) getChannelByName(name string) (*slack.Channel, error) {
|
2016-09-05 16:34:37 +02:00
|
|
|
if b.channels == nil {
|
2016-11-13 23:06:37 +01:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, name)
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.Name == name {
|
2016-10-08 21:57:03 +02:00
|
|
|
return &channel, nil
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-13 23:06:37 +01:00
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.Account, name)
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
|
2017-02-03 16:40:15 +01:00
|
|
|
func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
|
|
|
|
if b.channels == nil {
|
|
|
|
return nil, fmt.Errorf("%s: channel %s not found (no channels found)", b.Account, ID)
|
|
|
|
}
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
if channel.ID == ID {
|
|
|
|
return &channel, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%s: channel %s not found", b.Account, ID)
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleSlack() {
|
2016-09-05 16:34:37 +02:00
|
|
|
mchan := make(chan *MMMessage)
|
2017-07-15 16:49:47 +02:00
|
|
|
if b.Config.WebhookBindAddress != "" {
|
2017-06-24 19:36:10 +02:00
|
|
|
flog.Debugf("Choosing webhooks based receiving")
|
2016-09-05 16:34:37 +02:00
|
|
|
go b.handleMatterHook(mchan)
|
2017-06-24 19:36:10 +02:00
|
|
|
} else {
|
|
|
|
flog.Debugf("Choosing token based receiving")
|
|
|
|
go b.handleSlackClient(mchan)
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2016-09-20 00:13:57 +02:00
|
|
|
flog.Debug("Start listening for Slack messages")
|
2016-09-05 16:34:37 +02:00
|
|
|
for message := range mchan {
|
2016-11-04 00:05:15 +01:00
|
|
|
// do not send messages from ourself
|
2017-07-17 21:28:31 +02:00
|
|
|
if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" && message.Username == b.si.User.Name {
|
2016-11-04 00:05:15 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-09-11 22:45:15 +02:00
|
|
|
if (message.Text == "" || message.Username == "") && message.Raw.SubType != "message_deleted" {
|
2017-07-22 20:03:40 +02:00
|
|
|
continue
|
|
|
|
}
|
2017-09-10 18:19:33 +02:00
|
|
|
text := message.Text
|
|
|
|
text = b.replaceURL(text)
|
|
|
|
text = html.UnescapeString(text)
|
|
|
|
flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.Account)
|
2017-09-21 22:35:21 +02:00
|
|
|
msg := config.Message{Text: text, Username: message.Username, Channel: message.Channel, Account: b.Account, Avatar: b.getAvatar(message.Username), UserID: message.UserID, ID: "slack " + message.Raw.Timestamp, Extra: make(map[string][]interface{})}
|
2017-09-10 18:19:33 +02:00
|
|
|
if message.Raw.SubType == "me_message" {
|
|
|
|
msg.Event = config.EVENT_USER_ACTION
|
|
|
|
}
|
|
|
|
if message.Raw.SubType == "channel_leave" || message.Raw.SubType == "channel_join" {
|
|
|
|
msg.Username = "system"
|
|
|
|
msg.Event = config.EVENT_JOIN_LEAVE
|
|
|
|
}
|
|
|
|
// edited messages have a submessage, use this timestamp
|
|
|
|
if message.Raw.SubMessage != nil {
|
|
|
|
msg.ID = "slack " + message.Raw.SubMessage.Timestamp
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
2017-09-11 22:45:15 +02:00
|
|
|
if message.Raw.SubType == "message_deleted" {
|
|
|
|
msg.Text = config.EVENT_MSG_DELETE
|
|
|
|
msg.Event = config.EVENT_MSG_DELETE
|
|
|
|
msg.ID = "slack " + message.Raw.DeletedTimestamp
|
|
|
|
}
|
2018-02-02 21:04:43 +01:00
|
|
|
if message.Raw.SubType == "channel_topic" {
|
|
|
|
msg.Event = config.EVENT_TOPIC_CHANGE
|
|
|
|
}
|
2017-09-21 22:35:21 +02:00
|
|
|
|
|
|
|
// if we have a file attached, download it (in memory) and put a pointer to it in msg.Extra
|
|
|
|
if message.Raw.File != nil {
|
|
|
|
// limit to 1MB for now
|
2017-12-19 23:44:13 +01:00
|
|
|
if message.Raw.File.Size <= b.General.MediaDownloadSize {
|
2017-11-13 00:20:31 +01:00
|
|
|
comment := ""
|
2017-09-21 22:35:21 +02:00
|
|
|
data, err := b.downloadFile(message.Raw.File.URLPrivateDownload)
|
|
|
|
if err != nil {
|
|
|
|
flog.Errorf("download %s failed %#v", message.Raw.File.URLPrivateDownload, err)
|
|
|
|
} else {
|
2017-11-13 00:20:31 +01:00
|
|
|
results := regexp.MustCompile(`.*?commented: (.*)`).FindAllStringSubmatch(msg.Text, -1)
|
|
|
|
if len(results) > 0 {
|
|
|
|
comment = results[0][1]
|
|
|
|
}
|
|
|
|
msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: message.Raw.File.Name, Data: data, Comment: comment})
|
2017-09-21 22:35:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 19:50:18 +01:00
|
|
|
flog.Debugf("Message is %#v", msg)
|
|
|
|
b.Remote <- msg
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
|
2016-09-05 16:34:37 +02:00
|
|
|
for msg := range b.rtm.IncomingEvents {
|
2018-01-02 14:39:27 +01:00
|
|
|
if msg.Type != "user_typing" && msg.Type != "latency_report" {
|
|
|
|
flog.Debugf("Receiving from slackclient %#v", msg.Data)
|
|
|
|
}
|
2016-09-05 16:34:37 +02:00
|
|
|
switch ev := msg.Data.(type) {
|
|
|
|
case *slack.MessageEvent:
|
2017-09-07 23:47:23 +02:00
|
|
|
if len(ev.Attachments) > 0 {
|
|
|
|
// skip messages we made ourselves
|
|
|
|
if ev.Attachments[0].CallbackID == "matterbridge" {
|
|
|
|
continue
|
2017-04-16 00:16:24 +02:00
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
}
|
|
|
|
if !b.Config.EditDisable && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
|
|
|
flog.Debugf("SubMessage %#v", ev.SubMessage)
|
|
|
|
ev.User = ev.SubMessage.User
|
|
|
|
ev.Text = ev.SubMessage.Text + b.Config.EditSuffix
|
2017-09-19 22:33:26 +02:00
|
|
|
|
|
|
|
// it seems ev.SubMessage.Edited == nil when slack unfurls
|
|
|
|
// do not forward these messages #266
|
|
|
|
if ev.SubMessage.Edited == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
}
|
|
|
|
// use our own func because rtm.GetChannelInfo doesn't work for private channels
|
|
|
|
channel, err := b.getChannelByID(ev.Channel)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m := &MMMessage{}
|
2018-02-02 23:16:10 +01:00
|
|
|
if ev.BotID == "" && ev.SubType != "message_deleted" && ev.SubType != "file_comment" {
|
2017-09-07 23:47:23 +02:00
|
|
|
user, err := b.rtm.GetUserInfo(ev.User)
|
2016-10-29 21:05:33 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
m.UserID = user.ID
|
|
|
|
m.Username = user.Name
|
2017-11-02 17:11:42 +01:00
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
m.Username = user.Profile.DisplayName
|
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
}
|
|
|
|
m.Channel = channel.Name
|
|
|
|
m.Text = ev.Text
|
|
|
|
if m.Text == "" {
|
|
|
|
for _, attach := range ev.Attachments {
|
|
|
|
if attach.Text != "" {
|
|
|
|
m.Text = attach.Text
|
|
|
|
} else {
|
|
|
|
m.Text = attach.Fallback
|
2017-07-16 14:18:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
}
|
|
|
|
m.Raw = ev
|
|
|
|
m.Text = b.replaceMention(m.Text)
|
2017-11-03 20:32:28 +01:00
|
|
|
m.Text = b.replaceVariable(m.Text)
|
2017-11-02 21:21:46 +01:00
|
|
|
m.Text = b.replaceChannel(m.Text)
|
2017-09-07 23:47:23 +02:00
|
|
|
// when using webhookURL we can't check if it's our webhook or not for now
|
|
|
|
if ev.BotID != "" && b.Config.WebhookURL == "" {
|
|
|
|
bot, err := b.rtm.GetBotInfo(ev.BotID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2017-07-16 18:08:26 +02:00
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
if bot.Name != "" {
|
|
|
|
m.Username = bot.Name
|
2017-10-12 20:33:37 +02:00
|
|
|
if ev.Username != "" {
|
|
|
|
m.Username = ev.Username
|
|
|
|
}
|
2017-09-07 23:47:23 +02:00
|
|
|
m.UserID = bot.ID
|
2017-07-10 21:58:43 +02:00
|
|
|
}
|
2016-09-05 16:34:37 +02:00
|
|
|
}
|
2018-02-02 23:16:10 +01:00
|
|
|
|
|
|
|
if ev.SubType == "file_comment" {
|
|
|
|
m.Username = "system"
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:47:23 +02:00
|
|
|
mchan <- m
|
2016-09-05 16:34:37 +02:00
|
|
|
case *slack.OutgoingErrorEvent:
|
|
|
|
flog.Debugf("%#v", ev.Error())
|
2016-11-20 22:40:09 +01:00
|
|
|
case *slack.ChannelJoinedEvent:
|
|
|
|
b.Users, _ = b.sc.GetUsers()
|
2016-09-05 16:34:37 +02:00
|
|
|
case *slack.ConnectedEvent:
|
|
|
|
b.channels = ev.Info.Channels
|
2016-11-04 00:05:15 +01:00
|
|
|
b.si = ev.Info
|
2016-11-06 00:46:32 +01:00
|
|
|
b.Users, _ = b.sc.GetUsers()
|
2017-01-28 00:36:53 +01:00
|
|
|
// add private channels
|
|
|
|
groups, _ := b.sc.GetGroups(true)
|
|
|
|
for _, g := range groups {
|
|
|
|
channel := new(slack.Channel)
|
|
|
|
channel.ID = g.ID
|
|
|
|
channel.Name = g.Name
|
|
|
|
b.channels = append(b.channels, *channel)
|
|
|
|
}
|
2016-09-05 16:34:37 +02:00
|
|
|
case *slack.InvalidAuthEvent:
|
|
|
|
flog.Fatalf("Invalid Token %#v", ev)
|
2018-01-03 14:06:28 +01:00
|
|
|
case *slack.ConnectionErrorEvent:
|
|
|
|
flog.Errorf("Connection failed %#v %#v", ev.Error(), ev.ErrorObj)
|
2016-09-05 16:34:37 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:21:15 +02:00
|
|
|
func (b *Bslack) handleMatterHook(mchan chan *MMMessage) {
|
2016-09-05 16:34:37 +02:00
|
|
|
for {
|
|
|
|
message := b.mh.Receive()
|
2016-09-20 00:13:57 +02:00
|
|
|
flog.Debugf("receiving from matterhook (slack) %#v", message)
|
2016-09-05 16:34:37 +02:00
|
|
|
m := &MMMessage{}
|
|
|
|
m.Username = message.UserName
|
|
|
|
m.Text = message.Text
|
2016-11-20 22:40:09 +01:00
|
|
|
m.Text = b.replaceMention(m.Text)
|
2017-11-03 20:32:28 +01:00
|
|
|
m.Text = b.replaceVariable(m.Text)
|
2017-11-02 21:21:46 +01:00
|
|
|
m.Text = b.replaceChannel(m.Text)
|
2016-09-05 16:34:37 +02:00
|
|
|
m.Channel = message.ChannelName
|
2017-02-17 23:13:02 +01:00
|
|
|
if m.Username == "slackbot" {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-05 16:34:37 +02:00
|
|
|
mchan <- m
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 22:40:09 +01:00
|
|
|
|
|
|
|
func (b *Bslack) userName(id string) string {
|
|
|
|
for _, u := range b.Users {
|
|
|
|
if u.ID == id {
|
2017-11-02 17:11:42 +01:00
|
|
|
if u.Profile.DisplayName != "" {
|
|
|
|
return u.Profile.DisplayName
|
|
|
|
}
|
2016-11-20 22:40:09 +01:00
|
|
|
return u.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-03 20:32:28 +01:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
2016-11-20 22:40:09 +01:00
|
|
|
func (b *Bslack) replaceMention(text string) string {
|
|
|
|
results := regexp.MustCompile(`<@([a-zA-z0-9]+)>`).FindAllStringSubmatch(text, -1)
|
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, "<@"+r[1]+">", "@"+b.userName(r[1]), -1)
|
2017-11-02 21:21:46 +01:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2016-11-20 22:40:09 +01:00
|
|
|
|
2017-11-03 20:32:28 +01:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
|
2017-11-02 21:21:46 +01:00
|
|
|
func (b *Bslack) replaceChannel(text string) string {
|
|
|
|
results := regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`).FindAllStringSubmatch(text, -1)
|
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, r[0], "#"+r[1], -1)
|
2016-11-20 22:40:09 +01:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2017-06-26 22:16:19 +02:00
|
|
|
|
2017-11-03 20:32:28 +01:00
|
|
|
// @see https://api.slack.com/docs/message-formatting#variables
|
|
|
|
func (b *Bslack) replaceVariable(text string) string {
|
|
|
|
results := regexp.MustCompile(`<!([a-zA-Z0-9]+)(\|.+?)?>`).FindAllStringSubmatch(text, -1)
|
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, r[0], "@"+r[1], -1)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
// @see https://api.slack.com/docs/message-formatting#linking_to_urls
|
2017-06-26 22:16:19 +02:00
|
|
|
func (b *Bslack) replaceURL(text string) string {
|
2017-10-26 21:58:43 +02:00
|
|
|
results := regexp.MustCompile(`<(.*?)(\|.*?)?>`).FindAllStringSubmatch(text, -1)
|
2017-06-26 22:16:19 +02:00
|
|
|
for _, r := range results {
|
|
|
|
text = strings.Replace(text, r[0], r[1], -1)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
2017-09-18 23:51:27 +02:00
|
|
|
|
2017-09-21 22:35:21 +02:00
|
|
|
func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment {
|
2017-09-18 23:51:27 +02:00
|
|
|
var attachs []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{})
|
|
|
|
s := slack.Attachment{}
|
|
|
|
s.Fallback = entry["fallback"].(string)
|
|
|
|
s.Color = entry["color"].(string)
|
|
|
|
s.Pretext = entry["pretext"].(string)
|
|
|
|
s.AuthorName = entry["author_name"].(string)
|
|
|
|
s.AuthorLink = entry["author_link"].(string)
|
|
|
|
s.AuthorIcon = entry["author_icon"].(string)
|
|
|
|
s.Title = entry["title"].(string)
|
|
|
|
s.TitleLink = entry["title_link"].(string)
|
|
|
|
s.Text = entry["text"].(string)
|
|
|
|
s.ImageURL = entry["image_url"].(string)
|
|
|
|
s.ThumbURL = entry["thumb_url"].(string)
|
|
|
|
s.Footer = entry["footer"].(string)
|
|
|
|
s.FooterIcon = entry["footer_icon"].(string)
|
|
|
|
attachs = append(attachs, s)
|
2017-09-18 23:51:27 +02:00
|
|
|
}
|
|
|
|
return attachs
|
|
|
|
}
|
2017-09-21 22:35:21 +02:00
|
|
|
|
|
|
|
func (b *Bslack) downloadFile(url string) (*[]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("Authorization", "Bearer "+b.Config.Token)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
data := buf.Bytes()
|
|
|
|
resp.Body.Close()
|
|
|
|
return &data, nil
|
|
|
|
}
|