-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
278 lines (270 loc) · 9.43 KB
/
update.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var modeString []string = []string{"0空白", "1文字", "2圖片", "3影片", "4動畫", "5多圖組", "6文件"}
func getUpdates(bot *tgbotapi.BotAPI) {
var u tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
u.Timeout = config.Timeout
var updates tgbotapi.UpdatesChannel = bot.GetUpdatesChan(u)
var timers map[string]*time.Ticker = make(map[string]*time.Ticker)
var medias map[string][]interface{} = make(map[string][]interface{})
var tousrs map[string]int64 = make(map[string]int64)
for update := range updates {
dataCounts[0]++
if update.Message == nil || chatID(update, bot) {
continue
}
var text string = update.Message.Text
if len(update.Message.Caption) > 0 {
text = update.Message.Caption
}
var isCommand bool = (len(text) > 0 && text[0] == '/')
if isCommand {
var isOK bool = false
for _, id := range config.Whitelist {
if update.Message.From.ID == id {
isOK = true
}
}
if !isOK {
log.Println("未授權的使用者使用命令: ", update.Message.From.ID)
continue
}
}
var mode int8 = 0
var msg tgbotapi.Chattable
// var fromUser ChatObj
// var toUser ChatObj
// var fromChat ChatObj
// var toChat ChatObj
var toChat string = ""
var toChatID int64 = -1
var toChannel bool = false
var defaultTo bool = false
// if update.Message.IsCommand() {
// var userCommand string = update.Message.Command()
// var userCommandArg string = update.Message.CommandArguments()
// log.Println("收到指令: ", userCommand, userCommandArg)
// }
// fromChat = ChatObj{ID: update.Message.Chat.ID, Title: update.Message.Chat.UserName}
// fromUser = ChatObj{ID: update.Message.From.ID, Title: update.Message.From.UserName}
var logCache []string = []string{}
var message *tgbotapi.Message = update.Message
logCache = append(logCache, fmt.Sprintf("收到來自會話 %s(%d) 裡 %s(%d) 的訊息:%s", update.Message.Chat.UserName, update.Message.Chat.ID, update.Message.From.UserName, update.Message.From.ID, text))
_, inTousrs := tousrs[message.MediaGroupID]
if update.Message.ReplyToMessage != nil { // && update.Message.ReplyToMessage.From.ID == bot.Self.ID
message = update.Message.ReplyToMessage
if len(message.Caption) > 0 {
text = text + " " + message.Caption
}
logCache = append(logCache, fmt.Sprintf("需要轉發的訊息來自 %s (%d): %s\n", message.From.UserName, message.From.ID, message.Text))
}
var isMediaGroup bool = len(message.MediaGroupID) > 0
if isMediaGroup {
log.Println("多圖組: ", message.MediaGroupID)
}
if isCommand { // || update.Message.IsCommand()
var textUnit []string = strings.Split(text, " ")
var cmd string = textUnit[0]
textUnit = textUnit[1:]
text = strings.Join(textUnit, " ")
toChannel, toChat = cmdTChat(cmd)
if len(toChat) == 0 {
continue
}
toChatID, _ = strconv.ParseInt(toChat, 10, 64)
var logt = fmt.Sprintf("已指定收件人: %d", toChatID)
if toChannel {
logt += " (頻道)"
}
logCache = append(logCache, logt)
} else if !inTousrs && update.Message.Chat.ID == update.Message.From.ID && config.DefTo != -1 {
toChatID = config.DefTo
logCache = append(logCache, fmt.Sprintf("已指定為預設收件人: %d", toChatID))
defaultTo = true
}
text = filterTwitterURL(text)
var fileID tgbotapi.FileID
if message.Photo != nil || message.Video != nil || message.Animation != nil || message.Document != nil {
var photo tgbotapi.InputMediaPhoto
var video tgbotapi.InputMediaVideo
var animation tgbotapi.InputMediaAnimation
var file tgbotapi.InputMediaDocument
if message.Video != nil {
mode = 3
fileID = tgbotapi.FileID(message.Video.FileID)
video = tgbotapi.NewInputMediaVideo(fileID)
if medias[message.MediaGroupID] == nil {
text = config.Head.Video + text
video.Caption = text
}
} else if message.Photo != nil {
mode = 2
fileID = tgbotapi.FileID(message.Photo[0].FileID)
photo = tgbotapi.NewInputMediaPhoto(fileID)
if medias[message.MediaGroupID] == nil {
text = config.Head.Photo + text
photo.Caption = text
}
} else if message.Animation != nil {
mode = 4
fileID = tgbotapi.FileID(message.Animation.FileID)
animation = tgbotapi.NewInputMediaAnimation(fileID)
if medias[message.MediaGroupID] == nil {
text = config.Head.Animation + text
animation.Caption = text
}
} else if message.Document != nil {
mode = 6
fileID = tgbotapi.FileID(message.Document.FileID)
file = tgbotapi.NewInputMediaDocument(fileID)
if medias[message.MediaGroupID] == nil {
text = config.Head.Document + text
file.Caption = text
}
}
logCache = append(logCache, fmt.Sprintf("收到的資訊型別: %s", modeString[mode]))
if isMediaGroup {
var nMedia []interface{} = make([]interface{}, 0)
if toChannel || !inTousrs {
tousrs[message.MediaGroupID] = toChatID
}
if mode == 2 {
if medias[message.MediaGroupID] != nil {
nMedia = append(medias[message.MediaGroupID], photo)
} else {
nMedia = append(nMedia, photo)
}
} else if mode == 3 {
if medias[message.MediaGroupID] != nil {
nMedia = append(medias[message.MediaGroupID], video)
} else {
nMedia = append(nMedia, video)
}
} else if mode == 4 {
if medias[message.MediaGroupID] != nil {
nMedia = append(medias[message.MediaGroupID], animation)
} else {
nMedia = append(nMedia, animation)
}
} else if mode == 6 {
if medias[message.MediaGroupID] != nil {
nMedia = append(medias[message.MediaGroupID], file)
} else {
nMedia = append(nMedia, file)
}
}
medias[update.Message.MediaGroupID] = nMedia
// log.Println("新增媒體", update.Message.MediaGroupID, len(medias[update.Message.MediaGroupID]))
}
} else if len(text) > 0 {
mode = 1
text = config.Head.Text + text
logCache = append(logCache, fmt.Sprintf("收到的資訊型別: %s", modeString[mode]))
if update.Message.Chat.ID == update.Message.From.ID && len(config.Nitter) > 0 && tweetGETchk(text) {
logCache = append(logCache, "有且僅有一個推特連結,開始解析。")
logCaches(logCache)
go tweetPush(update, bot, text, toChannel, toChat)
continue
}
}
if toChatID == -1 {
if update.Message.From.ID == config.Debug {
toChatID = config.Debug
} else {
continue
}
}
if defaultTo && (mode != 2 && mode != 3 && !inTousrs || (len(text) == 0 || !strings.Contains(text, "http") || !strings.Contains(text, "://") || !strings.Contains(text, ".") || !strings.Contains(text, "/"))) && inTousrs {
logCache = append(logCache, fmt.Sprintf("無效投稿: mode%d: %s\n", mode, text))
toChatID = update.Message.Chat.ID
medias = make(map[string][]interface{})
isMediaGroup = false
mode = 1
toChannel = false
text = "直接发送给我图片或者视频,同时标注来源链接,可以进行投稿。\n投稿会经过编辑审查后,才会发布到频道。\n当前发送的内容无效喵。"
}
if !isMediaGroup {
switch mode {
case 1:
if toChannel {
msg = tgbotapi.NewMessageToChannel(toChat, text)
} else {
msg = tgbotapi.NewMessage(toChatID, text)
}
case 2:
if toChannel {
var photoMsg tgbotapi.PhotoConfig = tgbotapi.NewPhotoToChannel(toChat, fileID)
photoMsg.Caption = text
msg = photoMsg
} else {
var photoMsg tgbotapi.PhotoConfig = tgbotapi.NewPhoto(toChatID, fileID)
photoMsg.Caption = text
msg = photoMsg
}
case 3:
var videoMsg tgbotapi.VideoConfig = tgbotapi.NewVideo(toChatID, fileID)
videoMsg.Caption = text
msg = videoMsg
case 4:
var animationMsg tgbotapi.AnimationConfig = tgbotapi.NewAnimation(toChatID, fileID)
animationMsg.Caption = text
msg = animationMsg
case 6:
var fileMsg tgbotapi.DocumentConfig = tgbotapi.NewDocument(toChatID, fileID)
fileMsg.Caption = text
msg = fileMsg
default:
return
}
logCaches(logCache)
if _, err := bot.Send(msg); err != nil {
dataCounts[2]++
log.Printf("向 %d 傳送 %s类型 訊息失敗[N]: %s\n", toChatID, modeString[mode], err)
health(false)
} else {
dataCounts[1]++
log.Printf("已向 %d 傳送 %s类型 訊息: %s\n", toChatID, modeString[mode], text)
health(true)
}
} else {
var MediaGroupID string = update.Message.MediaGroupID
if !inTousrs {
tousrs[MediaGroupID] = toChatID
}
if timers[MediaGroupID] == nil {
newTicker := time.NewTicker(5 * time.Second)
timers[MediaGroupID] = newTicker
go func() {
<-newTicker.C
// logCache = append(logCache, fmt.Sprintf("提交媒體 %s %d", MediaGroupID, len(medias[MediaGroupID])))
timers[MediaGroupID].Stop()
var mediaGroup []interface{} = medias[MediaGroupID]
if len(mediaGroup) > 0 {
var mediaGroupMsg tgbotapi.MediaGroupConfig = tgbotapi.NewMediaGroup(tousrs[MediaGroupID], mediaGroup)
msg = mediaGroupMsg
logCaches(logCache)
if _, err := bot.Send(msg); err != nil {
dataCounts[2]++
log.Printf("向 %d 傳送多圖訊息失敗[G]: %s", tousrs[MediaGroupID], err)
health(false)
} else {
dataCounts[1]++
log.Printf("已向 %d 傳送多圖訊息: %s", tousrs[MediaGroupID], text)
health(true)
}
}
delete(timers, MediaGroupID)
delete(medias, MediaGroupID)
delete(tousrs, MediaGroupID)
}()
}
}
}
}