bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/sched/slack/slack.go (about)

     1  // Package slack is for creating slack notifications inside of Bosun templates
     2  package slack
     3  
     4  import "bosun.org/models"
     5  
     6  // NewAttachment returns a Attachment structure with the color
     7  // and Timestamp set from the IncidentState.
     8  func NewAttachment(m *models.IncidentState) *Attachment {
     9  	a := new(Attachment)
    10  	a.Color = StatusColor(m.CurrentStatus)
    11  	a.Ts = m.LastAbnormalTime.Unix()
    12  	return a
    13  }
    14  
    15  // StatusColor returns a color string that corresponds to Slack's colors
    16  // based on the Status.
    17  func StatusColor(status models.Status) string {
    18  	switch status {
    19  	case models.StNormal:
    20  		return "good"
    21  	case models.StWarning:
    22  		return "warning"
    23  	case models.StCritical:
    24  		return "danger"
    25  	case models.StUnknown:
    26  		return "#439FE0"
    27  	}
    28  	return ""
    29  }
    30  
    31  // Attachment contains all the information for a slack message attachment and
    32  // has methods to Set/Append fields.
    33  type Attachment struct {
    34  	Color    string `json:"color,omitempty"`
    35  	Fallback string `json:"fallback"`
    36  
    37  	AuthorID      string `json:"author_id,omitempty"`
    38  	AuthorName    string `json:"author_name,omitempty"`
    39  	AuthorSubname string `json:"author_subname,omitempty"`
    40  	AuthorLink    string `json:"author_link,omitempty"`
    41  	AuthorIcon    string `json:"author_icon,omitempty"`
    42  
    43  	Title     string `json:"title,omitempty"`
    44  	TitleLink string `json:"title_link,omitempty"`
    45  	Pretext   string `json:"pretext,omitempty"`
    46  	Text      string `json:"text"`
    47  
    48  	ImageURL string `json:"image_url,omitempty"`
    49  	ThumbURL string `json:"thumb_url,omitempty"`
    50  
    51  	Fields  []interface{} `json:"fields,omitempty"`
    52  	Actions []interface{} `json:"actions,omitempty"`
    53  
    54  	Footer     string `json:"footer,omitempty"`
    55  	FooterIcon string `json:"footer_icon,omitempty"`
    56  
    57  	Ts int64 `json:"ts,omitempty"`
    58  }
    59  
    60  // Field is a structure for what slack expects as an item in the Fields slice of an Attachment.
    61  type Field struct {
    62  	Title string      `json:"title"`
    63  	Value interface{} `json:"value"`
    64  	Short bool        `json:"short"`
    65  }
    66  
    67  // Action is a struture for what slack expects as an item in the Actions slice of an Attachment.
    68  type Action struct {
    69  	Type  string `json:"type"`
    70  	Text  string `json:"text"`
    71  	URL   string `json:"url"`
    72  	Style string `json:"style,omitempty"`
    73  }
    74  
    75  // AddActions appends an Action to the Actions field of the Attachment.
    76  func (a *Attachment) AddActions(action ...interface{}) interface{} {
    77  	a.Actions = append(a.Actions, action...)
    78  	return "" // have to return something
    79  }
    80  
    81  // AddFields appends a Field to the Fields field of the Attachment.
    82  func (a *Attachment) AddFields(field ...interface{}) interface{} {
    83  	a.Fields = append(a.Fields, field...)
    84  	return "" // have to return something
    85  }
    86  
    87  // SetColor appends an Action to the Actions field of the Attachment.
    88  func (a *Attachment) SetColor(color string) interface{} {
    89  	a.Color = color
    90  	return ""
    91  }
    92  
    93  // SetFallback sets the FallBack field of the Attachment.
    94  func (a *Attachment) SetFallback(fallback string) interface{} {
    95  	a.Fallback = fallback
    96  	return ""
    97  }
    98  
    99  // SetAuthorID sets the AuthorID field of the Attachment.
   100  func (a *Attachment) SetAuthorID(authorID string) interface{} {
   101  	a.AuthorID = authorID
   102  	return ""
   103  }
   104  
   105  // SetAuthorName sets the AuthorName field of the Attachment.
   106  func (a *Attachment) SetAuthorName(authorName string) interface{} {
   107  	a.AuthorName = authorName
   108  	return ""
   109  }
   110  
   111  // SetAuthorSubname sets the AuthorSubname field of the Attachment.
   112  func (a *Attachment) SetAuthorSubname(authorSubname string) interface{} {
   113  	a.AuthorSubname = authorSubname
   114  	return ""
   115  }
   116  
   117  // SetAuthorLink sets the AuthorLink field of the Attachment.
   118  func (a *Attachment) SetAuthorLink(authorLink string) interface{} {
   119  	a.AuthorLink = authorLink
   120  	return ""
   121  }
   122  
   123  // SetAuthorIcon sets the AuthorIcon field of the Attachment.
   124  func (a *Attachment) SetAuthorIcon(authorIcon string) interface{} {
   125  	a.AuthorIcon = authorIcon
   126  	return ""
   127  }
   128  
   129  // SetTitle sets the Title field of the Attachment.
   130  func (a *Attachment) SetTitle(title string) interface{} {
   131  	a.Title = title
   132  	return ""
   133  }
   134  
   135  // SetTitleLink sets the TitleLink field of the Attachment.
   136  func (a *Attachment) SetTitleLink(titleLink string) interface{} {
   137  	a.TitleLink = titleLink
   138  	return ""
   139  }
   140  
   141  // SetPretext sets the Pretext field of the Attachment.
   142  func (a *Attachment) SetPretext(pretext string) interface{} {
   143  	a.Pretext = pretext
   144  	return ""
   145  }
   146  
   147  // SetText sets the Text field of the Attachment.
   148  func (a *Attachment) SetText(text string) interface{} {
   149  	a.Text = text
   150  	return ""
   151  }
   152  
   153  // SetImageURL sets the ImageURL field of the Attachment.
   154  func (a *Attachment) SetImageURL(url string) interface{} {
   155  	a.ImageURL = url
   156  	return ""
   157  }
   158  
   159  // SetThumbURL sets the ThumbURL field of the Attachment.
   160  func (a *Attachment) SetThumbURL(url string) interface{} {
   161  	a.ThumbURL = url
   162  	return ""
   163  }
   164  
   165  // SetFooter sets the Footer field of the Attachment.
   166  func (a *Attachment) SetFooter(footer string) interface{} {
   167  	a.Footer = footer
   168  	return ""
   169  }
   170  
   171  // SetFooterIcon sets the FooterIcon field of the Attachment.
   172  func (a *Attachment) SetFooterIcon(footerIcon string) interface{} {
   173  	a.FooterIcon = footerIcon
   174  	return ""
   175  }
   176  
   177  // SetTs sets the Ts field of the Attachment.
   178  func (a *Attachment) SetTs(ts int64) interface{} {
   179  	a.Ts = ts
   180  	return ""
   181  }