From b2d2f4f5b0280ee1fd44e45a4c56619f1d5a5ac9 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Mon, 7 Sep 2015 13:09:08 -0500 Subject: [PATCH] add a http listener for webhooks that uses the normal updates chan --- webhook.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 webhook.go diff --git a/webhook.go b/webhook.go new file mode 100644 index 0000000..8519459 --- /dev/null +++ b/webhook.go @@ -0,0 +1,23 @@ +package tgbotapi + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +// ListenForWebhook registers a http handler for a webhook. +// Useful for Google App Engine or other places where you cannot +// use a normal update chan. +func (bot *BotAPI) ListenForWebhook() { + bot.Updates = make(chan Update, 100) + + http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) { + bytes, _ := ioutil.ReadAll(r.Body) + + var update Update + json.Unmarshal(bytes, &update) + + bot.Updates <- update + }) +}