1
bot/webhook_handler.go
Renato Saksanni 8082f3c9d5
Add some unit tests (#92)
* Add some unit tests

* Add some fixes and remove deps

* Add some webhook_handler logic fixes and test

---------

Co-authored-by: renato.saksanni <renatosaksanni@ALG-RenatoSaksanni-C02GX30QQ05P.local>
2024-07-18 17:40:36 +03:00

50 lines
1018 B
Go

package bot
import (
"encoding/json"
"io"
"net/http"
"github.com/go-telegram/bot/models"
)
func (b *Bot) WebhookHandler() http.HandlerFunc {
return func(_ http.ResponseWriter, req *http.Request) {
if b.webhookSecretToken != "" && req.Header.Get("X-Telegram-Bot-Api-Secret-Token") != b.webhookSecretToken {
b.error("invalid webhook secret token received from update")
return
}
body, errReadBody := io.ReadAll(req.Body)
if errReadBody != nil {
b.error("error read request body, %w", errReadBody)
return
}
update := &models.Update{}
errDecode := json.Unmarshal(body, update)
if errDecode != nil {
b.error("error decode request body, %s, %w", body, errDecode)
return
}
if b.isDebug {
b.debugHandler("webhook request '%s'", body)
}
select {
case <-req.Context().Done():
b.error("some updates lost, ctx done")
return
default:
}
select {
case b.updates <- update:
case <-req.Context().Done():
b.error("failed to send update, ctx done")
}
}
}