New Slack Library
This commit is contained in:
parent
09a662a8cd
commit
29ff2c7daf
3
Makefile
3
Makefile
@ -11,6 +11,9 @@ package:
|
||||
cp cmd/helperbot/helperbot.service build/
|
||||
cd build && tar -zcvf helperbot.tgz *
|
||||
|
||||
run:
|
||||
cd build && ./helperbot
|
||||
|
||||
aoc-util:
|
||||
go build -o build/aoc-util cmd/aoc-util/*.go
|
||||
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
@ -86,7 +86,7 @@ func (a *App) watchMessageChannel() {
|
||||
} else if msg.GetDestination() == "error" {
|
||||
fmt.Printf("ERROR: %s: %s\n", msg.GetSource(), msg.GetMessage().Text)
|
||||
} else if msg.GetDestination() == "slack" {
|
||||
a.m.SendSlackChannelMessage(&slackMsg)
|
||||
a.m.SendSlackChannelMessage(slackMsg.Text, msg.GetDestination())
|
||||
}
|
||||
|
||||
for _, v := range a.plugins {
|
||||
|
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
func GetMessageJson(msg *slack.Message) string {
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
var DebugMode = false
|
||||
|
@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/nlopes/slack"
|
||||
import "github.com/slack-go/slack"
|
||||
|
||||
// This message type is for communications over the messages channel
|
||||
type BotMessage struct {
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
"git.bullercodeworks.com/brian/boltease"
|
||||
"git.bullercodeworks.com/brian/helperbot"
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type BotModel struct {
|
||||
@ -44,7 +44,13 @@ func NewBotModel() (*BotModel, error) {
|
||||
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) {
|
||||
fmt.Println("Sending Message:", src, "->", dst)
|
||||
fmt.Println(msg.Text)
|
||||
m.messages <- NewBotMessage(src, dst, msg)
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
/* DB Functions */
|
||||
@ -73,6 +72,8 @@ func (m *BotModel) RequestSlackToken() {
|
||||
|
||||
func (m *BotModel) HandleRTMEvents() {
|
||||
for msg := range m.slackRTM.IncomingEvents {
|
||||
fmt.Printf("RTM Message: %v", msg)
|
||||
|
||||
switch ev := msg.Data.(type) {
|
||||
case *slack.MessageEvent:
|
||||
m.processMessageEvent(ev)
|
||||
@ -89,27 +90,37 @@ func (m *BotModel) HandleRTMEvents() {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *BotModel) SendSlackChannelMessage(msg *slack.Message) error {
|
||||
func (m *BotModel) SendSlackChannelMessage(msg string, cid string) error {
|
||||
if DebugMode {
|
||||
return m.SendSlackAdminMessage(msg)
|
||||
}
|
||||
// Send message to slack channel
|
||||
m.PostSlackMessage(msg)
|
||||
return nil
|
||||
//m.PostSlackMessage(msg)
|
||||
_, _, 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
|
||||
var err error
|
||||
msg.Channel, err = m.GetSlackAdminDMId()
|
||||
dmId, err := m.GetSlackAdminDMId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.PostSlackMessage(msg)
|
||||
_, _, err = m.slackApi.PostMessage(
|
||||
dmId,
|
||||
slack.MsgOptionText(msg, false),
|
||||
slack.MsgOptionAsUser(true),
|
||||
)
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *BotModel) LoadDirectMessages() {
|
||||
/*
|
||||
cs, err := m.slackApi.GetIMChannels()
|
||||
if err != nil {
|
||||
return
|
||||
@ -121,28 +132,15 @@ func (m *BotModel) LoadDirectMessages() {
|
||||
}
|
||||
m.slackIdToFriendly[v.ID] = uname
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func (m *BotModel) processMessageEvent(ev *slack.MessageEvent) {
|
||||
m.GetSlackUserName(ev.User)
|
||||
m.GetSlackIdName(ev.Channel)
|
||||
m.GetSlackChannelName(ev.User)
|
||||
m.GetSlackChannelName(ev.Channel)
|
||||
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) {
|
||||
if v, ok := m.slackIdToFriendly[id]; ok {
|
||||
return v, nil
|
||||
@ -155,23 +153,11 @@ func (m *BotModel) GetSlackUserName(id string) (string, error) {
|
||||
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) {
|
||||
if v, ok := m.slackIdToFriendly[id]; ok {
|
||||
return v, nil
|
||||
}
|
||||
c, err := m.slackApi.GetChannelInfo(id)
|
||||
c, err := m.GetChannelInfo(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -179,44 +165,17 @@ func (m *BotModel) GetSlackChannelName(id string) (string, error) {
|
||||
return c.Name, nil
|
||||
}
|
||||
|
||||
func (m *BotModel) GetSlackUserIM(id string) (string, error) {
|
||||
for k, v := range m.slackIdToFriendly {
|
||||
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)
|
||||
func (m *BotModel) SendMessageToChannel(msg string, cid string) error {
|
||||
dmId, err := m.GetSlackChannelName(cid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg.Channel = dmId
|
||||
m.PostSlackMessage(msg)
|
||||
return nil
|
||||
_, _, err = m.slackApi.PostMessage(
|
||||
dmId,
|
||||
slack.MsgOptionText(msg, false),
|
||||
slack.MsgOptionAsUser(true),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *BotModel) GetSlackUserInfo(uid string) (*slack.User, error) {
|
||||
|
10
go.mod
10
go.mod
@ -1,16 +1,16 @@
|
||||
module git.bullercodeworks.com/brian/helperbot
|
||||
|
||||
go 1.17
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
git.bullercodeworks.com/brian/boltease v1.0.0
|
||||
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 (
|
||||
github.com/boltdb/bolt v1.3.1 // indirect
|
||||
github.com/gorilla/websocket v1.2.0 // indirect
|
||||
github.com/pkg/errors v0.8.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
golang.org/x/net v0.19.0 // indirect
|
||||
golang.org/x/sys v0.15.0 // indirect
|
||||
)
|
||||
|
21
go.sum
21
go.sum
@ -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/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/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
|
||||
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA=
|
||||
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk=
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
|
||||
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
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/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/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.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=
|
||||
|
@ -3,7 +3,7 @@ package helperbot
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type Model interface {
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
|
||||
aoc "git.bullercodeworks.com/brian/go-adventofcode"
|
||||
"git.bullercodeworks.com/brian/helperbot"
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
/* Plugin State */
|
||||
|
@ -4,11 +4,12 @@ import (
|
||||
"C"
|
||||
)
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.bullercodeworks.com/brian/helperbot"
|
||||
"github.com/nlopes/slack"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type StatsState struct {
|
||||
@ -26,6 +27,7 @@ func (s *StatsState) Initialize(m helperbot.Model) error {
|
||||
}
|
||||
|
||||
func (s *StatsState) ProcessMessage(m helperbot.Message) {
|
||||
fmt.Printf("Processing Message: %s\n", m.GetMessage().Text)
|
||||
if m.GetSource() == "slack" {
|
||||
slackMsg := m.GetMessage()
|
||||
if len(slackMsg.Channel) == 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user