github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/command_response.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "io/ioutil" 10 "strings" 11 12 "github.com/mattermost/mattermost-server/utils/jsonutils" 13 ) 14 15 const ( 16 COMMAND_RESPONSE_TYPE_IN_CHANNEL = "in_channel" 17 COMMAND_RESPONSE_TYPE_EPHEMERAL = "ephemeral" 18 ) 19 20 type CommandResponse struct { 21 ResponseType string `json:"response_type"` 22 Text string `json:"text"` 23 Username string `json:"username"` 24 IconURL string `json:"icon_url"` 25 Type string `json:"type"` 26 Props StringInterface `json:"props"` 27 GotoLocation string `json:"goto_location"` 28 Attachments []*SlackAttachment `json:"attachments"` 29 } 30 31 func (o *CommandResponse) ToJson() string { 32 b, _ := json.Marshal(o) 33 return string(b) 34 } 35 36 func CommandResponseFromHTTPBody(contentType string, body io.Reader) (*CommandResponse, error) { 37 if strings.TrimSpace(strings.Split(contentType, ";")[0]) == "application/json" { 38 return CommandResponseFromJson(body) 39 } 40 if b, err := ioutil.ReadAll(body); err == nil { 41 return CommandResponseFromPlainText(string(b)), nil 42 } 43 return nil, nil 44 } 45 46 func CommandResponseFromPlainText(text string) *CommandResponse { 47 return &CommandResponse{ 48 Text: text, 49 } 50 } 51 52 func CommandResponseFromJson(data io.Reader) (*CommandResponse, error) { 53 b, err := ioutil.ReadAll(data) 54 if err != nil { 55 return nil, err 56 } 57 58 var o CommandResponse 59 err = json.Unmarshal(b, &o) 60 if err != nil { 61 return nil, jsonutils.HumanizeJsonError(err, b) 62 } 63 64 o.Attachments = StringifySlackFieldValue(o.Attachments) 65 66 return &o, nil 67 }