matterbridge/vendor/github.com/mattermost/platform/model/command_response.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-08-16 23:37:37 +02:00
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
2016-04-10 23:39:38 +02:00
// See License.txt for license information.
package model
import (
"encoding/json"
"io"
2018-02-09 00:11:04 +01:00
"io/ioutil"
"strings"
2016-04-10 23:39:38 +02:00
)
const (
COMMAND_RESPONSE_TYPE_IN_CHANNEL = "in_channel"
COMMAND_RESPONSE_TYPE_EPHEMERAL = "ephemeral"
)
type CommandResponse struct {
2017-03-25 21:04:10 +01:00
ResponseType string `json:"response_type"`
Text string `json:"text"`
Username string `json:"username"`
IconURL string `json:"icon_url"`
2018-02-09 00:11:04 +01:00
Type string `json:"type"`
Props StringInterface `json:"props"`
2017-03-25 21:04:10 +01:00
GotoLocation string `json:"goto_location"`
Attachments []*SlackAttachment `json:"attachments"`
2016-04-10 23:39:38 +02:00
}
func (o *CommandResponse) ToJson() string {
b, err := json.Marshal(o)
if err != nil {
return ""
} else {
return string(b)
}
}
2018-02-09 00:11:04 +01:00
func CommandResponseFromHTTPBody(contentType string, body io.Reader) *CommandResponse {
if strings.TrimSpace(strings.Split(contentType, ";")[0]) == "application/json" {
return CommandResponseFromJson(body)
}
if b, err := ioutil.ReadAll(body); err == nil {
return CommandResponseFromPlainText(string(b))
}
return nil
}
func CommandResponseFromPlainText(text string) *CommandResponse {
return &CommandResponse{
Text: text,
}
}
2016-04-10 23:39:38 +02:00
func CommandResponseFromJson(data io.Reader) *CommandResponse {
decoder := json.NewDecoder(data)
var o CommandResponse
2017-03-25 21:04:10 +01:00
if err := decoder.Decode(&o); err != nil {
2016-04-10 23:39:38 +02:00
return nil
}
2017-03-25 21:04:10 +01:00
2018-02-09 00:11:04 +01:00
o.Attachments = StringifySlackFieldValue(o.Attachments)
2017-03-25 21:04:10 +01:00
return &o
2016-04-10 23:39:38 +02:00
}