Skip to content

Commit

Permalink
Add secret_token support to webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
zry98 committed Aug 18, 2022
1 parent c6bead1 commit 91fcbd4
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 91fcbd4

Please sign in to comment.