GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bot/webhook_handler.go at main · go-telegram/bot

Expand file tree

/

webhook_handler.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
82 lines (71 loc) · 2.17 KB
/

webhook_handler.go

Copy path

File metadata and controls

82 lines (71 loc) · 2.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package bot
import (
"context"
"encoding/json"
"io"
"net/http"
"github.com/go-telegram/bot/models"
)
// WebhookHandler returns an HTTP handler for updates processed by StartWebhook.
// For a valid update, it returns 200 once a worker takes the update, after
// which the worker runs its matchers, middlewares and handler. It returns 503
// if StartWebhook is not running, or if no worker takes the update before the
// run stops or the request is canceled, and 400 if the request body cannot be
// read. Such an update is not handled, and Telegram sends it again.
func (b *Bot) WebhookHandler() http.HandlerFunc {
return func(w 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)
w.WriteHeader(http.StatusBadRequest)
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)
}
if !b.queueWebhookUpdate(req.Context(), update) {
w.WriteHeader(http.StatusServiceUnavailable)
}
}
}
func (b *Bot) queueWebhookUpdate(ctx context.Context, update *models.Update) bool {
b.updatesMx.Lock()
run := b.updatesRun
b.updatesMx.Unlock()
if run == nil {
b.error("webhook update rejected: StartWebhook is not running")
return false
}
if !run.webhook || run.ctx.Err() != nil || ctx.Err() != nil {
return false
}
dispatched := make(chan struct{})
requestDone := ctx.Done()
select {
case run.queue <- queuedUpdate{update: update, dispatched: dispatched, requestDone: requestDone}:
case <-requestDone:
return false
case <-run.ctx.Done():
return false
}
// The handoff is unbuffered, so the request and the worker agree on
// whether the worker took the update.
select {
case <-dispatched:
return true
case <-requestDone:
return false
case <-run.ctx.Done():
return false
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.