-
Notifications
You must be signed in to change notification settings - Fork 2
/
handleupdate.go
121 lines (108 loc) · 3.95 KB
/
handleupdate.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
package main
import (
"fmt"
"log"
"strings"
"github.com/go-telegram-bot-api/telegram-bot-api"
)
// handleUpdate handles updates sent from Telegram to us
func handleUpdate(update tgbotapi.Update) {
if update.InlineQuery != nil {
inputMsg := update.InlineQuery.Query
debugPrint("received an inline query from " + update.InlineQuery.From.UserName)
// inline query, check for amazon.* urls, get a random refcode and build the url
val, user, err := refs.GenAffiliate(inputMsg)
if !(err != nil || len(refs.ReferralCodes) < 0) {
payloads := []interface{}{}
happyString := fmt.Sprintf("The lucky winner is is @%s!\nHere's the reflink: %s\n", user, val)
okArticle := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID+"-ok", SuccessTitle, happyString)
okArticle.Description = val
payloads = append(payloads, okArticle)
currentUser, err := refs.GetUserByID(int64(update.InlineQuery.From.ID))
if err == nil { // user is present in the database
val, _ := refs.GenAffiliateWithRefcode(inputMsg, currentUser.Code)
personalString := fmt.Sprintf("This is my reflink -> %s\n", val)
personalArticle := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID+"-personal", PersonalTitle, personalString)
personalArticle.Description = val
payloads = append(payloads, personalArticle)
}
inlineConf := tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 0,
Results: payloads,
}
if _, err := botInstance.AnswerInlineQuery(inlineConf); err != nil {
log.Println(err)
}
}
} else if update.Message != nil {
debugPrint("received a private message from " + update.Message.From.UserName)
// private message, handle various messages
handleTelegramMessage(update.Message.Text, int64(update.Message.From.ID), update.Message.From.UserName)
} else if update.CallbackQuery != nil {
debugPrint("received a callback query from " + update.CallbackQuery.From.UserName)
handleTelegramMessage(update.CallbackQuery.Data, int64(update.CallbackQuery.From.ID), update.CallbackQuery.From.UserName)
}
}
func handleTelegramMessage(messageBlob string, chatid int64, username string) {
message := strings.Split(messageBlob, " ")
debugPrint(fmt.Sprintln("parsed message array:", message))
debugPrint("command -> " + message[0])
switch message[0] { // the first element of the array is usually the command
case ListPendingCommand:
debugPrint("called handleListPending()")
handleListPending(chatid)
case DenyCommand:
adminErr := checkIfAdministrator(int(chatid))
paramsErr := checkIfHasParameter(message, int(chatid), "deny")
if adminErr != nil || paramsErr != nil {
return
}
debugPrint("called handleDeny()")
handleDeny(message[1])
case AllowCommand:
adminErr := checkIfAdministrator(int(chatid))
paramsErr := checkIfHasParameter(message, int(chatid), "allow")
if adminErr != nil || paramsErr != nil {
return
}
debugPrint("called handleAllow()")
handleAllow(message[1])
case AddCodeCommand:
paramsErr := checkIfHasParameter(message, int(chatid), "addcode")
if paramsErr != nil {
return
}
debugPrint("called handleAddCode()")
handleAddCode(username, message[1], chatid)
default:
debugPrint("called handleDefault()")
handleDefault(chatid)
}
}
func handleListPending(user int64) {
listStr := refs.PrettyPrintList()
var message tgbotapi.MessageConfig
if user == userAdministrator {
message = tgbotapi.NewMessage(user, listStr)
} else {
message = tgbotapi.NewMessage(user, "You're not authorized to use this command.")
}
_, err := botInstance.Send(message)
checkError(err)
}
func handleDeny(user string) {
refs.Deny(user)
}
func handleAllow(user string) {
refs.Allow(user)
}
func handleAddCode(user string, refcode string, chatid int64) {
refs.AddCode(user, refcode, chatid)
}
func handleDefault(user int64) {
message := tgbotapi.NewMessage(user, "What? I didn't understood that command!")
_, err := botInstance.Send(message)
checkError(err)
}