github.com/wgh-/mattermost-server@v4.8.0-rc2+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  
    13  const (
    14  	COMMAND_RESPONSE_TYPE_IN_CHANNEL = "in_channel"
    15  	COMMAND_RESPONSE_TYPE_EPHEMERAL  = "ephemeral"
    16  )
    17  
    18  type CommandResponse struct {
    19  	ResponseType string             `json:"response_type"`
    20  	Text         string             `json:"text"`
    21  	Username     string             `json:"username"`
    22  	IconURL      string             `json:"icon_url"`
    23  	Type         string             `json:"type"`
    24  	Props        StringInterface    `json:"props"`
    25  	GotoLocation string             `json:"goto_location"`
    26  	Attachments  []*SlackAttachment `json:"attachments"`
    27  }
    28  
    29  func (o *CommandResponse) ToJson() string {
    30  	b, _ := json.Marshal(o)
    31  	return string(b)
    32  }
    33  
    34  func CommandResponseFromHTTPBody(contentType string, body io.Reader) *CommandResponse {
    35  	if strings.TrimSpace(strings.Split(contentType, ";")[0]) == "application/json" {
    36  		return CommandResponseFromJson(body)
    37  	}
    38  	if b, err := ioutil.ReadAll(body); err == nil {
    39  		return CommandResponseFromPlainText(string(b))
    40  	}
    41  	return nil
    42  }
    43  
    44  func CommandResponseFromPlainText(text string) *CommandResponse {
    45  	return &CommandResponse{
    46  		Text: text,
    47  	}
    48  }
    49  
    50  func CommandResponseFromJson(data io.Reader) *CommandResponse {
    51  	decoder := json.NewDecoder(data)
    52  	var o CommandResponse
    53  
    54  	if err := decoder.Decode(&o); err != nil {
    55  		return nil
    56  	}
    57  
    58  	o.Attachments = StringifySlackFieldValue(o.Attachments)
    59  
    60  	return &o
    61  }