-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
182 lines (155 loc) · 4.52 KB
/
messages.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
package main
import (
"context"
_ "embed"
"fmt"
"log"
"regexp"
"strconv"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type ChatMessage struct {
DataType string `json:"type"`
MessageId int `json:"id"`
UserId int64 `json:"userId"`
Text string `json:"text"`
Date int64 `json:"date"`
ReplyToId int64 `json:"replyToId"`
User tgbotapi.User `json:"user"`
Message tgbotapi.Message `json:"messageDetails"`
}
func processUpdate(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
db := getUpdatesDB()
fmt.Printf("update %+v\n", update)
docId := fmt.Sprintf("tgupdate:%d", update.UpdateID)
if update.Message != nil {
replyToId := 0
if update.Message.ReplyToMessage != nil {
replyToId = update.Message.ReplyToMessage.MessageID
}
db.Put(context.TODO(), docId, ChatMessage{
DataType: "message",
MessageId: update.Message.MessageID,
UserId: int64(update.Message.From.ID),
Text: update.Message.Text,
User: *update.Message.From,
ReplyToId: int64(replyToId),
Date: int64(update.Message.Date),
Message: *update.Message,
})
processMessage(bot, *update.Message)
}
}
func processMessage(bot *tgbotapi.BotAPI, message tgbotapi.Message) {
re := regexp.MustCompile(`[\s@]+`)
messageChunks := re.Split(message.Text, 5)
if len(messageChunks) >= 1 && stringInSlice(messageChunks[0], AllowedCommands) {
switch messageChunks[0] {
case "/stats":
stats := getStats(int(message.Chat.ID))
statsFormatted := "Хуїстика:\n"
for _, v := range stats {
statsFormatted += fmt.Sprintf("%s <b>%s</b>: повідомлень: %d, карма: %d\n",
calculateDesignation(v.Karma, int(v.MessageCount)),
getName(&v.User),
v.MessageCount,
v.Karma)
}
responseConfig := tgbotapi.NewMessage(
message.Chat.ID,
statsFormatted,
)
// TODO: Investigate escaping options
responseConfig.ParseMode = "HTML"
bot.Send(responseConfig)
case "/showRanksTest":
for _, chunk := range getRanksDescriptionsSplitted() {
rankDesk := ""
for _, v := range chunk {
rankDesk = rankDesk + fmt.Sprintf("%s -> Rank: %s - minmsg: %d congrat:%s pic: %s\n",
v.Emoji,
v.RankName,
v.MinMessages,
v.CongratulationMessage,
v.Picture,
)
}
responseConfig := tgbotapi.NewMessage(
message.Chat.ID,
rankDesk,
)
bot.Send(responseConfig)
}
for _, chunk := range getKarmaRanksDescriptionsSplitted() {
rankDesk := ""
for _, v := range chunk {
rankDesk = rankDesk + fmt.Sprintf("%d -> %s\n",
v.Karma,
v.Rank,
)
}
responseConfig := tgbotapi.NewMessage(
message.Chat.ID,
rankDesk,
)
bot.Send(responseConfig)
}
case "/rankDiag":
if len(messageChunks) != 3 {
return
} else {
s, err := strconv.Atoi(messageChunks[1])
if err != nil {
log.Printf("Incorrect Params %s", message.Text)
}
k, err := strconv.Atoi(messageChunks[2])
if err != nil {
log.Printf("Incorrect Params %s", message.Text)
}
responseConfig := tgbotapi.NewMessage(
message.Chat.ID,
calculateDesignation(k, s),
)
bot.Send(responseConfig)
}
case "/bledina":
go func() {
if !sraketa() {
log.Printf("Could not take screenshot")
return
}
responseConfig := tgbotapi.NewPhoto(message.Chat.ID, tgbotapi.FilePath("sraketa_current.png"))
bot.Send(responseConfig)
}()
}
}
if stringInSlice(message.Text, []string{"+", "-", "Дякую", "дякую", "дякі", "дяк", "thanks", "ty", "🔥", "👍"}) && message.ReplyToMessage != nil && !message.ReplyToMessage.From.IsBot {
switch message.Text {
case "+", "Дякую", "дякую", "дякі", "дяк", "thanks", "ty", "🔥", "👍":
voteUp(bot, message)
case "-":
voteDown(bot, message)
}
}
}
func deleteMessageWithDelay(bot *tgbotapi.BotAPI, message tgbotapi.Message, response tgbotapi.Message) {
go func(message tgbotapi.Message, response tgbotapi.Message) {
time.Sleep(10 * time.Second)
//delete := tgbotapi.NewDeleteMessage(message.Chat.ID, response.MessageID)
//bot.Send(delete)
// delete := tgbotapi.NewDeleteMessage(message.Chat.ID, message.MessageID)
// bot.Send(delete)
}(message, response)
}
func getName(user *tgbotapi.User) string {
return fmt.Sprintf("%s %s", user.FirstName, user.LastName)
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}