add method to write custom webhook http funcs

pull/184/head
Vincent Heins 7 years ago committed by GitHub
parent 4c16a90966
commit 94f3da603f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      bot.go

@ -427,7 +427,7 @@ func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
return updates, nil return updates, nil
} }
// RemoveWebhook unsets the webhook. // Remove unsets the webhook.
func (bot *BotAPI) RemoveWebhook() (APIResponse, error) { func (bot *BotAPI) RemoveWebhook() (APIResponse, error) {
return bot.MakeRequest("setWebhook", url.Values{}) return bot.MakeRequest("setWebhook", url.Values{})
} }
@ -505,17 +505,25 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
return ch, nil return ch, nil
} }
//GetWebhookUpdate gathers webhook information from a http.Request
func GetWebhookUpdate(r *http.Request) (*Update, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
var update Update
err = json.Unmarshal(b, &update)
return &update, err
}
// ListenForWebhook registers a http handler for a webhook. // ListenForWebhook registers a http handler for a webhook.
func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
ch := make(chan Update, bot.Buffer) ch := make(chan Update, bot.Buffer)
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body) update, _ := GetWebhookUpdate(r)
ch <- *update
var update Update
json.Unmarshal(bytes, &update)
ch <- update
}) })
return ch return ch