-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
plugin.go
279 lines (226 loc) · 5.13 KB
/
plugin.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/appleboy/drone-facebook/template"
"gopkg.in/telegram-bot-api.v4"
)
type (
// Repo information.
Repo struct {
Owner string
Name string
}
// Build information.
Build struct {
Event string
Number int
Commit string
Message string
Branch string
Author string
Status string
Link string
Started float64
Finished float64
}
// Config for the plugin.
Config struct {
Token string
Debug bool
To []string
Message []string
Photo []string
Document []string
Sticker []string
Audio []string
Voice []string
Location []string
Video []string
Venue []string
Format string
}
// Plugin values.
Plugin struct {
Repo Repo
Build Build
Config Config
}
// Location format
Location struct {
Title string
Address string
Latitude float64
Longitude float64
}
)
func trimElement(keys []string) []string {
var newKeys []string
for _, value := range keys {
value = strings.Trim(value, " ")
if len(value) == 0 {
continue
}
newKeys = append(newKeys, value)
}
return newKeys
}
func fileExist(keys []string) []string {
var newKeys []string
for _, value := range keys {
if _, err := os.Stat(value); os.IsNotExist(err) {
continue
}
newKeys = append(newKeys, value)
}
return newKeys
}
func convertLocation(value string) (Location, bool) {
var latitude, longitude float64
var title, address string
var err error
values := trimElement(strings.Split(value, ","))
if len(values) < 2 {
return Location{}, true
}
if len(values) > 2 {
title = values[2]
}
if len(values) > 3 {
title = values[2]
address = values[3]
}
latitude, err = strconv.ParseFloat(values[0], 64)
if err != nil {
log.Println(err.Error())
return Location{}, true
}
longitude, err = strconv.ParseFloat(values[1], 64)
if err != nil {
log.Println(err.Error())
return Location{}, true
}
return Location{
Title: title,
Address: address,
Latitude: latitude,
Longitude: longitude,
}, false
}
func parseID(keys []string) []int64 {
var newKeys []int64
for _, value := range keys {
id, err := strconv.ParseInt(value, 10, 64)
if err != nil {
log.Println(err.Error())
continue
}
newKeys = append(newKeys, id)
}
return newKeys
}
// Exec executes the plugin.
func (p Plugin) Exec() error {
if len(p.Config.Token) == 0 || len(p.Config.To) == 0 {
log.Println("missing telegram token or user list")
return errors.New("missing telegram token or user list")
}
var message []string
if len(p.Config.Message) > 0 {
message = p.Config.Message
} else {
message = p.Message(p.Repo, p.Build)
}
bot, err := tgbotapi.NewBotAPI(p.Config.Token)
if err != nil {
log.Println("Initialize New Bot Error:", err.Error())
return err
}
bot.Debug = p.Config.Debug
// parse ids
ids := parseID(p.Config.To)
photos := fileExist(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document))
stickers := fileExist(trimElement(p.Config.Sticker))
audios := fileExist(trimElement(p.Config.Audio))
voices := fileExist(trimElement(p.Config.Voice))
videos := fileExist(trimElement(p.Config.Video))
locations := trimElement(p.Config.Location)
venues := trimElement(p.Config.Venue)
// send message.
for _, user := range ids {
for _, value := range trimElement(message) {
txt, err := template.RenderTrim(value, p)
if err != nil {
return err
}
msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format
p.Send(bot, msg)
}
for _, value := range photos {
msg := tgbotapi.NewPhotoUpload(user, value)
p.Send(bot, msg)
}
for _, value := range documents {
msg := tgbotapi.NewDocumentUpload(user, value)
p.Send(bot, msg)
}
for _, value := range stickers {
msg := tgbotapi.NewStickerUpload(user, value)
p.Send(bot, msg)
}
for _, value := range audios {
msg := tgbotapi.NewAudioUpload(user, value)
msg.Title = "Audio Message."
p.Send(bot, msg)
}
for _, value := range voices {
msg := tgbotapi.NewVoiceUpload(user, value)
p.Send(bot, msg)
}
for _, value := range videos {
msg := tgbotapi.NewVideoUpload(user, value)
msg.Caption = "Video Message"
p.Send(bot, msg)
}
for _, value := range locations {
location, empty := convertLocation(value)
if empty == true {
continue
}
msg := tgbotapi.NewLocation(user, location.Latitude, location.Longitude)
p.Send(bot, msg)
}
for _, value := range venues {
location, empty := convertLocation(value)
if empty == true {
continue
}
msg := tgbotapi.NewVenue(user, location.Title, location.Address, location.Latitude, location.Longitude)
p.Send(bot, msg)
}
}
return nil
}
// Send bot message.
func (p Plugin) Send(bot *tgbotapi.BotAPI, msg tgbotapi.Chattable) {
_, err := bot.Send(msg)
if err != nil {
log.Println(err.Error())
}
}
// Message is plugin default message.
func (p Plugin) Message(repo Repo, build Build) []string {
return []string{fmt.Sprintf("[%s] <%s> (%s)『%s』by %s",
build.Status,
build.Link,
build.Branch,
build.Message,
build.Author,
)}
}