-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathtelegram.go
More file actions
58 lines (47 loc) · 1.49 KB
/
Copy pathtelegram.go
File metadata and controls
58 lines (47 loc) · 1.49 KB
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
package main
import (
"encoding/json"
"strconv"
"github.com/abhinavxd/libredesk/internal/inbox"
"github.com/abhinavxd/libredesk/internal/inbox/channel/telegram"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
func handleTelegramWebhook(r *fastglue.Request) error {
var app = r.Context.(*App)
inboxID, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string))
if err != nil || inboxID == 0 {
r.RequestCtx.SetStatusCode(fasthttp.StatusBadRequest)
return nil
}
inb, err := app.inbox.Get(inboxID)
if err != nil {
r.RequestCtx.SetStatusCode(fasthttp.StatusNotFound)
return nil
}
if inb.Channel() != inbox.ChannelTelegram {
r.RequestCtx.SetStatusCode(fasthttp.StatusBadRequest)
return nil
}
tgInbox, ok := inb.(*telegram.Telegram)
if !ok {
r.RequestCtx.SetStatusCode(fasthttp.StatusInternalServerError)
return nil
}
// Verify webhook authenticity using X-Telegram-Bot-Api-Secret-Token header.
secretToken := string(r.RequestCtx.Request.Header.Peek("X-Telegram-Bot-Api-Secret-Token"))
if !tgInbox.VerifyWebhook(secretToken) {
r.RequestCtx.SetStatusCode(fasthttp.StatusUnauthorized)
return nil
}
var update telegram.Update
if err := json.Unmarshal(r.RequestCtx.PostBody(), &update); err != nil {
r.RequestCtx.SetStatusCode(fasthttp.StatusBadRequest)
return nil
}
if err := tgInbox.ProcessWebhookUpdate(update); err != nil {
app.lo.Error("telegram webhook: error processing update", "error", err)
}
r.RequestCtx.SetStatusCode(fasthttp.StatusOK)
return nil
}