-
Notifications
You must be signed in to change notification settings - Fork 68
/
webhook_handler.go
49 lines (40 loc) · 1018 Bytes
/
webhook_handler.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
package bot
import (
"encoding/json"
"io"
"net/http"
"github.com/go-telegram/bot/models"
)
func (b *Bot) WebhookHandler() http.HandlerFunc {
return func(_ http.ResponseWriter, req *http.Request) {
if b.webhookSecretToken != "" && req.Header.Get("X-Telegram-Bot-Api-Secret-Token") != b.webhookSecretToken {
b.error("invalid webhook secret token received from update")
return
}
body, errReadBody := io.ReadAll(req.Body)
if errReadBody != nil {
b.error("error read request body, %w", errReadBody)
return
}
update := &models.Update{}
errDecode := json.Unmarshal(body, update)
if errDecode != nil {
b.error("error decode request body, %s, %w", body, errDecode)
return
}
if b.isDebug {
b.debugHandler("webhook request '%s'", body)
}
select {
case <-req.Context().Done():
b.error("some updates lost, ctx done")
return
default:
}
select {
case b.updates <- update:
case <-req.Context().Done():
b.error("failed to send update, ctx done")
}
}
}