2019-02-06 03:52:49 +00:00
|
|
|
package slackapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-18 23:45:23 +00:00
|
|
|
"container/ring"
|
2019-02-06 03:52:49 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
2019-02-08 19:54:40 +00:00
|
|
|
"io/ioutil"
|
2019-02-06 03:52:49 +00:00
|
|
|
"net/http"
|
2019-02-08 19:54:40 +00:00
|
|
|
"net/url"
|
2019-06-07 16:19:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-02-06 03:52:49 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-06-07 16:19:24 +00:00
|
|
|
"text/template"
|
2019-02-06 03:52:49 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-21 17:40:49 +00:00
|
|
|
zerowidth "github.com/trubitsyn/go-zero-width"
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2020-05-08 21:05:01 +00:00
|
|
|
"github.com/slack-go/slack"
|
|
|
|
"github.com/slack-go/slack/slackevents"
|
2019-02-06 03:52:49 +00:00
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
const DefaultIDRing = 10
|
|
|
|
const DefaultMsgRing = 50
|
2019-06-07 18:23:14 +00:00
|
|
|
const defaultLogFormat = "[{{fixDate .Time \"2006-01-02 15:04:05\"}}] {{if .TopicChange}}*** {{.User.Name}}{{else if .Action}}* {{.User.Name}}{{else}}<{{.User.Name}}>{{end}} {{.Body}}\n"
|
2019-06-07 16:19:24 +00:00
|
|
|
|
|
|
|
// 11:10AM DBG connectors/slackapp/slackApp.go:496 > Slack event dir=logs raw={"Action":false,"AdditionalData":
|
|
|
|
// {"RAW_SLACK_TIMESTAMP":"1559920235.001100"},"Body":"aoeu","Channel":"C0S04SMRC","ChannelName":"test",
|
|
|
|
// "Command":false,"Host":"","IsIM":false,"Raw":{"channel":"C0S04SMRC","channel_type":"channel",
|
|
|
|
// "event_ts":1559920235.001100,"files":null,"text":"aoeu","thread_ts":"","ts":"1559920235.001100",
|
|
|
|
// "type":"message","upload":false,"user":"U0RLUDELD"},"Time":"2019-06-07T11:10:35.0000011-04:00",
|
|
|
|
// "User":{"Admin":false,"ID":"U0RLUDELD","Name":"flyngpngn"}}
|
2019-02-18 23:45:23 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
type SlackApp struct {
|
|
|
|
config *config.Config
|
|
|
|
api *slack.Client
|
|
|
|
|
2019-02-06 05:17:32 +00:00
|
|
|
botToken string
|
|
|
|
userToken string
|
2019-02-06 03:52:49 +00:00
|
|
|
verification string
|
|
|
|
id string
|
|
|
|
|
|
|
|
lastRecieved time.Time
|
|
|
|
|
2019-06-07 16:19:24 +00:00
|
|
|
myBotID string
|
|
|
|
users map[string]*slack.User
|
|
|
|
emoji map[string]string
|
|
|
|
channels map[string]*slack.Channel
|
2019-02-06 03:52:49 +00:00
|
|
|
|
|
|
|
event bot.Callback
|
2019-02-18 23:45:23 +00:00
|
|
|
|
|
|
|
msgIDBuffer *ring.Ring
|
2019-09-19 20:30:59 +00:00
|
|
|
msgBuffer *ring.Ring
|
2019-06-07 16:19:24 +00:00
|
|
|
|
|
|
|
logFormat *template.Template
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 16:19:24 +00:00
|
|
|
func fixDate(input time.Time, format string) string {
|
|
|
|
return input.Format(format)
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
func New(c *config.Config) *SlackApp {
|
|
|
|
token := c.Get("slack.token", "NONE")
|
|
|
|
if token == "NONE" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Msg("No slack token found. Set SLACKTOKEN env.")
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 18:22:54 +00:00
|
|
|
api := slack.New(token, slack.OptionDebug(false))
|
2019-02-06 03:52:49 +00:00
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
tplTxt := c.GetString("slackapp.logMessage.format", defaultLogFormat)
|
2019-06-07 16:19:24 +00:00
|
|
|
funcs := template.FuncMap{
|
|
|
|
"fixDate": fixDate,
|
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
tpl := template.Must(template.New("logMessage").Funcs(funcs).Parse(tplTxt))
|
2019-06-07 16:19:24 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
return &SlackApp{
|
|
|
|
api: api,
|
|
|
|
config: c,
|
2019-02-06 05:17:32 +00:00
|
|
|
botToken: token,
|
|
|
|
userToken: c.Get("slack.usertoken", "NONE"),
|
2019-02-06 03:52:49 +00:00
|
|
|
verification: c.Get("slack.verification", "NONE"),
|
2019-02-06 05:17:32 +00:00
|
|
|
myBotID: c.Get("slack.botid", ""),
|
2019-02-06 03:52:49 +00:00
|
|
|
lastRecieved: time.Now(),
|
2019-06-07 16:19:24 +00:00
|
|
|
users: make(map[string]*slack.User),
|
2019-02-06 03:52:49 +00:00
|
|
|
emoji: make(map[string]string),
|
2019-06-07 16:19:24 +00:00
|
|
|
channels: make(map[string]*slack.Channel),
|
2019-09-19 20:30:59 +00:00
|
|
|
msgIDBuffer: ring.New(c.GetInt("idRingSize", DefaultIDRing)),
|
|
|
|
msgBuffer: ring.New(c.GetInt("msgRingSize", DefaultMsgRing)),
|
2019-06-07 16:19:24 +00:00
|
|
|
logFormat: tpl,
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) RegisterEvent(f bot.Callback) {
|
|
|
|
s.event = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) Serve() error {
|
|
|
|
s.populateEmojiList()
|
2019-02-06 05:17:32 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
http.HandleFunc("/evt", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r.Body)
|
|
|
|
body := buf.String()
|
|
|
|
eventsAPIEvent, e := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionVerifyToken(&slackevents.TokenComparator{VerificationToken: s.verification}))
|
|
|
|
if e != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(e)
|
2019-02-06 03:52:49 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if eventsAPIEvent.Type == slackevents.URLVerification {
|
|
|
|
var r *slackevents.ChallengeResponse
|
|
|
|
err := json.Unmarshal([]byte(body), &r)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err)
|
2019-02-06 03:52:49 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text")
|
|
|
|
w.Write([]byte(r.Challenge))
|
|
|
|
} else if eventsAPIEvent.Type == slackevents.CallbackEvent {
|
|
|
|
innerEvent := eventsAPIEvent.InnerEvent
|
2020-07-24 14:50:56 +00:00
|
|
|
typ := innerEvent.Type
|
2019-02-06 03:52:49 +00:00
|
|
|
switch ev := innerEvent.Data.(type) {
|
2019-09-19 20:30:59 +00:00
|
|
|
case *slackevents.MessageAction:
|
|
|
|
log.Debug().Interface("ev", ev).Msg("MessageAction")
|
|
|
|
case *slackevents.MessageActionResponse:
|
|
|
|
log.Debug().Interface("ev", ev).Msg("MessageActionResponse")
|
2019-02-06 03:52:49 +00:00
|
|
|
case *slackevents.AppMentionEvent:
|
|
|
|
// This is a bit of a problem. AppMentionEvent also needs to
|
|
|
|
// End up in msgReceived
|
|
|
|
//s.msgReceivd(ev)
|
|
|
|
case *slackevents.MessageEvent:
|
|
|
|
s.msgReceivd(ev)
|
2020-07-24 14:50:56 +00:00
|
|
|
case *slackevents.ReactionAddedEvent:
|
|
|
|
err := s.reactionReceived(ev)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error with reaction recording")
|
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
default:
|
|
|
|
log.Debug().
|
|
|
|
Interface("ev", ev).
|
2020-07-24 14:50:56 +00:00
|
|
|
Interface("type", typ).
|
2019-09-19 20:30:59 +00:00
|
|
|
Msg("Unknown CallbackEvent")
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-02-08 19:54:40 +00:00
|
|
|
} else {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("type", eventsAPIEvent.Type).
|
|
|
|
Interface("event", eventsAPIEvent).
|
|
|
|
Msg("event")
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:45:23 +00:00
|
|
|
// checkRingOrAdd returns true if it finds the ts value
|
|
|
|
// or false if the ts isn't yet in the ring (and adds it)
|
|
|
|
func (s *SlackApp) checkRingOrAdd(ts string) bool {
|
|
|
|
found := false
|
|
|
|
s.msgIDBuffer.Do(func(p interface{}) {
|
2019-09-19 20:30:59 +00:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-02-18 23:45:23 +00:00
|
|
|
if p.(string) == ts {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
s.msgIDBuffer.Value = ts
|
|
|
|
s.msgIDBuffer = s.msgIDBuffer.Next()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) {
|
2019-03-11 22:11:36 +00:00
|
|
|
if msg.TimeStamp == "" {
|
|
|
|
log.Debug().
|
|
|
|
Str("type", msg.SubType).
|
|
|
|
Msg("ignoring an unhandled event type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().
|
2019-06-06 14:33:50 +00:00
|
|
|
Interface("event", msg).
|
2019-03-11 22:11:36 +00:00
|
|
|
Str("type", msg.SubType).
|
|
|
|
Msg("accepting a message")
|
|
|
|
|
2019-02-18 23:45:23 +00:00
|
|
|
if s.checkRingOrAdd(msg.TimeStamp) {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("ts", msg.TimeStamp).
|
|
|
|
Msg("Got a duplicate message from server")
|
2019-02-18 23:45:23 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-07 16:19:24 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
isItMe := msg.BotID != "" && msg.BotID == s.myBotID
|
2019-06-07 20:49:18 +00:00
|
|
|
m := s.buildMessage(msg)
|
|
|
|
if m.Time.Before(s.lastRecieved) {
|
|
|
|
log.Debug().
|
|
|
|
Time("ts", m.Time).
|
|
|
|
Interface("lastRecv", s.lastRecieved).
|
|
|
|
Msg("Ignoring message")
|
|
|
|
return
|
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
if err := s.logMessage(m); err != nil {
|
2019-06-07 20:49:18 +00:00
|
|
|
log.Fatal().Err(err).Msg("Error logging message")
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
if !isItMe && msg.ThreadTimeStamp == "" {
|
2019-06-07 20:49:18 +00:00
|
|
|
s.lastRecieved = m.Time
|
|
|
|
s.event(s, bot.Message, m)
|
2019-02-06 03:52:49 +00:00
|
|
|
} else if msg.ThreadTimeStamp != "" {
|
|
|
|
//we're throwing away some information here by not parsing the correct reply object type, but that's okay
|
2019-05-27 23:21:53 +00:00
|
|
|
s.event(s, bot.Reply, s.buildMessage(msg), msg.ThreadTimeStamp)
|
2019-06-07 20:49:18 +00:00
|
|
|
} else if isItMe {
|
|
|
|
s.event(s, bot.SelfMessage, m)
|
2019-02-06 03:52:49 +00:00
|
|
|
} else {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("text", msg.Text).
|
2019-06-07 20:49:18 +00:00
|
|
|
Msg("Unknown message is hidden")
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
|
|
|
|
s.msgBuffer.Value = msg
|
|
|
|
s.msgBuffer = s.msgBuffer.Next()
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
|
|
|
switch kind {
|
|
|
|
case bot.Message:
|
2019-03-10 03:40:03 +00:00
|
|
|
return s.sendMessage(args[0].(string), args[1].(string), false, args...)
|
2019-02-06 03:52:49 +00:00
|
|
|
case bot.Action:
|
2019-03-10 03:40:03 +00:00
|
|
|
return s.sendMessage(args[0].(string), args[1].(string), true, args...)
|
2019-02-06 03:52:49 +00:00
|
|
|
case bot.Edit:
|
|
|
|
return s.edit(args[0].(string), args[1].(string), args[2].(string))
|
|
|
|
case bot.Reply:
|
|
|
|
switch args[2].(type) {
|
|
|
|
case msg.Message:
|
|
|
|
return s.replyToMessage(args[0].(string), args[1].(string), args[2].(msg.Message))
|
|
|
|
case string:
|
|
|
|
return s.replyToMessageIdentifier(args[0].(string), args[1].(string), args[2].(string))
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("Invalid types given to Reply")
|
|
|
|
}
|
|
|
|
case bot.Reaction:
|
|
|
|
return s.react(args[0].(string), args[1].(string), args[2].(msg.Message))
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("No handler for message type %d", kind)
|
|
|
|
}
|
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
func (s *SlackApp) sendMessage(channel, message string, meMessage bool, args ...interface{}) (string, error) {
|
2019-02-06 05:17:32 +00:00
|
|
|
ts, err := "", fmt.Errorf("")
|
2019-02-06 03:52:49 +00:00
|
|
|
nick := s.config.Get("Nick", "bot")
|
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
options := []slack.MsgOption{
|
|
|
|
slack.MsgOptionUsername(nick),
|
|
|
|
slack.MsgOptionText(message, false),
|
|
|
|
}
|
2019-02-06 05:17:32 +00:00
|
|
|
if meMessage {
|
2019-03-10 03:40:03 +00:00
|
|
|
options = append(options, slack.MsgOptionMeMessage())
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
// Check for message attachments
|
|
|
|
attachments := []slack.Attachment{}
|
|
|
|
if len(args) > 0 {
|
|
|
|
for _, a := range args {
|
|
|
|
switch a := a.(type) {
|
|
|
|
case bot.ImageAttachment:
|
|
|
|
attachments = append(attachments, slack.Attachment{
|
|
|
|
ImageURL: a.URL,
|
|
|
|
Text: a.AltTxt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
if len(attachments) > 0 {
|
|
|
|
options = append(options, slack.MsgOptionAttachments(attachments...))
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("channel", channel).
|
|
|
|
Str("message", message).
|
2019-03-10 03:40:03 +00:00
|
|
|
Int("attachment count", len(attachments)).
|
|
|
|
Int("option count", len(options)).
|
|
|
|
Int("arg count", len(args)).
|
2019-03-07 16:35:42 +00:00
|
|
|
Msg("Sending message")
|
2019-02-06 03:52:49 +00:00
|
|
|
|
2019-03-10 03:40:03 +00:00
|
|
|
_, ts, err = s.api.PostMessage(channel, options...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error sending message")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts, nil
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) replyToMessageIdentifier(channel, message, identifier string) (string, error) {
|
|
|
|
nick := s.config.Get("Nick", "bot")
|
2019-02-08 19:54:40 +00:00
|
|
|
|
|
|
|
resp, err := http.PostForm("https://slack.com/api/chat.postMessage",
|
|
|
|
url.Values{"token": {s.botToken},
|
|
|
|
"username": {nick},
|
|
|
|
"channel": {channel},
|
|
|
|
"text": {message},
|
|
|
|
"thread_ts": {identifier},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error sending Slack reply: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading Slack API body: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Bytes("body", body)
|
2019-02-08 19:54:40 +00:00
|
|
|
|
|
|
|
type MessageResponse struct {
|
|
|
|
OK bool `json:"ok"`
|
|
|
|
Timestamp string `json:"ts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var mr MessageResponse
|
|
|
|
err = json.Unmarshal(body, &mr)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error parsing message response: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mr.OK {
|
|
|
|
return "", fmt.Errorf("Got !OK from slack message response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return mr.Timestamp, err
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) replyToMessage(channel, message string, replyTo msg.Message) (string, error) {
|
|
|
|
return s.replyToMessageIdentifier(channel, message, replyTo.AdditionalData["RAW_SLACK_TIMESTAMP"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) react(channel, reaction string, message msg.Message) (string, error) {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("channel", channel).
|
|
|
|
Str("reaction", reaction).
|
|
|
|
Msg("reacting")
|
2019-02-06 05:17:32 +00:00
|
|
|
ref := slack.ItemRef{
|
|
|
|
Channel: channel,
|
|
|
|
Timestamp: message.AdditionalData["RAW_SLACK_TIMESTAMP"],
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-02-06 05:17:32 +00:00
|
|
|
err := s.api.AddReaction(reaction, ref)
|
|
|
|
return "", err
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) edit(channel, newMessage, identifier string) (string, error) {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("channel", channel).
|
|
|
|
Str("identifier", identifier).
|
|
|
|
Str("newMessage", newMessage).
|
|
|
|
Msg("editing")
|
2019-02-06 05:17:32 +00:00
|
|
|
nick := s.config.Get("Nick", "bot")
|
|
|
|
_, ts, err := s.api.PostMessage(channel,
|
|
|
|
slack.MsgOptionUsername(nick),
|
|
|
|
slack.MsgOptionText(newMessage, false),
|
|
|
|
slack.MsgOptionMeMessage(),
|
|
|
|
slack.MsgOptionUpdate(identifier))
|
|
|
|
return ts, err
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) GetEmojiList() map[string]string {
|
|
|
|
return s.emoji
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) populateEmojiList() {
|
2019-02-06 05:17:32 +00:00
|
|
|
if s.userToken == "NONE" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Msg("Cannot get emoji list without slack.usertoken")
|
2019-02-06 03:52:49 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-15 18:22:54 +00:00
|
|
|
api := slack.New(s.userToken, slack.OptionDebug(false))
|
2019-02-06 03:52:49 +00:00
|
|
|
|
2019-02-06 05:17:32 +00:00
|
|
|
em, err := api.GetEmoji()
|
2019-02-06 03:52:49 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error retrieving emoji list from Slack")
|
2019-02-06 05:17:32 +00:00
|
|
|
return
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 05:17:32 +00:00
|
|
|
s.emoji = em
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// I think it's horseshit that I have to do this
|
|
|
|
func slackTStoTime(t string) time.Time {
|
|
|
|
ts := strings.Split(t, ".")
|
2019-03-07 20:36:59 +00:00
|
|
|
if len(ts) < 2 {
|
|
|
|
log.Fatal().
|
|
|
|
Str("ts", t).
|
|
|
|
Msg("Could not parse Slack timestamp")
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
sec, _ := strconv.ParseInt(ts[0], 10, 64)
|
|
|
|
nsec, _ := strconv.ParseInt(ts[1], 10, 64)
|
|
|
|
return time.Unix(sec, nsec)
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlDetector = regexp.MustCompile(`<(.+)://([^|^>]+).*>`)
|
|
|
|
|
|
|
|
// Convert a slackMessage to a msg.Message
|
|
|
|
func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message {
|
|
|
|
text := html.UnescapeString(m.Text)
|
|
|
|
|
|
|
|
text = fixText(s.getUser, text)
|
|
|
|
|
2020-05-17 13:39:42 +00:00
|
|
|
// Slack likes to put these NBSP in and it screws with matching later
|
|
|
|
text = strings.ReplaceAll(text, "\u00a0", " ")
|
2020-10-21 17:40:49 +00:00
|
|
|
text = zerowidth.RemoveZeroWidthCharacters(text)
|
2020-05-17 13:39:42 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
isCmd, text := bot.IsCmd(s.config, text)
|
|
|
|
|
|
|
|
isAction := m.SubType == "me_message"
|
|
|
|
|
2019-06-07 16:19:24 +00:00
|
|
|
// We have to try a few layers to get a valid name for the user because Slack
|
2019-09-24 17:52:20 +00:00
|
|
|
defaultName := "unknown"
|
|
|
|
if m.BotID != "" {
|
|
|
|
defaultName = m.Username
|
|
|
|
}
|
2020-05-08 21:47:32 +00:00
|
|
|
icon := ""
|
|
|
|
log.Debug().Msgf("Getting user %s", m.User)
|
2020-05-08 14:31:27 +00:00
|
|
|
name, u := s.getUser(m.User, defaultName)
|
2020-05-08 21:47:32 +00:00
|
|
|
if u != nil {
|
|
|
|
icon = u.Profile.Image192
|
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
if m.Username != "" && name == "unknown" {
|
2019-06-07 16:19:24 +00:00
|
|
|
name = m.Username
|
|
|
|
}
|
2019-06-07 20:49:18 +00:00
|
|
|
|
|
|
|
chName := m.Channel
|
|
|
|
if ch, _ := s.getChannel(m.Channel); ch != nil {
|
|
|
|
chName = ch.Name
|
|
|
|
}
|
2019-02-06 03:52:49 +00:00
|
|
|
|
|
|
|
tstamp := slackTStoTime(m.TimeStamp)
|
|
|
|
|
|
|
|
return msg.Message{
|
2020-09-09 17:21:39 +00:00
|
|
|
ID: m.TimeStamp,
|
2019-02-06 03:52:49 +00:00
|
|
|
User: &user.User{
|
|
|
|
ID: m.User,
|
2019-06-07 16:19:24 +00:00
|
|
|
Name: name,
|
2020-05-08 21:47:32 +00:00
|
|
|
Icon: icon,
|
2019-02-06 03:52:49 +00:00
|
|
|
},
|
2019-06-07 16:19:24 +00:00
|
|
|
Body: text,
|
|
|
|
Raw: m,
|
|
|
|
Channel: m.Channel,
|
2019-06-07 20:49:18 +00:00
|
|
|
ChannelName: chName,
|
2019-06-07 16:19:24 +00:00
|
|
|
IsIM: m.ChannelType == "im",
|
|
|
|
Command: isCmd,
|
|
|
|
Action: isAction,
|
|
|
|
Time: tstamp,
|
2019-02-06 03:52:49 +00:00
|
|
|
AdditionalData: map[string]string{
|
|
|
|
"RAW_SLACK_TIMESTAMP": m.TimeStamp,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 16:19:24 +00:00
|
|
|
func (s *SlackApp) getChannel(id string) (*slack.Channel, error) {
|
|
|
|
if ch, ok := s.channels[id]; ok {
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
Str("id", id).
|
|
|
|
Msg("Channel not known, requesting info")
|
|
|
|
|
|
|
|
ch, err := s.api.GetChannelInfo(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.channels[id] = ch
|
|
|
|
return s.channels[id], nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
func stringForUser(user *slack.User) string {
|
|
|
|
if user.Profile.DisplayName != "" {
|
|
|
|
return user.Profile.DisplayName
|
|
|
|
}
|
|
|
|
return user.Name
|
|
|
|
}
|
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
// Get username for Slack user ID
|
2020-05-08 14:31:27 +00:00
|
|
|
func (s *SlackApp) getUser(id, defaultName string) (string, *slack.User) {
|
2019-09-19 20:30:59 +00:00
|
|
|
if u, ok := s.users[id]; ok {
|
2020-05-08 14:31:27 +00:00
|
|
|
return stringForUser(u), u
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("id", id).
|
|
|
|
Msg("User not already found, requesting info")
|
2019-09-19 20:30:59 +00:00
|
|
|
|
2019-02-06 03:52:49 +00:00
|
|
|
u, err := s.api.GetUserInfo(id)
|
|
|
|
if err != nil {
|
2020-05-08 14:31:27 +00:00
|
|
|
return defaultName, nil
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-06-07 16:19:24 +00:00
|
|
|
s.users[id] = u
|
2020-05-08 14:31:27 +00:00
|
|
|
return stringForUser(u), u
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Who gets usernames out of a channel
|
|
|
|
func (s *SlackApp) Who(id string) []string {
|
2019-02-07 19:21:22 +00:00
|
|
|
if s.userToken == "NONE" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Msg("Cannot get emoji list without slack.usertoken")
|
2019-02-07 19:21:22 +00:00
|
|
|
return []string{s.config.Get("nick", "bot")}
|
|
|
|
}
|
2019-02-15 18:22:54 +00:00
|
|
|
api := slack.New(s.userToken, slack.OptionDebug(false))
|
2019-02-07 19:21:22 +00:00
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().
|
|
|
|
Str("id", id).
|
|
|
|
Msg("Who is queried")
|
2019-02-06 03:52:49 +00:00
|
|
|
// Not super sure this is the correct call
|
2019-02-07 19:21:22 +00:00
|
|
|
params := &slack.GetUsersInConversationParameters{
|
|
|
|
ChannelID: id,
|
|
|
|
Limit: 50,
|
|
|
|
}
|
|
|
|
members, _, err := api.GetUsersInConversation(params)
|
2019-02-06 03:52:49 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err)
|
2019-02-07 19:21:22 +00:00
|
|
|
return []string{s.config.Get("nick", "bot")}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []string{}
|
|
|
|
for _, m := range members {
|
2019-09-24 17:52:20 +00:00
|
|
|
if m == "" {
|
|
|
|
log.Error().Msg("empty member")
|
|
|
|
}
|
2020-05-08 14:31:27 +00:00
|
|
|
u, _ := s.getUser(m, "unknown")
|
2019-09-26 14:31:09 +00:00
|
|
|
if u == "unknown" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().
|
|
|
|
Err(err).
|
|
|
|
Str("user", m).
|
|
|
|
Msg("Couldn't get user")
|
2019-02-07 19:21:22 +00:00
|
|
|
continue
|
2019-06-07 16:19:24 +00:00
|
|
|
/**/
|
2019-02-07 19:21:22 +00:00
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
ret = append(ret, u)
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-02-07 19:21:22 +00:00
|
|
|
return ret
|
2019-02-06 03:52:49 +00:00
|
|
|
}
|
2019-06-07 16:19:24 +00:00
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
// logMessage writes to a <slackapp.logMessage.dir>/<channel>.logMessage
|
|
|
|
// Uses slackapp.logMessage.format to write entries
|
|
|
|
func (s *SlackApp) logMessage(raw msg.Message) error {
|
2019-06-07 18:23:14 +00:00
|
|
|
// Do some filtering and fixing up front
|
|
|
|
if raw.Body == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
msg.Message
|
|
|
|
TopicChange bool
|
|
|
|
}{
|
|
|
|
Message: raw,
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(raw.Body, "set the channel topic: ") {
|
|
|
|
topic := strings.SplitN(raw.Body, "set the channel topic: ", 2)
|
|
|
|
data.Body = "changed topic to " + topic[1]
|
|
|
|
data.TopicChange = true
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
if err := s.logFormat.Execute(&buf, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.log(buf.String(), raw.ChannelName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SlackApp) log(msg, channel string) error {
|
|
|
|
dir := path.Join(s.config.Get("slackapp.logMessage.dir", "logs"), channel)
|
2019-06-07 16:19:24 +00:00
|
|
|
now := time.Now()
|
|
|
|
fname := now.Format("20060102") + ".log"
|
|
|
|
path := path.Join(dir, fname)
|
|
|
|
|
|
|
|
log.Debug().
|
2019-09-19 20:30:59 +00:00
|
|
|
Interface("raw", msg).
|
2019-06-07 16:19:24 +00:00
|
|
|
Str("dir", dir).
|
|
|
|
Msg("Slack event")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
log.Error().
|
|
|
|
Err(err).
|
2019-09-19 20:30:59 +00:00
|
|
|
Msg("Could not create logMessage directory")
|
2019-06-07 16:19:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
2019-09-19 20:30:59 +00:00
|
|
|
Msg("Error opening logMessage file")
|
2019-06-07 16:19:24 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
if _, err := fmt.Fprint(f, msg); err != nil {
|
2019-06-07 16:19:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Sync()
|
|
|
|
}
|
2019-09-19 20:30:59 +00:00
|
|
|
|
2020-07-24 14:50:56 +00:00
|
|
|
func (s *SlackApp) reactionReceived(event *slackevents.ReactionAddedEvent) error {
|
|
|
|
log.Debug().Msgf("reactionReceived(%+v)", event)
|
2020-05-08 14:31:27 +00:00
|
|
|
name, _ := s.getUser(event.User, "unknown")
|
2019-09-19 20:30:59 +00:00
|
|
|
|
|
|
|
ch, err := s.getChannel(event.Item.Channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
channel := ch.Name
|
|
|
|
|
|
|
|
body := "unknown message"
|
|
|
|
|
|
|
|
// Iterate through the ring and print its contents
|
|
|
|
s.msgBuffer.Do(func(p interface{}) {
|
|
|
|
switch m := p.(type) {
|
|
|
|
case nil:
|
|
|
|
case *slackevents.MessageEvent:
|
|
|
|
if m.TimeStamp == event.Item.Timestamp {
|
2019-09-24 17:52:20 +00:00
|
|
|
defaultName := "unknown"
|
|
|
|
if m.BotID != "" {
|
|
|
|
defaultName = m.Username
|
|
|
|
}
|
2020-05-08 14:31:27 +00:00
|
|
|
u, _ := s.getUser(m.User, defaultName)
|
2019-09-19 20:30:59 +00:00
|
|
|
body = fmt.Sprintf("%s: %s", u, m.Text)
|
|
|
|
}
|
|
|
|
default:
|
2020-07-24 14:50:56 +00:00
|
|
|
log.Debug().Interface("msg", m).Msg("Unexpected type in reaction received")
|
2019-09-19 20:30:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
tstamp := slackTStoTime(event.EventTimestamp)
|
|
|
|
msg := fmt.Sprintf("[%s] <%s> reacted to %s with :%s:\n",
|
|
|
|
fixDate(tstamp, "2006-01-02 15:04:05"),
|
|
|
|
name, body, event.Reaction)
|
|
|
|
|
2020-07-24 14:50:56 +00:00
|
|
|
log.Debug().Msgf("Made it to reaction received, logging %v: %v", msg, channel)
|
|
|
|
|
2019-09-19 20:30:59 +00:00
|
|
|
return s.log(msg, channel)
|
|
|
|
}
|
2020-05-08 14:31:27 +00:00
|
|
|
|
2020-09-09 17:21:39 +00:00
|
|
|
func (s *SlackApp) Profile(identifier string) (user.User, error) {
|
|
|
|
log.Debug().Msgf("Getting profile for %s", identifier)
|
2020-05-08 21:47:32 +00:00
|
|
|
|
|
|
|
users, err := s.api.GetUsers()
|
|
|
|
if err != nil {
|
|
|
|
return user.User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range users {
|
2020-09-09 17:21:39 +00:00
|
|
|
if u.Name == identifier || u.ID == identifier {
|
2020-05-08 21:47:32 +00:00
|
|
|
return user.User{
|
|
|
|
ID: u.ID,
|
|
|
|
Name: stringForUser(&u),
|
|
|
|
Admin: false,
|
|
|
|
Icon: u.Profile.Image192,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.User{}, fmt.Errorf("user %s not found", err)
|
2020-05-08 14:31:27 +00:00
|
|
|
}
|