From 74b32dd2e99686dc7e85a8f25301ac484cde09ca Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 7 Jun 2019 16:49:18 -0400 Subject: [PATCH 1/5] slackApp: a few fixes... * DMs work again and are logged * bot knows who itself is --- connectors/slackapp/slackApp.go | 40 +++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/connectors/slackapp/slackApp.go b/connectors/slackapp/slackApp.go index ee187f5..07b9a8d 100644 --- a/connectors/slackapp/slackApp.go +++ b/connectors/slackapp/slackApp.go @@ -185,27 +185,29 @@ func (s *SlackApp) msgReceivd(msg *slackevents.MessageEvent) { } isItMe := msg.BotID != "" && msg.BotID == s.myBotID + m := s.buildMessage(msg) + if m.Time.Before(s.lastRecieved) { + log.Debug(). + Time("ts", m.Time). + Interface("lastRecv", s.lastRecieved). + Msg("Ignoring message") + return + } + if err := s.log(m); err != nil { + log.Fatal().Err(err).Msg("Error logging message") + } if !isItMe && msg.ThreadTimeStamp == "" { - m := s.buildMessage(msg) - if m.Time.Before(s.lastRecieved) { - log.Debug(). - Time("ts", m.Time). - Interface("lastRecv", s.lastRecieved). - Msg("Ignoring message") - } else { - if err := s.log(m); err != nil { - log.Fatal().Err(err).Msg("Error logging message") - } - s.lastRecieved = m.Time - s.event(s, bot.Message, m) - } + s.lastRecieved = m.Time + s.event(s, bot.Message, m) } else if msg.ThreadTimeStamp != "" { //we're throwing away some information here by not parsing the correct reply object type, but that's okay s.event(s, bot.Reply, s.buildMessage(msg), msg.ThreadTimeStamp) + } else if isItMe { + s.event(s, bot.SelfMessage, m) } else { log.Debug(). Str("text", msg.Text). - Msg("THAT MESSAGE WAS HIDDEN") + Msg("Unknown message is hidden") } } @@ -408,12 +410,16 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message { name := "UNKNOWN" u, _ := s.getUser(m.User) if u != nil { - name = u.Name + name = u.Profile.DisplayName } if m.Username != "" && u == nil { name = m.Username } - ch, _ := s.getChannel(m.Channel) + + chName := m.Channel + if ch, _ := s.getChannel(m.Channel); ch != nil { + chName = ch.Name + } tstamp := slackTStoTime(m.TimeStamp) @@ -425,7 +431,7 @@ func (s *SlackApp) buildMessage(m *slackevents.MessageEvent) msg.Message { Body: text, Raw: m, Channel: m.Channel, - ChannelName: ch.Name, + ChannelName: chName, IsIM: m.ChannelType == "im", Command: isCmd, Action: isAction, From 9ea45f0ad3d318ca50c364706791dd87cbfa6fee Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 9 Jun 2019 00:15:06 -0400 Subject: [PATCH 2/5] web: add a menu and redo index We can add arbitrary links now with the `bot.links` config --- bot/bot.go | 51 ++++--------------------- bot/interfaces.go | 1 + bot/mock.go | 1 + bot/web.go | 73 ++++++++++++++++++++++++++++++++++++ plugins/admin/admin.go | 5 ++- plugins/admin/index.go | 12 ++++-- plugins/cli/cli.go | 5 ++- plugins/cli/index.go | 12 ++++-- plugins/counter/counter.go | 7 ++-- plugins/counter/html.go | 20 ++++++---- plugins/fact/factoid.go | 4 +- plugins/fact/webTemplates.go | 10 ++++- 12 files changed, 136 insertions(+), 65 deletions(-) create mode 100644 bot/web.go diff --git a/bot/bot.go b/bot/bot.go index 8674a34..c313457 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -3,7 +3,6 @@ package bot import ( - "html/template" "net/http" "reflect" "strings" @@ -38,7 +37,7 @@ type bot struct { version string // The entries to the bot's HTTP interface - httpEndPoints map[string]string + httpEndPoints []EndPoint // filters registered by plugins filters map[string]func(string) string @@ -46,6 +45,10 @@ type bot struct { callbacks CallbackMap } +type EndPoint struct { + Name, URL string +} + // Variable represents a $var replacement type Variable struct { Variable, Value string @@ -73,7 +76,7 @@ func New(config *config.Config, connector Connector) Bot { me: users[0], logIn: logIn, logOut: logOut, - httpEndPoints: make(map[string]string), + httpEndPoints: make([]EndPoint, 0), filters: make(map[string]func(string) string), callbacks: make(CallbackMap), } @@ -133,46 +136,6 @@ func (b *bot) Who(channel string) []user.User { return users } -var rootIndex = ` - - - - Factoids - - - - {{if .EndPoints}} -
- - - - - - - - - {{range $key, $value := .EndPoints}} - - - - {{end}} - -
Plugin
{{$key}}
-
- {{end}} - -` - -func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { - context := make(map[string]interface{}) - context["EndPoints"] = b.httpEndPoints - t, err := template.New("rootIndex").Parse(rootIndex) - if err != nil { - log.Error().Err(err) - } - t.Execute(w, context) -} - // IsCmd checks if message is a command and returns its curtailed version func IsCmd(c *config.Config, message string) (bool, string) { cmdcs := c.GetArray("CommandChar", []string{"!"}) @@ -266,5 +229,5 @@ func (b *bot) Register(p Plugin, kind Kind, cb Callback) { } func (b *bot) RegisterWeb(root, name string) { - b.httpEndPoints[name] = root + b.httpEndPoints = append(b.httpEndPoints, EndPoint{name, root}) } diff --git a/bot/interfaces.go b/bot/interfaces.go index fba9396..d9d10d3 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -66,6 +66,7 @@ type Bot interface { RegisterFilter(string, func(string) string) RegisterWeb(string, string) DefaultConnector() Connector + GetWebNavigation() []EndPoint } // Connector represents a server connection to a chat service diff --git a/bot/mock.go b/bot/mock.go index 6f67c62..3511cf1 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -52,6 +52,7 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) RegisterWeb(_, _ string) {} +func (mb *MockBot) GetWebNavigation() []EndPoint { return nil } func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool { return false } diff --git a/bot/web.go b/bot/web.go new file mode 100644 index 0000000..bbe62ec --- /dev/null +++ b/bot/web.go @@ -0,0 +1,73 @@ +package bot + +import ( + "html/template" + "net/http" + "strings" +) + +func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { + context := make(map[string]interface{}) + context["Nav"] = b.GetWebNavigation() + t := template.Must(template.New("rootIndex").Parse(rootIndex)) + t.Execute(w, context) +} + +// GetWebNavigation returns a list of bootstrap-vue links +// The parent