Merge pull request #543 from zry98/v3

Add secret_token support to webhook
This commit is contained in:
demget 2022-10-04 22:37:03 +03:00 committed by GitHub
commit d782b53c7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,7 @@ type Webhook struct {
AllowedUpdates []string `json:"allowed_updates"`
IP string `json:"ip_address"`
DropUpdates bool `json:"drop_pending_updates"`
SecretToken string `json:"secret_token"`
// (WebhookInfo)
HasCustomCert bool `json:"has_custom_certificate"`
@ -96,6 +97,9 @@ func (h *Webhook) getParams() map[string]string {
if h.DropUpdates {
params["drop_pending_updates"] = strconv.FormatBool(h.DropUpdates)
}
if h.SecretToken != "" {
params["secret_token"] = h.SecretToken
}
if h.TLS != nil {
params["url"] = "https://" + h.Listen
@ -153,6 +157,11 @@ func (h *Webhook) waitForStop(stop chan struct{}) {
// The handler simply reads the update from the body of the requests
// and writes them to the update channel.
func (h *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.SecretToken != "" && r.Header.Get("X-Telegram-Bot-Api-Secret-Token") != h.SecretToken {
h.bot.debug(fmt.Errorf("invalid secret token in request"))
return
}
var update Update
if err := json.NewDecoder(r.Body).Decode(&update); err != nil {
h.bot.debug(fmt.Errorf("cannot decode update: %v", err))