Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example usage of outgoing webhooks #171

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}