webhook.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "io/ioutil"
6 "net/http"
7)
8
9// ListenForWebhook registers a http handler for a webhook.
10// Useful for Google App Engine or other places where you cannot
11// use a normal update chan.
12func (bot *BotAPI) ListenForWebhook(config WebhookConfig) {
13 bot.Updates = make(chan Update, 100)
14
15 http.HandleFunc("/"+config.Url.Path, func(w http.ResponseWriter, r *http.Request) {
16 bytes, _ := ioutil.ReadAll(r.Body)
17
18 var update Update
19 json.Unmarshal(bytes, &update)
20
21 bot.Updates <- update
22 })
23}