Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// v5.10.0からMessage Attachmentsのタイトルに絵文字とリンクを使用できるようになったため、その機能を試すサンプルコード
package main
import (
"fmt"
"log"
"github.com/mattermost/mattermost-server/model"
)
const (
MattermostURL = "http://localhost:8065"
ChannelId = "zyxwvutsrqponmlkjihgfedcba"
AccessToken = "abcdefghijklmnopqrstuvwxyz"
)
func main() {
client := model.NewAPIv4Client(MattermostURL)
client.AuthToken = AccessToken
client.AuthType = model.HEADER_BEARER
post := &model.Post{
Type: model.POST_SLACK_ATTACHMENT,
ChannelId: ChannelId,
Props: model.StringInterface{
"attachments": []*model.SlackAttachment{
{
Text: "Emoji :+1:\n[Link](https://www.google.com)",
Actions: []*model.PostAction{
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Button :+1:",
DataSource: "channels",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
},
},
},
},
}
if _, resp := client.CreatePost(post); resp.Error != nil {
log.Fatalf("Failed to create post: %#v", resp.Error)
}
}
// v5.10.0で追加された、自分のみ閲覧可能なInteractive Messageを投稿する投稿するサンプル
// Go言語SDKのCreatePostEphemeralメソッドからは作成できなかったため、現在はプラグインの`SendEphemeralPost`からしか作成できない?
package main
import (
"fmt"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
)
type Plugin struct {
plugin.MattermostPlugin
}
func main() {
plugin.ClientMain(&Plugin{})
}
func (p *Plugin) OnActivate() error {
return p.API.RegisterCommand(&model.Command{
Trigger: "mm510-eim",
AutoComplete: true,
})
}
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
post := &model.Post{
Type: model.POST_EPHEMERAL,
ChannelId: args.ChannelId,
Props: model.StringInterface{
"attachments": []*model.SlackAttachment{
{
Text: "test",
Actions: []*model.PostAction{
{
Type: model.POST_ACTION_TYPE_SELECT,
Name: "TEST",
DataSource: "channels",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/eim", ""),
},
},
},
},
},
},
}
post.Message = "SendEphemeralPost"
p.API.SendEphemeralPost(args.UserId, post)
return &model.CommandResponse{
Text: "test",
}, nil
}
// v5.24.0からInteractive Message Buttonの色を変えることができるようになったため、その機能を試すサンプルコード
// https://github.com/mattermost/mattermost-server/blob/8d0343d2eb3b45af56d2a6e3e1b26ed7932ff24e/model/integration_action.go#L61
package main
import (
"fmt"
"log"
"github.com/mattermost/mattermost-server/v5/model"
)
const (
MattermostURL = "http://localhost:8065"
ChannelId = "tgsgkqzjpfgrtgwpn8x3iuy13y"
AccessToken = "t1hpzdoqnib59dmm36td9rezfe"
)
func main() {
client := model.NewAPIv4Client(MattermostURL)
client.AuthToken = AccessToken
client.AuthType = model.HEADER_BEARER
post := &model.Post{
Type: model.POST_SLACK_ATTACHMENT,
ChannelId: ChannelId,
Props: model.StringInterface{
"attachments": []*model.SlackAttachment{
{
Text: "Emoji :+1:\n[Link](https://www.google.com)",
Actions: []*model.PostAction{
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Default",
Style: "default",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Primary",
Style: "primary",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Good",
Style: "good",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Warning",
Style: "warning",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Danger",
Style: "danger",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
{
Type: model.POST_ACTION_TYPE_BUTTON,
Name: "Hex",
Style: "#000000",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("%s/deco", ""),
},
},
},
},
},
},
}
if _, resp := client.CreatePost(post); resp.Error != nil {
log.Fatalf("Failed to create post: %#v", resp.Error)
}
}
package main
import (
"io"
"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("/multi", handleMultiResponse)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func handleMultiResponse(w http.ResponseWriter, r *http.Request) {
resp := model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: "Multi Response Sample",
ExtraResponses: []*model.CommandResponse{
{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: "Sample Response1",
},
{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: "Sample Response2(Ephemeral)",
},
},
}
w.Header().Add("Content-Type", "application/json")
io.WriteString(w, resp.ToJson())
}
// v5.8.0より有効になった、スラッシュコマンド実行時のレスポンスメッセージを別のチャンネルに投稿するサンプル
// L31で別のチャンネルのIDを指定しています。
package main
import (
"io"
"log"
"net/http"
"github.com/mattermost/mattermost-server/model"
)
const (
ServerURL = "http://localhost:8080"
MattermostURL = "http://localhost:8066"
OtherChannelID = "csg7u95tipn63ciybec6whb8ao"
)
func main() {
http.HandleFunc("/multi", handleMultiResponse)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
func handleMultiResponse(w http.ResponseWriter, r *http.Request) {
resp := model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
Text: "Multi Response Sample",
ExtraResponses: []*model.CommandResponse{
{
ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
ChannelId: OtherChannelID,
Text: "Sample Response1 to other channel",
},
{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: "Sample Response2(Ephemeral)",
},
},
}
w.Header().Add("Content-Type", "application/json")
io.WriteString(w, resp.ToJson())
}
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.