Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mattermost Interactive Dialog sample
package main
import (
"fmt"
"log"
"net/http"
"github.com/mattermost/mattermost-server/model"
)
const (
ServerURL = "http://localhost:8080"
MattermostURL = "http://localhost:8065"
AccessToken = "yj4xzckhiig5urrdzm4ydduccc"
)
var client *model.Client4
func main() {
client = model.NewAPIv4Client(MattermostURL)
client.SetOAuthToken(AccessToken)
http.HandleFunc("/dialog", handleDialogRequest)
http.HandleFunc("/callback", handleCallBack)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func handleDialogRequest(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
r.ParseForm()
dialog := model.OpenDialogRequest{
TriggerId: r.Form["trigger_id"][0],
URL: fmt.Sprintf("%s/callback", ServerURL),
Dialog: model.Dialog{
Title: "SampleDialog",
SubmitLabel: "Submit",
Elements: []model.DialogElement{
{
DisplayName: "Text",
Name: "text",
Type: "textarea",
SubType: "text",
},
{
DisplayName: "Channel",
Name: "channel",
Type: "select",
DataSource: "channels",
},
},
},
}
if ok, resp := client.OpenInteractiveDialog(dialog); !ok {
log.Fatalf("%#v", resp)
}
}
func handleCallBack(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
request := model.SubmitDialogRequestFromJson(r.Body)
text := request.Submission["text"].(string)
channel := request.Submission["channel"].(string)
client.CreatePost(&model.Post{
ChannelId: channel,
Message: fmt.Sprintf("```\n%s\n```", text),
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.