2020-09-19 01:05:06 +00:00
|
|
|
package slacknotifier
|
|
|
|
|
|
|
|
import (
|
2022-08-03 06:10:56 +00:00
|
|
|
"bytes"
|
2020-09-19 01:05:06 +00:00
|
|
|
"context"
|
2024-11-08 04:31:25 +00:00
|
|
|
"errors"
|
2020-09-19 01:05:06 +00:00
|
|
|
"fmt"
|
2024-11-08 04:31:25 +00:00
|
|
|
"regexp"
|
2024-11-08 05:12:23 +00:00
|
|
|
"strings"
|
2021-05-28 17:30:57 +00:00
|
|
|
"time"
|
2020-09-19 01:05:06 +00:00
|
|
|
|
2022-01-07 09:37:52 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2024-11-05 09:02:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/livenote"
|
2022-01-07 09:37:52 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
|
2020-10-28 09:34:12 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-09-19 01:05:06 +00:00
|
|
|
"github.com/slack-go/slack"
|
|
|
|
)
|
|
|
|
|
2024-03-15 10:24:44 +00:00
|
|
|
var limiter = rate.NewLimiter(rate.Every(1*time.Second), 3)
|
2022-01-07 09:37:52 +00:00
|
|
|
|
2021-05-28 17:30:57 +00:00
|
|
|
type notifyTask struct {
|
|
|
|
Channel string
|
|
|
|
Opts []slack.MsgOption
|
|
|
|
}
|
|
|
|
|
2020-09-19 01:05:06 +00:00
|
|
|
type Notifier struct {
|
2020-10-22 02:47:54 +00:00
|
|
|
client *slack.Client
|
|
|
|
channel string
|
2021-05-28 17:30:57 +00:00
|
|
|
|
|
|
|
taskC chan notifyTask
|
2024-11-05 09:34:26 +00:00
|
|
|
|
|
|
|
liveNotePool *livenote.Pool
|
2024-11-08 04:31:25 +00:00
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
userIdCache map[string]*slack.User
|
2024-11-08 04:31:25 +00:00
|
|
|
groupIdCache map[string]slack.UserGroup
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NotifyOption func(notifier *Notifier)
|
|
|
|
|
2022-01-16 11:06:26 +00:00
|
|
|
func New(client *slack.Client, channel string, options ...NotifyOption) *Notifier {
|
2020-09-19 01:05:06 +00:00
|
|
|
notifier := &Notifier{
|
2024-11-05 09:34:26 +00:00
|
|
|
channel: channel,
|
|
|
|
client: client,
|
|
|
|
taskC: make(chan notifyTask, 100),
|
|
|
|
liveNotePool: livenote.NewPool(100),
|
2024-11-08 04:40:02 +00:00
|
|
|
userIdCache: make(map[string]*slack.User, 30),
|
2024-11-08 04:31:25 +00:00
|
|
|
groupIdCache: make(map[string]slack.UserGroup, 50),
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(notifier)
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:31:25 +00:00
|
|
|
userGroups, err := client.GetUserGroupsContext(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("failed to get the slack user groups")
|
|
|
|
} else {
|
|
|
|
for _, group := range userGroups {
|
|
|
|
notifier.groupIdCache[group.Name] = group
|
|
|
|
}
|
|
|
|
|
|
|
|
// user groups: map[
|
|
|
|
// Development Team:{
|
|
|
|
// ID:S08004CQYQK
|
|
|
|
// TeamID:T036FASR3
|
|
|
|
// IsUserGroup:true
|
|
|
|
// Name:Development Team
|
|
|
|
// Description:dev
|
|
|
|
// Handle:dev
|
|
|
|
// IsExternal:false
|
|
|
|
// DateCreate:"Fri Nov 8"
|
|
|
|
// DateUpdate:"Fri Nov 8" DateDelete:"Thu Jan 1"
|
|
|
|
// AutoType: CreatedBy:U036FASR5 UpdatedBy:U12345678 DeletedBy:
|
|
|
|
// Prefs:{Channels:[] Groups:[]} UserCount:1 Users:[]}]
|
|
|
|
log.Debugf("slack user groups: %+v", notifier.groupIdCache)
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:30:57 +00:00
|
|
|
go notifier.worker()
|
|
|
|
|
2020-09-19 01:05:06 +00:00
|
|
|
return notifier
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:30:57 +00:00
|
|
|
func (n *Notifier) worker() {
|
|
|
|
ctx := context.Background()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case task := <-n.taskC:
|
2024-11-05 08:02:34 +00:00
|
|
|
// ignore the wait error
|
|
|
|
_ = limiter.Wait(ctx)
|
2023-07-14 05:19:46 +00:00
|
|
|
|
2021-05-28 17:30:57 +00:00
|
|
|
_, _, err := n.client.PostMessageContext(ctx, task.Channel, task.Opts...)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).
|
|
|
|
WithField("channel", task.Channel).
|
|
|
|
Errorf("slack api error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:31:25 +00:00
|
|
|
// userIdRegExp matches strings like <@U012AB3CD>
|
|
|
|
var userIdRegExp = regexp.MustCompile(`^<@(.+?)>$`)
|
|
|
|
|
|
|
|
// groupIdRegExp matches strings like <!subteam^ID>
|
|
|
|
var groupIdRegExp = regexp.MustCompile(`^<!subteam\^(.+?)>$`)
|
|
|
|
|
2024-11-08 05:12:23 +00:00
|
|
|
func (n *Notifier) translateHandles(ctx context.Context, handles []string) ([]string, error) {
|
|
|
|
var tags []string
|
|
|
|
for _, handle := range handles {
|
|
|
|
tag, err := n.translateHandle(ctx, handle)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
func (n *Notifier) translateHandle(ctx context.Context, handle string) (string, error) {
|
|
|
|
if handle == "" {
|
|
|
|
return "", errors.New("handle is empty")
|
2024-11-08 04:31:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
// trim the prefix '@' if we get a string like '@username'
|
|
|
|
if handle[0] == '@' {
|
|
|
|
handle = handle[1:]
|
2024-11-08 04:31:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
// if the given handle is already in slack user id format, we don't need to look up
|
|
|
|
if userIdRegExp.MatchString(handle) || groupIdRegExp.MatchString(handle) {
|
|
|
|
return handle, nil
|
2024-11-08 04:31:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
if user, exists := n.userIdCache[handle]; exists {
|
|
|
|
return toUserHandle(user.ID), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if group, exists := n.groupIdCache[handle]; exists {
|
|
|
|
return toSubteamHandle(group.ID), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
slackUser, err := n.client.GetUserInfoContext(ctx, handle)
|
2024-11-08 04:31:25 +00:00
|
|
|
if err != nil {
|
2024-11-08 05:12:23 +00:00
|
|
|
return "", fmt.Errorf("user handle %s not found: %v", handle, err)
|
2024-11-08 04:31:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 04:40:02 +00:00
|
|
|
if slackUser != nil {
|
|
|
|
n.userIdCache[handle] = slackUser
|
|
|
|
return toUserHandle(slackUser.ID), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("handle %s not found", handle)
|
|
|
|
}
|
|
|
|
|
2024-11-07 08:44:55 +00:00
|
|
|
func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) error {
|
2024-11-05 09:34:26 +00:00
|
|
|
note := n.liveNotePool.Update(obj)
|
2024-11-05 08:02:34 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
channel := note.ChannelID
|
|
|
|
if channel == "" {
|
|
|
|
channel = n.channel
|
|
|
|
}
|
|
|
|
|
2024-11-05 09:34:26 +00:00
|
|
|
var attachment slack.Attachment
|
|
|
|
if creator, ok := note.Object.(types.SlackAttachmentCreator); ok {
|
|
|
|
attachment = creator.SlackAttachment()
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("livenote object does not support types.SlackAttachmentCreator interface")
|
|
|
|
}
|
|
|
|
|
2024-11-07 08:31:02 +00:00
|
|
|
var slackOpts []slack.MsgOption
|
|
|
|
slackOpts = append(slackOpts, slack.MsgOptionAttachments(attachment))
|
|
|
|
|
2024-11-08 05:12:23 +00:00
|
|
|
var firstTimeHandles []string
|
|
|
|
var commentHandles []string
|
|
|
|
var mentions []*livenote.OptionOneTimeMention
|
2024-11-08 04:31:25 +00:00
|
|
|
var comments []*livenote.OptionComment
|
2024-11-07 08:31:02 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch val := opt.(type) {
|
2024-11-08 05:12:23 +00:00
|
|
|
case *livenote.OptionOneTimeMention:
|
2024-11-07 08:31:02 +00:00
|
|
|
mentions = append(mentions, val)
|
2024-11-08 05:12:23 +00:00
|
|
|
firstTimeHandles = append(firstTimeHandles, val.Users...)
|
2024-11-08 04:31:25 +00:00
|
|
|
case *livenote.OptionComment:
|
2024-11-07 08:31:02 +00:00
|
|
|
comments = append(comments, val)
|
2024-11-08 05:12:23 +00:00
|
|
|
commentHandles = append(commentHandles, val.Users...)
|
2024-11-07 08:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
2024-11-05 09:34:26 +00:00
|
|
|
|
2024-11-08 05:12:23 +00:00
|
|
|
firstTimeTags, err := n.translateHandles(context.Background(), firstTimeHandles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
commentTags, err := n.translateHandles(context.Background(), commentHandles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 04:31:25 +00:00
|
|
|
// format: mention slack user
|
|
|
|
// <@U012AB3CD>
|
|
|
|
|
2024-11-05 08:02:34 +00:00
|
|
|
if note.MessageID != "" {
|
2024-11-08 04:31:25 +00:00
|
|
|
// If compare is enabled, we need to attach the comments
|
|
|
|
|
2024-11-05 08:02:34 +00:00
|
|
|
// UpdateMessageContext returns channel, timestamp, text, err
|
2024-11-07 08:31:02 +00:00
|
|
|
_, _, _, err := n.client.UpdateMessageContext(ctx, channel, note.MessageID, slackOpts...)
|
2024-11-05 08:02:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-08 05:12:23 +00:00
|
|
|
_ = commentTags
|
|
|
|
|
2024-11-05 08:02:34 +00:00
|
|
|
} else {
|
2024-11-07 08:31:02 +00:00
|
|
|
respCh, respTs, err := n.client.PostMessageContext(ctx, channel, slackOpts...)
|
2024-11-05 08:02:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).
|
|
|
|
WithField("channel", n.channel).
|
|
|
|
Errorf("slack api error: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
note.SetChannelID(respCh)
|
|
|
|
note.SetMessageID(respTs)
|
2024-11-08 05:12:23 +00:00
|
|
|
|
|
|
|
_, _, err = n.client.PostMessageContext(ctx, channel,
|
|
|
|
slack.MsgOptionText(joinTags(firstTimeTags), false),
|
|
|
|
slack.MsgOptionTS(respTs))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-05 08:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *Notifier) Notify(obj interface{}, args ...interface{}) {
|
2024-11-05 08:02:34 +00:00
|
|
|
// TODO: filter args for the channel option
|
2021-05-12 04:37:48 +00:00
|
|
|
n.NotifyTo(n.channel, obj, args...)
|
2020-10-22 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func filterSlackAttachments(args []interface{}) (slackAttachments []slack.Attachment, pureArgs []interface{}) {
|
|
|
|
var firstAttachmentOffset = -1
|
2020-09-19 01:05:06 +00:00
|
|
|
for idx, arg := range args {
|
|
|
|
switch a := arg.(type) {
|
|
|
|
|
|
|
|
// concrete type assert first
|
|
|
|
case slack.Attachment:
|
2021-05-12 04:37:48 +00:00
|
|
|
if firstAttachmentOffset == -1 {
|
|
|
|
firstAttachmentOffset = idx
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slackAttachments = append(slackAttachments, a)
|
|
|
|
|
2024-02-21 07:24:45 +00:00
|
|
|
case *slack.Attachment:
|
|
|
|
if firstAttachmentOffset == -1 {
|
|
|
|
firstAttachmentOffset = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
slackAttachments = append(slackAttachments, *a)
|
|
|
|
|
2023-07-14 05:19:46 +00:00
|
|
|
case types.SlackAttachmentCreator:
|
2021-05-12 04:37:48 +00:00
|
|
|
if firstAttachmentOffset == -1 {
|
|
|
|
firstAttachmentOffset = idx
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slackAttachments = append(slackAttachments, a.SlackAttachment())
|
|
|
|
|
2021-10-13 16:49:28 +00:00
|
|
|
case types.PlainText:
|
|
|
|
if firstAttachmentOffset == -1 {
|
|
|
|
firstAttachmentOffset = idx
|
|
|
|
}
|
2021-10-13 17:03:57 +00:00
|
|
|
|
2021-10-13 16:49:28 +00:00
|
|
|
// fallback to PlainText if it's not supported
|
|
|
|
// convert plain text to slack attachment
|
|
|
|
text := a.PlainText()
|
|
|
|
slackAttachments = append(slackAttachments, slack.Attachment{
|
|
|
|
Title: text,
|
|
|
|
})
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
pureArgs = args
|
|
|
|
if firstAttachmentOffset > -1 {
|
|
|
|
pureArgs = args[:firstAttachmentOffset]
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
2021-10-13 16:49:28 +00:00
|
|
|
|
|
|
|
return slackAttachments, pureArgs
|
2021-05-12 04:37:48 +00:00
|
|
|
}
|
2020-09-19 01:05:06 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *Notifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {
|
|
|
|
if len(channel) == 0 {
|
|
|
|
channel = n.channel
|
|
|
|
}
|
|
|
|
|
|
|
|
slackAttachments, pureArgs := filterSlackAttachments(args)
|
|
|
|
|
|
|
|
var opts []slack.MsgOption
|
|
|
|
|
|
|
|
switch a := obj.(type) {
|
|
|
|
case string:
|
|
|
|
opts = append(opts, slack.MsgOptionText(fmt.Sprintf(a, pureArgs...), true),
|
2021-05-09 16:02:08 +00:00
|
|
|
slack.MsgOptionAttachments(slackAttachments...))
|
2021-05-12 04:37:48 +00:00
|
|
|
|
|
|
|
case slack.Attachment:
|
|
|
|
opts = append(opts, slack.MsgOptionAttachments(append([]slack.Attachment{a}, slackAttachments...)...))
|
|
|
|
|
2023-07-14 05:19:46 +00:00
|
|
|
case types.SlackAttachmentCreator:
|
2021-05-12 04:37:48 +00:00
|
|
|
// convert object to slack attachment (if supported)
|
|
|
|
opts = append(opts, slack.MsgOptionAttachments(append([]slack.Attachment{a.SlackAttachment()}, slackAttachments...)...))
|
|
|
|
|
|
|
|
default:
|
2021-05-28 17:30:57 +00:00
|
|
|
log.Errorf("slack message conversion error, unsupported object: %T %+v", a, a)
|
2021-05-12 04:37:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:30:57 +00:00
|
|
|
select {
|
|
|
|
case n.taskC <- notifyTask{
|
|
|
|
Channel: channel,
|
|
|
|
Opts: opts,
|
|
|
|
}:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
return
|
|
|
|
}
|
2020-09-19 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 06:10:56 +00:00
|
|
|
func (n *Notifier) SendPhoto(buffer *bytes.Buffer) {
|
|
|
|
n.SendPhotoTo(n.channel, buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {
|
|
|
|
// TODO
|
|
|
|
}
|
2024-11-08 05:12:23 +00:00
|
|
|
|
|
|
|
func toUserHandle(id string) string {
|
|
|
|
return fmt.Sprintf("<@%s>", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSubteamHandle(id string) string {
|
|
|
|
return fmt.Sprintf("<!subteam^%s>", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinTags(tags []string) string {
|
|
|
|
return strings.Join(tags, " ")
|
|
|
|
}
|