Skip to content

Commit

Permalink
Add example usage of outgoing webhooks (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei authored Dec 18, 2023
1 parent 308bdd6 commit d6a03f3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ This demo implementation sends back whether or not the plugin hooks are currentl
is used by the web app to recover from a network reconnection and synchronize the state of the
plugin's hooks.

It also implements a receiver for outgoing webhooks. To utilize that, create an Outgoing Webhook
using the following configuration:
- Title: Choose as you want
- Content Type: `application/json`
- Channel: Pick any channel
- Callback URLs: `http://localhost:8065/plugins/com.mattermost.demo-plugin/webhook/outgoing`

Leave the rest of the fields with their default value.

Now post a message in the selected channel. You will see a webhook response, which contains the payload the plugin received.

## [message_hooks.go](message_hooks.go)

### MessageWillBePosted
Expand Down
28 changes: 28 additions & 0 deletions server/http_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (p *Plugin) initializeAPI() {
router.HandleFunc("/dynamic_arg_test_url", p.handleDynamicArgTest)
router.HandleFunc("/check_auth_header", p.handleCheckAuthHeader)

webhook := router.PathPrefix("/webhook").Subrouter()
webhook.Use(p.withDelay)
webhook.HandleFunc("/outgoing", p.handleOutgoingWebhook).Methods(http.MethodPost)

interativeRouter := router.PathPrefix("/interactive").Subrouter()
interativeRouter.Use(p.withDelay)
interativeRouter.HandleFunc("/button/1", p.handleInteractiveAction)
Expand Down Expand Up @@ -103,6 +107,30 @@ func (p *Plugin) handleCheckAuthHeader(w http.ResponseWriter, r *http.Request) {
}
}

func (p *Plugin) handleOutgoingWebhook(w http.ResponseWriter, r *http.Request) {
var request model.OutgoingWebhookPayload
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
p.API.LogError("Failed to decode OutgoingWebhookPayload", "err", err)
w.WriteHeader(http.StatusBadRequest)
return
}
defer r.Body.Close()

s, err := PrettyJSON(request)
if err != nil {
p.API.LogError("Failed to Marshal payload back to JSON", "err", err.Error())
return
}

text := "```\n" + s + "\n```"
resp := model.OutgoingWebhookResponse{
Text: &text,
}

p.writeJSON(w, resp)
}

func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {
var request model.SubmitDialogRequest
err := json.NewDecoder(r.Body).Decode(&request)
Expand Down
13 changes: 13 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"encoding/json"
)

func PrettyJSON(in interface{}) (string, error) {
bb, err := json.MarshalIndent(in, "", " ")
if err != nil {
return "", err
}
return string(bb), nil
}

0 comments on commit d6a03f3

Please sign in to comment.