add a http listener for webhooks that uses the normal updates chan

bot-api-6.1
Syfaro 2015-09-07 13:09:08 -05:00
parent 3b466def71
commit b2d2f4f5b0
1 changed files with 23 additions and 0 deletions

23
webhook.go 100644
View File

@ -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
})
}