Skip to content

Commit

Permalink
add response per hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas committed Jun 30, 2017
1 parent 6fda29c commit 3ef385a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 14 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import (
"net/http"
)

type HandledEventResult struct {
AppliedRules []string `json:"applied_rules,omitempty"`
Message string `json:"message,omitempty"`
}

type Bot interface {
HandleEvent(r *http.Request)
HandleEvent(r *http.Request) *HandledEventResult
}

type bot struct {
configuration Configuration
}

func (b *bot) HandleEvent(r *http.Request) {
func (b *bot) HandleEvent(r *http.Request) *HandledEventResult {
result := &HandledEventResult{
AppliedRules: []string{},
}
data, process := buildFromRequest(b.configuration.GetClientConfig(), r)
if !process {
return
result.Message = "Skipping rules processing (could be not supported event type)"
return result
}
applied := make([]Rule, 0)
for _, rule := range b.configuration.GetRules() {
if rule.Accept(data) {
util.Logger.Debug("Accepting rule %s for '%s'", rule.Name(), data.GetTitle())
applied = append(applied, rule)
result.AppliedRules = append(result.AppliedRules, rule.Name())
}
}
for _, rule := range applied {
Expand All @@ -31,6 +41,7 @@ func (b *bot) HandleEvent(r *http.Request) {
action.Apply(b.configuration, data)
}
}
return result
}

func New(configPath string) (Bot, error) {
Expand Down
4 changes: 3 additions & 1 deletion rivi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ func main() {
log.Fatalln("Unable to start bot handler", err)
}
s := server.BotServer{Port: *port, Uri: *uri, Bot: run}
s.Run()
if err := s.Run(); err != nil {
log.Fatalln("Bot exited with error. %s", err)
}
}
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (server *BotServer) Run() error {
})
}
engine.POST(server.Uri, func(c *gin.Context) {
server.Bot.HandleEvent(c.Request)
c.Status(200)
result := server.Bot.HandleEvent(c.Request)
c.JSON(200, result)
})
return engine.Run(fmt.Sprintf(":%d", server.Port))
}

0 comments on commit 3ef385a

Please sign in to comment.