Fix bug with update channel closing, add ListenForWebhookRespReqFormat method, for using in serverless apps, add example to README.md, add exaple pf a published bot on AWS Lambda

This commit is contained in:
farit2000 2020-11-27 11:24:41 +03:00
parent 54104a08f9
commit 18031c85e5
2 changed files with 90 additions and 0 deletions

22
bot.go
View file

@ -567,11 +567,33 @@ func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
}
ch <- *update
close(ch)
})
return ch
}
// ListenForWebhookRespReqFormat registers a http handler for a webhook.
func (bot *BotAPI) ListenForWebhookRespReqFormat(w http.ResponseWriter, r *http.Request) UpdatesChannel {
ch := make(chan Update, bot.Buffer)
func(w http.ResponseWriter, r *http.Request) {
update, err := bot.HandleUpdate(r)
if err != nil {
errMsg, _ := json.Marshal(map[string]string{"error": err.Error()})
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(errMsg)
return
}
ch <- *update
close(ch)
}(w, r)
return ch
}
// HandleUpdate parses and returns update received via webhook
func (bot *BotAPI) HandleUpdate(r *http.Request) (*Update, error) {
if r.Method != http.MethodPost {