-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathhandle_telegram_msg.go
87 lines (84 loc) · 2.77 KB
/
handle_telegram_msg.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
strings = import("strings")
func handleOGameMessage(msg) {
err = SendTelegram(TELEGRAM_CHAT_ID, "Bot: " + BotID + ", PlayerID: " + msg.SenderID + ", Assoc: " + msg.AssociationID + ", " + msg.Text)
if err != nil {
LogError("err: ", err)
}
}
func handleTelegramMessage(msg) {
if msg.ReplyToMessage.MessageID != 0 {
parts = strings.Split(msg.ReplyToMessage.Text, ", ")
botID = strings.Split(parts[0], "Bot: ")[1]
playerID = strings.Split(parts[1], "PlayerID: ")[1]
msgToSend = msg.Text
err = SendMessage(Atoi(playerID), msgToSend)
if err != nil {
LogError("SendMessage: ", err, playerID, msgToSend)
return
}
Print("Message was sent")
return
}
parts = strings.Split(msg.Text, " ")
if len(parts) >= 2 {
targetBotID = parts[0]
cmd = parts[1]
if targetBotID != BotID {
return // Return early, cmd is not for this bot
}
switch cmd {
// Message should have this format:
// <bot_id> msg <player_id> <message>
// eg: `5 msg 95828 How are you doing ?`
case "msg":
if len(parts) < 4 {
LogError("Invalid number of arguments for msg command")
return
}
playerID = Atoi(parts[2])
if playerID == 0 {
LogError("PlayerID argument must be an integer")
return
}
msgToSend = strings.Join(parts[3:], " ")
err = SendMessage(playerID, msgToSend)
if err != nil {
LogError("SendMessage: ", err, playerID, msgToSend)
return
}
Print("Message was sent")
// Message should have this format:
// <bot_id> msga <alliance_id> <message>
// eg: `5 msga 537 How are you doing ?`
case "msga":
if len(parts) < 4 {
LogError("Invalid number of arguments for msga command")
return
}
allianceID = Atoi(parts[2])
if allianceID == 0 {
LogError("allianceID argument must be an integer")
return
}
msgToSend = strings.Join(parts[3:], " ")
err = SendMessageAlliance(allianceID, msgToSend)
if err != nil {
LogError("SendMessage: ", err, allianceID, msgToSend)
return
}
Print("Message was sent")
default:
LogError("Unknown cmd: ", cmd)
}
} else {
Print("Receved: ", msg)
}
}
for {
select {
case msg = <-OnTelegramMessageReceivedCh:
handleTelegramMessage(msg)
case msg = <-OnChatMessageReceivedCh:
handleOGameMessage(msg)
}
}