Package list golang-github-nlopes-slack / adc67bc
Add attachment type as Go struct (not []byte) Make Id consistent as Id instead of ID throughout the code Norberto Lopes 6 years ago
6 changed file(s) with 79 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
00 package slack
11
22 import (
3 "encoding/json"
34 "errors"
45 "net/url"
56 "strings"
1617 )
1718
1819 type chatResponseFull struct {
19 ChannelID string `json:"channel"`
20 ChannelId string `json:"channel"`
2021 Timestamp string `json:"ts"`
2122 Text string `json:"text"`
2223 SlackResponse
24 }
25
26 type AttachmentField struct {
27 Title string `json:"title"`
28 Value string `json:"value"`
29 Short string `json:"short"`
30 }
31
32 type Attachment struct {
33 Fallback string `json:"fallback"`
34
35 Color string `json:"color,omitempty"`
36
37 Pretext string `json:"pretext,omitempty"`
38
39 AuthorName string `json:"author_name,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
46 Text string `json:"text"`
47
48 Fields []AttachmentField `json:"fields,omitempty"`
2349 }
2450
2551 type PostMessageParameters struct {
2753 Username string
2854 Parse string
2955 LinkNames int
30 Attachments []byte
56 Attachments []Attachment
3157 UnfurlLinks bool
3258 UnfurlMedia bool
3359 IconURL string
6995 if err != nil {
7096 return "", "", err
7197 }
72 return response.ChannelID, response.Timestamp, nil
98 return response.ChannelId, response.Timestamp, nil
7399 }
74100
75101 func EscapeMessage(message string) string {
82108 return replacer.Replace(message)
83109 }
84110
85 func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (string, string, error) {
111 func (api *Slack) PostMessage(channelId string, text string, params PostMessageParameters) (channel string, timestamp string, err error) {
86112 values := url.Values{
87113 "token": {api.config.token},
88114 "channel": {channelId},
98124 values.Set("link_names", "1")
99125 }
100126 if params.Attachments != nil {
101 values.Set("attachments", string(params.Attachments))
127 attachments, err := json.Marshal(params.Attachments)
128 if err != nil {
129 return "", "", err
130 }
131 values.Set("attachments", string(attachments))
102132 }
103133 if params.UnfurlLinks == DEFAULT_MESSAGE_UNFURL_LINKS {
104134 values.Set("unfurl_links", "false")
117147 if err != nil {
118148 return "", "", err
119149 }
120 return response.ChannelID, response.Timestamp, nil
150 return response.ChannelId, response.Timestamp, nil
121151 }
122152
123153 func (api *Slack) UpdateMessage(channelId, timestamp, text string) (string, string, string, error) {
131161 if err != nil {
132162 return "", "", "", err
133163 }
134 return response.ChannelID, response.Timestamp, response.Text, nil
164 return response.ChannelId, response.Timestamp, response.Text, nil
135165
136166 }
0 package main
1
2 import (
3 "fmt"
4
5 "github.com/nlopes/slack"
6 )
7
8 func main() {
9 api := slack.New("YOUR_TOKEN_HERE")
10 params := slack.PostMessageParameters{}
11 attachment := slack.Attachment{
12 Pretext: "some pretext",
13 Text: "some text",
14 // Uncomment the following part to send a field too
15 /*
16 Fields: []slack.AttachmentField{
17 slack.AttachmentField{
18 Title: "a",
19 Value: "no",
20 },
21 },
22 */
23 }
24 params.Attachments = []slack.Attachment{attachment}
25 channelId, timestamp, err := api.PostMessage("CHANNEL_ID", "Some text", params)
26 if err != nil {
27 fmt.Printf("%s\n", err)
28 return
29 }
30 fmt.Printf("Message successfully sent to channel %s at %s", channelId, timestamp)
31 }
1919 type Comment struct {
2020 Id string `json:"id"`
2121 Timestamp JSONTime `json:"timestamp"`
22 UserID string `json:"user"`
22 UserId string `json:"user"`
2323 Comment string `json:"comment"`
2424 }
2525
3333 Mimetype string `json:"mimetype"`
3434 Filetype string `json:"filetype"`
3535 PrettyType string `json:"pretty_type"`
36 UserID string `json:"user"`
36 UserId string `json:"user"`
3737
3838 Mode string `json:"mode"`
3939 Editable bool `json:"editable"`
11
22 type OutgoingMessage struct {
33 Id int `json:"id"`
4 ChannelID string `json:"channel,omitempty"`
4 ChannelId string `json:"channel,omitempty"`
55 Text string `json:"text,omitempty"`
66 Type string `json:"type,omitempty"`
77 }
1313
1414 type Msg struct {
1515 Id string `json:"id"`
16 UserID string `json:"user,omitempty"`
17 ChannelID string `json:"channel,omitempty"`
16 UserId string `json:"user,omitempty"`
17 ChannelId string `json:"channel,omitempty"`
1818 Timestamp string `json:"ts,omitempty"`
1919 Text string `json:"text,omitempty"`
2020 Team string `json:"team,omitempty"`
3030
3131 type Presence struct {
3232 Presence string `json:"presence"`
33 UserID string `json:"user"`
33 UserId string `json:"user"`
3434 }
3535
3636 type Event struct {
5656 return &OutgoingMessage{
5757 Id: api.messageId,
5858 Type: "message",
59 ChannelID: channel,
59 ChannelId: channel,
6060 Text: text,
6161 }
6262 }
88
99 type UserTyping struct {
1010 Type string `json:"type"`
11 UserID string `json:"user"`
12 ChannelID string `json:"channel"`
11 UserId string `json:"user"`
12 ChannelId string `json:"channel"`
1313 }
1414
1515 type SlackEvent struct {
152152 // Allows us to have stats about latency and what not
153153 return
154154 case "presence_change":
155 //log.Printf("`%s is %s`\n", info.GetUserById(event.PUserID).Name, event.Presence)
155 //log.Printf("`%s is %s`\n", info.GetUserById(event.PUserId).Name, event.Presence)
156156 case "message":
157157 handleMessage(ch, event)
158158 case "channel_marked":