bot: add icon and name to slack messages

This commit is contained in:
Chris Sexton 2019-01-11 19:58:17 -05:00
parent 5d9541ab3d
commit bbe0cdd28e
3 changed files with 19 additions and 8 deletions

View File

@ -35,6 +35,7 @@ type Config struct {
Token string Token string
} }
Nick string Nick string
IconURL string
FullName string FullName string
Version string Version string
CommandChar []string CommandChar []string

View File

@ -7,6 +7,7 @@ config = {
MaxBatchAdd = 10 MaxBatchAdd = 10
}, },
Nick = "CatBaseTest", Nick = "CatBaseTest",
IconURL = "http://placekitten.com/g/200/300",
LeftPad = { LeftPad = {
Who = "person", Who = "person",
MaxLen = 50 MaxLen = 50

View File

@ -16,9 +16,10 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
// "sync/atomic" // "sync/atomic"
"time"
"context" "context"
"time"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg" "github.com/velour/catbase/bot/msg"
@ -209,11 +210,15 @@ func (s *Slack) SendMessageType(channel, message string, meMessage bool) (string
postUrl = "https://slack.com/api/chat.meMessage" postUrl = "https://slack.com/api/chat.meMessage"
} }
nick := s.config.Nick
icon := s.config.IconURL
resp, err := http.PostForm(postUrl, resp, err := http.PostForm(postUrl,
url.Values{"token": {s.config.Slack.Token}, url.Values{"token": {s.config.Slack.Token},
"as_user": {"true"}, "username": {nick},
"channel": {channel}, "icon_url": {icon},
"text": {message}, "channel": {channel},
"text": {message},
}) })
if err != nil { if err != nil {
@ -264,9 +269,13 @@ func (s *Slack) SendAction(channel, message string) string {
} }
func (s *Slack) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) { func (s *Slack) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) {
nick := s.config.Nick
icon := s.config.IconURL
resp, err := http.PostForm("https://slack.com/api/chat.postMessage", resp, err := http.PostForm("https://slack.com/api/chat.postMessage",
url.Values{"token": {s.config.Slack.Token}, url.Values{"token": {s.config.Slack.Token},
"as_user": {"true"}, "username": {nick},
"icon_url": {icon},
"channel": {channel}, "channel": {channel},
"text": {message}, "text": {message},
"thread_ts": {identifier}, "thread_ts": {identifier},
@ -373,9 +382,9 @@ func (s *Slack) ping(ctx context.Context) {
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-ticker.C: case <-ticker.C:
ping := map[string]interface{}{"type": "ping", "time": time.Now().UnixNano()} ping := map[string]interface{}{"type": "ping", "time": time.Now().UnixNano()}
if err := s.ws.Send(context.TODO(), ping); err != nil { if err := s.ws.Send(context.TODO(), ping); err != nil {
panic(err) panic(err)