From bbe0cdd28eea84dc867f90040c01040842415b41 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 11 Jan 2019 19:58:17 -0500 Subject: [PATCH] bot: add icon and name to slack messages --- config/config.go | 1 + example_config.lua | 3 ++- slack/slack.go | 23 ++++++++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index 8868b2b..a16e00c 100644 --- a/config/config.go +++ b/config/config.go @@ -35,6 +35,7 @@ type Config struct { Token string } Nick string + IconURL string FullName string Version string CommandChar []string diff --git a/example_config.lua b/example_config.lua index fce3646..b1a2ea3 100644 --- a/example_config.lua +++ b/example_config.lua @@ -7,6 +7,7 @@ config = { MaxBatchAdd = 10 }, Nick = "CatBaseTest", + IconURL = "http://placekitten.com/g/200/300", LeftPad = { Who = "person", MaxLen = 50 @@ -119,4 +120,4 @@ config = { } } -} \ No newline at end of file +} diff --git a/slack/slack.go b/slack/slack.go index 7802039..32faaaa 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -16,9 +16,10 @@ import ( "regexp" "strconv" "strings" + // "sync/atomic" - "time" "context" + "time" "github.com/velour/catbase/bot" "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" } + nick := s.config.Nick + icon := s.config.IconURL + resp, err := http.PostForm(postUrl, url.Values{"token": {s.config.Slack.Token}, - "as_user": {"true"}, - "channel": {channel}, - "text": {message}, + "username": {nick}, + "icon_url": {icon}, + "channel": {channel}, + "text": {message}, }) 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) { + nick := s.config.Nick + icon := s.config.IconURL + resp, err := http.PostForm("https://slack.com/api/chat.postMessage", url.Values{"token": {s.config.Slack.Token}, - "as_user": {"true"}, + "username": {nick}, + "icon_url": {icon}, "channel": {channel}, "text": {message}, "thread_ts": {identifier}, @@ -373,9 +382,9 @@ func (s *Slack) ping(ctx context.Context) { defer ticker.Stop() for { select { - case <-ctx.Done(): + case <-ctx.Done(): return - case <-ticker.C: + case <-ticker.C: ping := map[string]interface{}{"type": "ping", "time": time.Now().UnixNano()} if err := s.ws.Send(context.TODO(), ping); err != nil { panic(err)