-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
202 lines (176 loc) · 5.42 KB
/
function.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
// Package p contains an HTTP Cloud Function.
package p
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var groupChatNote string
// PostWebhook ...
func PostWebhook(w http.ResponseWriter, r *http.Request) {
botToken := os.Getenv("BOT_API_TOKEN")
gcpProject := os.Getenv("GCP_PROJECT")
funcRegion := os.Getenv("FUNCTION_REGION")
funcName := os.Getenv("K_SERVICE")
if funcName == "" || gcpProject == "" || botToken == "" || funcRegion == "" {
log.Print("Environment variables are missing!")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
webhookURL := fmt.Sprintf("https://%v-%v.cloudfunctions.net/%v", funcRegion, gcpProject, funcName)
log.Println("Using webhook URL:", webhookURL)
bot, err := tg.NewBotAPI(botToken)
if err != nil {
log.Fatal(err)
}
botName := "@" + bot.Self.UserName
bot.Debug = true
log.Printf("Authorized on account %s", botName)
groupChatNote = fmt.Sprintf("To get the ID for a group chat, invite %v to the chat", botName)
switch r.URL.RawQuery {
case "register":
log.Printf("Registering webhook...")
wh, _ := tg.NewWebhook(webhookURL)
if _, err := bot.Request(wh); err != nil {
fmt.Fprint(w, "Failed to register webhook")
log.Print(err)
} else {
fmt.Fprint(w, "Webhook registered")
}
return
case "unregister":
log.Printf("Unregistering webhook...")
payload := tg.DeleteWebhookConfig{DropPendingUpdates: true}
if _, err := bot.Request(payload); err != nil {
fmt.Fprint(w, "Failed to unregister webhook")
log.Print(err)
} else {
fmt.Fprint(w, "Webhook unregistered")
}
return
default:
update := tg.Update{}
if err := json.NewDecoder(r.Body).Decode(&update); err != nil {
if err != io.EOF {
log.Printf("JSON Error: %v", err)
}
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if msg := update.Message; msg != nil {
var reply *tg.MessageConfig
isDirectedMessage := strings.HasPrefix(msg.Text, botName)
isForwarded := msg.ForwardFrom != nil
isPrivate := msg.Chat != nil && msg.Chat.Type == "private"
if isPrivate {
if isForwarded {
if msg.ForwardFromChat != nil {
reply = replyFromChat(msg, msg.ForwardFromChat)
} else {
reply = replyFromUser(msg, msg.ForwardFrom)
}
} else {
reply_msg := tg.NewMessage(msg.From.ID, "To get the ID of a chat, forward a message to this chat or start your message with "+botName)
reply = &reply_msg
}
}
if isDirectedMessage {
reply = replyFromChat(msg, msg.Chat)
}
if reply != nil {
if _, err = bot.Send(reply); err != nil {
log.Println("Error sending message:", err)
}
}
}
updateParts := getUpdateParts(&update)
log.Println("Got update with:", updateParts)
}
fmt.Fprint(w, "OK")
}
func getUpdateParts(update *tg.Update) (updateParts []string) {
if update.CallbackQuery != nil {
updateParts = append(updateParts, "CallbackQuery")
}
if update.ChannelPost != nil {
updateParts = append(updateParts, "ChannelPost")
}
if update.ChatJoinRequest != nil {
updateParts = append(updateParts, "ChatJoinRequest")
}
if update.ChatMember != nil {
updateParts = append(updateParts, "ChatMember")
}
if update.ChosenInlineResult != nil {
updateParts = append(updateParts, "ChosenInlineResult")
}
if update.EditedChannelPost != nil {
updateParts = append(updateParts, "EditedChannelPost")
}
if update.EditedMessage != nil {
updateParts = append(updateParts, "EditedMessage")
}
if update.InlineQuery != nil {
updateParts = append(updateParts, "InlineQuery")
}
if update.Message != nil {
updateParts = append(updateParts, "Message")
}
if update.MyChatMember != nil {
updateParts = append(updateParts, "MyChatMember")
}
if update.Poll != nil {
updateParts = append(updateParts, "Poll")
}
if update.PollAnswer != nil {
updateParts = append(updateParts, "PollAnswer")
}
if update.PreCheckoutQuery != nil {
updateParts = append(updateParts, "PreCheckoutQuery")
}
if update.ShippingQuery != nil {
updateParts = append(updateParts, "ShippingQuery")
}
return updateParts
}
func replyFromChat(msg *tg.Message, chat *tg.Chat) *tg.MessageConfig {
return createReply(msg, chat.ID, chat.Type, chat.Title, chat.UserName, "")
}
func replyFromUser(msg *tg.Message, user *tg.User) *tg.MessageConfig {
title := user.FirstName + " " + user.LastName
return createReply(msg, user.ID, "user", title, user.UserName, groupChatNote)
}
func createReply(msg *tg.Message, chatID int64, chatType string, title string, username string, note string) *tg.MessageConfig {
text := strings.Builder{}
addKeyVal(&text, "Chat ID", chatID)
addKeyVal(&text, "Type", chatType)
addKeyVal(&text, "Title", title)
addKeyVal(&text, "Username", username)
if note != "" {
text.WriteString("<i><b>Note:</b> ")
text.WriteString(note)
text.WriteString("</i>")
}
reply := tg.NewMessage(msg.From.ID, text.String())
reply.ParseMode = "HTML"
// For some reason, this causes "no such message" when referring to a message in a group chat
// reply.ReplyToMessageID = msg.MessageID
return &reply
}
func addKeyVal(sb *strings.Builder, key string, val interface{}) {
sb.WriteString("<b>")
sb.WriteString(key)
sb.WriteString(": </b>")
if val == "" {
sb.WriteString("<i>none</i>\n")
} else {
sb.WriteString("<code>")
sb.WriteString(fmt.Sprintf("%v", val))
sb.WriteString("</code>\n")
}
}