New Slack Library

This commit is contained in:
Brian Buller 2023-11-29 17:54:12 -06:00
parent 09a662a8cd
commit 29ff2c7daf
12 changed files with 80 additions and 103 deletions

View File

@ -11,6 +11,9 @@ package:
cp cmd/helperbot/helperbot.service build/ cp cmd/helperbot/helperbot.service build/
cd build && tar -zcvf helperbot.tgz * cd build && tar -zcvf helperbot.tgz *
run:
cd build && ./helperbot
aoc-util: aoc-util:
go build -o build/aoc-util cmd/aoc-util/*.go go build -o build/aoc-util cmd/aoc-util/*.go

View File

@ -6,7 +6,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
type App struct { type App struct {
@ -86,7 +86,7 @@ func (a *App) watchMessageChannel() {
} else if msg.GetDestination() == "error" { } else if msg.GetDestination() == "error" {
fmt.Printf("ERROR: %s: %s\n", msg.GetSource(), msg.GetMessage().Text) fmt.Printf("ERROR: %s: %s\n", msg.GetSource(), msg.GetMessage().Text)
} else if msg.GetDestination() == "slack" { } else if msg.GetDestination() == "slack" {
a.m.SendSlackChannelMessage(&slackMsg) a.m.SendSlackChannelMessage(slackMsg.Text, msg.GetDestination())
} }
for _, v := range a.plugins { for _, v := range a.plugins {

View File

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
func GetMessageJson(msg *slack.Message) string { func GetMessageJson(msg *slack.Message) string {

View File

@ -7,7 +7,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
var DebugMode = false var DebugMode = false

View File

@ -1,6 +1,6 @@
package main package main
import "github.com/nlopes/slack" import "github.com/slack-go/slack"
// This message type is for communications over the messages channel // This message type is for communications over the messages channel
type BotMessage struct { type BotMessage struct {

View File

@ -10,7 +10,7 @@ import (
"git.bullercodeworks.com/brian/boltease" "git.bullercodeworks.com/brian/boltease"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
type BotModel struct { type BotModel struct {
@ -44,7 +44,13 @@ func NewBotModel() (*BotModel, error) {
return m, nil return m, nil
} }
func (m *BotModel) GetChannelInfo(id string) (*slack.Channel, error) {
return m.slackApi.GetConversationInfo(&slack.GetConversationInfoInput{id, false, false})
}
func (m *BotModel) SendMessage(src, dst string, msg slack.Message) { func (m *BotModel) SendMessage(src, dst string, msg slack.Message) {
fmt.Println("Sending Message:", src, "->", dst)
fmt.Println(msg.Text)
m.messages <- NewBotMessage(src, dst, msg) m.messages <- NewBotMessage(src, dst, msg)
} }

View File

@ -2,13 +2,12 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
/* DB Functions */ /* DB Functions */
@ -73,6 +72,8 @@ func (m *BotModel) RequestSlackToken() {
func (m *BotModel) HandleRTMEvents() { func (m *BotModel) HandleRTMEvents() {
for msg := range m.slackRTM.IncomingEvents { for msg := range m.slackRTM.IncomingEvents {
fmt.Printf("RTM Message: %v", msg)
switch ev := msg.Data.(type) { switch ev := msg.Data.(type) {
case *slack.MessageEvent: case *slack.MessageEvent:
m.processMessageEvent(ev) m.processMessageEvent(ev)
@ -89,60 +90,57 @@ func (m *BotModel) HandleRTMEvents() {
} }
} }
func (m *BotModel) SendSlackChannelMessage(msg *slack.Message) error { func (m *BotModel) SendSlackChannelMessage(msg string, cid string) error {
if DebugMode { if DebugMode {
return m.SendSlackAdminMessage(msg) return m.SendSlackAdminMessage(msg)
} }
// Send message to slack channel // Send message to slack channel
m.PostSlackMessage(msg) //m.PostSlackMessage(msg)
return nil _, _, err := m.slackApi.PostMessage(
cid,
slack.MsgOptionText(msg, false),
slack.MsgOptionAsUser(true),
)
return err
} }
func (m *BotModel) SendSlackAdminMessage(msg *slack.Message) error { func (m *BotModel) SendSlackAdminMessage(msg string) error {
// Send message to slack admin // Send message to slack admin
var err error dmId, err := m.GetSlackAdminDMId()
msg.Channel, err = m.GetSlackAdminDMId()
if err != nil { if err != nil {
return err return err
} }
m.PostSlackMessage(msg) _, _, err = m.slackApi.PostMessage(
dmId,
slack.MsgOptionText(msg, false),
slack.MsgOptionAsUser(true),
)
return err
return nil return nil
} }
func (m *BotModel) LoadDirectMessages() { func (m *BotModel) LoadDirectMessages() {
cs, err := m.slackApi.GetIMChannels() /*
if err != nil { cs, err := m.slackApi.GetIMChannels()
return
}
for _, v := range cs {
uname, err := m.GetSlackUserName(v.User)
if err != nil { if err != nil {
uname = v.User return
} }
m.slackIdToFriendly[v.ID] = uname for _, v := range cs {
} uname, err := m.GetSlackUserName(v.User)
if err != nil {
uname = v.User
}
m.slackIdToFriendly[v.ID] = uname
}
*/
} }
func (m *BotModel) processMessageEvent(ev *slack.MessageEvent) { func (m *BotModel) processMessageEvent(ev *slack.MessageEvent) {
m.GetSlackUserName(ev.User) m.GetSlackChannelName(ev.User)
m.GetSlackIdName(ev.Channel) m.GetSlackChannelName(ev.Channel)
m.IncomingSlackMessages <- ev m.IncomingSlackMessages <- ev
} }
func (m *BotModel) GetSlackIdName(id string) (string, error) {
switch id[0] {
case 'U':
return m.GetSlackUserName(id)
case 'G':
return m.GetSlackGroupName(id)
case 'C':
return m.GetSlackChannelName(id)
case 'D':
return m.GetSlackIMName(id)
}
return "", errors.New("Unknown ID Type")
}
func (m *BotModel) GetSlackUserName(id string) (string, error) { func (m *BotModel) GetSlackUserName(id string) (string, error) {
if v, ok := m.slackIdToFriendly[id]; ok { if v, ok := m.slackIdToFriendly[id]; ok {
return v, nil return v, nil
@ -155,23 +153,11 @@ func (m *BotModel) GetSlackUserName(id string) (string, error) {
return user.Profile.DisplayName, nil return user.Profile.DisplayName, nil
} }
func (m *BotModel) GetSlackIMName(id string) (string, error) {
if v, ok := m.slackIdToFriendly[id]; ok {
return v, nil
}
c, err := m.slackApi.GetChannelInfo(id)
if err != nil {
return "", err
}
m.slackIdToFriendly[id] = c.Name
return c.Name, nil
}
func (m *BotModel) GetSlackChannelName(id string) (string, error) { func (m *BotModel) GetSlackChannelName(id string) (string, error) {
if v, ok := m.slackIdToFriendly[id]; ok { if v, ok := m.slackIdToFriendly[id]; ok {
return v, nil return v, nil
} }
c, err := m.slackApi.GetChannelInfo(id) c, err := m.GetChannelInfo(id)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -179,44 +165,17 @@ func (m *BotModel) GetSlackChannelName(id string) (string, error) {
return c.Name, nil return c.Name, nil
} }
func (m *BotModel) GetSlackUserIM(id string) (string, error) { func (m *BotModel) SendMessageToChannel(msg string, cid string) error {
for k, v := range m.slackIdToFriendly { dmId, err := m.GetSlackChannelName(cid)
if v == id {
return k, nil
}
}
_, _, newId, err := m.slackApi.OpenIMChannel(id)
if err != nil {
return "", err
}
m.slackIdToFriendly[id] = newId
return newId, nil
}
func (m *BotModel) GetSlackGroupName(id string) (string, error) {
if v, ok := m.slackIdToFriendly[id]; ok {
return v, nil
}
g, err := m.slackApi.GetGroupInfo(id)
if err != nil {
return "", err
}
m.slackIdToFriendly[id] = g.Name
return g.Name, nil
}
func (m *BotModel) PostSlackMessage(msg *slack.Message) {
m.slackRTM.SendMessage(m.slackRTM.NewOutgoingMessage(msg.Text, msg.Channel))
}
func (m *BotModel) SendMessageToUser(msg *slack.Message, uid string) error {
dmId, err := m.GetSlackUserIM(uid)
if err != nil { if err != nil {
return err return err
} }
msg.Channel = dmId _, _, err = m.slackApi.PostMessage(
m.PostSlackMessage(msg) dmId,
return nil slack.MsgOptionText(msg, false),
slack.MsgOptionAsUser(true),
)
return err
} }
func (m *BotModel) GetSlackUserInfo(uid string) (*slack.User, error) { func (m *BotModel) GetSlackUserInfo(uid string) (*slack.User, error) {

10
go.mod
View File

@ -1,16 +1,16 @@
module git.bullercodeworks.com/brian/helperbot module git.bullercodeworks.com/brian/helperbot
go 1.17 go 1.20
require ( require (
git.bullercodeworks.com/brian/boltease v1.0.0 git.bullercodeworks.com/brian/boltease v1.0.0
git.bullercodeworks.com/brian/go-adventofcode v1.1.1 git.bullercodeworks.com/brian/go-adventofcode v1.1.1
github.com/nlopes/slack v0.6.0 github.com/slack-go/slack v0.12.3
) )
require ( require (
github.com/boltdb/bolt v1.3.1 // indirect github.com/boltdb/bolt v1.3.1 // indirect
github.com/gorilla/websocket v1.2.0 // indirect github.com/gorilla/websocket v1.5.1 // indirect
github.com/pkg/errors v0.8.0 // indirect golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect golang.org/x/sys v0.15.0 // indirect
) )

21
go.sum
View File

@ -6,15 +6,22 @@ github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ= github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/slack-go/slack v0.12.3 h1:92/dfFU8Q5XP6Wp5rr5/T5JHLM5c5Smtn53fhToAP88=
github.com/slack-go/slack v0.12.3/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -3,7 +3,7 @@ package helperbot
import ( import (
"time" "time"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
type Model interface { type Model interface {

View File

@ -15,7 +15,7 @@ import (
aoc "git.bullercodeworks.com/brian/go-adventofcode" aoc "git.bullercodeworks.com/brian/go-adventofcode"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
/* Plugin State */ /* Plugin State */

View File

@ -4,11 +4,12 @@ import (
"C" "C"
) )
import ( import (
"fmt"
"strings" "strings"
"time" "time"
"git.bullercodeworks.com/brian/helperbot" "git.bullercodeworks.com/brian/helperbot"
"github.com/nlopes/slack" "github.com/slack-go/slack"
) )
type StatsState struct { type StatsState struct {
@ -26,6 +27,7 @@ func (s *StatsState) Initialize(m helperbot.Model) error {
} }
func (s *StatsState) ProcessMessage(m helperbot.Message) { func (s *StatsState) ProcessMessage(m helperbot.Message) {
fmt.Printf("Processing Message: %s\n", m.GetMessage().Text)
if m.GetSource() == "slack" { if m.GetSource() == "slack" {
slackMsg := m.GetMessage() slackMsg := m.GetMessage()
if len(slackMsg.Channel) == 0 { if len(slackMsg.Channel) == 0 {