-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
126 lines (101 loc) · 2.5 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package slackreporting
import "net/url"
type message struct {
reporter *reporter
ts string
c string
}
func (m *message) Update(text string) error {
if m.ts != "" && m.c != "" {
err := m.updateMessage(text)
if err != nil {
err = m.postMessage(text)
}
return err
}
err := m.postMessage(text)
return err
}
func (m *message) Delete() error {
if m.ts != "" && m.c != "" {
err := m.deleteMessage()
return err
}
return nil
}
func (m *message) postMessage(text string) error {
args := make(url.Values)
args["text"] = []string{text}
args["channel"] = []string{m.reporter.opt.Channel}
if m.reporter.opt.Username != "" {
args["username"] = []string{m.reporter.opt.Username}
}
if m.reporter.opt.Icon != nil {
m.reporter.opt.Icon.apply(args)
}
m.reporter.printf("Posting message to '%s'...\n", m.reporter.opt.Channel)
var resp postResponse
err := m.reporter.callMethod("chat.postMessage", args, &resp)
if err != nil {
return err
}
m.ts = resp.Ts
m.c = resp.Channel
m.reporter.printf("Message has been posted: ts=%s, c=%s\n", m.ts, m.c)
return nil
}
type postResponse struct {
response
Ts string `json:"ts"`
Channel string `json:"channel"`
}
func (r postResponse) validate() (bool, string) {
return r.OK, r.Error
}
func (m *message) updateMessage(text string) error {
args := make(url.Values)
args["text"] = []string{text}
args["ts"] = []string{m.ts}
args["channel"] = []string{m.c}
m.reporter.printf("Updating message ts=%s, c=%s...\n", m.ts, m.c)
var resp updateResponse
err := m.reporter.callMethod("chat.update", args, &resp)
if err != nil {
return err
}
m.ts = resp.Ts
m.c = resp.Channel
m.reporter.printf("Message has been updated: ts=%s, c=%s\n", m.ts, m.c)
return nil
}
type updateResponse struct {
response
Ts string `json:"ts"`
Channel string `json:"channel"`
}
func (r updateResponse) validate() (bool, string) {
return r.OK, r.Error
}
func (m *message) deleteMessage() error {
args := make(url.Values)
args["ts"] = []string{m.ts}
args["channel"] = []string{m.c}
m.reporter.printf("Deleting message ts=%s, c=%s...\n", m.ts, m.c)
var resp deleteResponse
err := m.reporter.callMethod("chat.delete", args, &resp)
if err != nil {
return err
}
m.ts = ""
m.c = ""
m.reporter.printf("Message has been deleted: ts=%s, c=%s\n", resp.Ts, resp.Channel)
return nil
}
type deleteResponse struct {
response
Ts string `json:"ts"`
Channel string `json:"channel"`
}
func (r deleteResponse) validate() (bool, string) {
return r.OK, r.Error
}