Last active
December 15, 2018 06:52
-
-
Save kaakaa/1f87869f0d6520a2f645f8af014be1a5 to your computer and use it in GitHub Desktop.
Mattermost Interactive Dialog sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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