Skip to content

Commit

Permalink
Merge pull request #12 from gadget-bot/feature/expose-botid-on-router
Browse files Browse the repository at this point in the history
Expose botid on router
  • Loading branch information
xortim committed Nov 22, 2021
2 parents d24d3db + efb1b00 commit 33240c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
37 changes: 4 additions & 33 deletions core/core.go
Expand Up @@ -2,7 +2,6 @@ package core

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -39,23 +38,6 @@ var listenPort = os.Getenv("GADGET_LISTEN_PORT")

var api = slack.New(slackOauthToken)

type EventMessageAuthorization struct {
UserId string `json:"user_id"`
TeamId string `json:"team_id"`
}

// this is required because slack-go doesn't seem to provide a way to get the bot's own ID
type EventsAPICallbackEvent struct {
Type string `json:"type"`
Token string `json:"token"`
TeamID string `json:"team_id"`
APIAppID string `json:"api_app_id"`
Authoritzations []EventMessageAuthorization `json:"authorizations"`
EventID string `json:"event_id"`
EventTime int `json:"event_time"`
EventContext string `json:"event_context"`
}

type Gadget struct {
Router router.Router
}
Expand All @@ -65,17 +47,6 @@ func requestLog(code int, r http.Request) {
log.Info().Str("method", r.Method).Str("code", string_code).Str("uri", r.URL.String()).Msg("")
}

func getBotUuid(body []byte) (string, error) {
var authorizedUsers EventsAPICallbackEvent
json.Unmarshal([]byte(body), &authorizedUsers)

if len(authorizedUsers.Authoritzations) > 0 {
return authorizedUsers.Authoritzations[0].UserId, nil
} else {
return "", errors.New("Weird")
}
}

func getListenPort() string {
if listenPort != "" {
return listenPort
Expand Down Expand Up @@ -190,15 +161,15 @@ func (gadget Gadget) Run() error {

if eventsAPIEvent.Type == slackevents.CallbackEvent {
innerEvent := eventsAPIEvent.InnerEvent
myUuid, err := getBotUuid(body)
err := gadget.Router.UpdateBotUID(body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

eventUser := userFromInnerEvent(&innerEvent)
// Ignore all events that Gadget produces to avoid infinite loops
if myUuid == eventUser {
if gadget.Router.BotUID == eventUser {
w.WriteHeader(http.StatusOK)
return
}
Expand All @@ -208,7 +179,7 @@ func (gadget Gadget) Run() error {

switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
trimmedMessage := stripBotMention(ev.Text, myUuid)
trimmedMessage := stripBotMention(ev.Text, gadget.Router.BotUID)
route, exists := gadget.Router.FindMentionRouteByMessage(trimmedMessage)
if !exists {
route = gadget.Router.DefaultMentionRoute
Expand All @@ -223,7 +194,7 @@ func (gadget Gadget) Run() error {

go route.Execute(*api, gadget.Router, *ev, trimmedMessage)
case *slackevents.MessageEvent:
trimmedMessage := stripBotMention(ev.Text, myUuid)
trimmedMessage := stripBotMention(ev.Text, gadget.Router.BotUID)
route, exists := gadget.Router.FindChannelMessageRouteByMessage(trimmedMessage)
if !exists {
w.WriteHeader(http.StatusNotFound)
Expand Down
2 changes: 1 addition & 1 deletion router/channel_message.go
Expand Up @@ -11,7 +11,7 @@ type ChannelMessageRoute struct {
Plugin func(router Router, route Route, api slack.Client, ev slackevents.MessageEvent, message string)
}

// mentionRoutesSortedByPriority implements Sort such that those with higher priority are first
// channelMessageRoutesSortedByPriority implements Sort such that those with higher priority are first
type channelMessageRoutesSortedByPriority []ChannelMessageRoute

// Execute calls Plugin()
Expand Down
40 changes: 40 additions & 0 deletions router/router.go
@@ -1,6 +1,8 @@
package router

import (
"encoding/json"
"errors"
"regexp"
"sort"

Expand All @@ -26,6 +28,23 @@ type Router struct {
DefaultMentionRoute MentionRoute
DeniedMentionRoute MentionRoute
DbConnection *gorm.DB
BotUID string
}

// this is required because slack-go doesn't seem to provide a way to get the bot's own ID
type EventsAPICallbackEvent struct {
Type string `json:"type"`
Token string `json:"token"`
TeamID string `json:"team_id"`
APIAppID string `json:"api_app_id"`
Authoritzations []EventMessageAuthorization `json:"authorizations"`
EventID string `json:"event_id"`
EventTime int `json:"event_time"`
EventContext string `json:"event_context"`
}
type EventMessageAuthorization struct {
UserId string `json:"user_id"`
TeamId string `json:"team_id"`
}

// NewRouter returns a new Router
Expand All @@ -36,6 +55,27 @@ func NewRouter() *Router {
return &newRouter
}

// UpdateUID sets the UID field from an event body. Only updates if currently empty
func (r *Router) UpdateBotUID(body []byte) error {
if r.BotUID != "" {
return nil
}
uid, err := getBotUidFromBody(body)
r.BotUID = uid
return err
}

func getBotUidFromBody(body []byte) (string, error) {
var authorizedUsers EventsAPICallbackEvent
json.Unmarshal([]byte(body), &authorizedUsers)

if len(authorizedUsers.Authoritzations) > 0 {
return authorizedUsers.Authoritzations[0].UserId, nil
} else {
return "", errors.New("Weird")
}
}

// SetupDb migrates the shcemas
func (router Router) SetupDb() {
// Migrate the schema
Expand Down

0 comments on commit 33240c5

Please sign in to comment.