2018-06-30 18:53:07 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-04-26 17:43:17 +00:00
|
|
|
"strconv"
|
2018-06-30 18:53:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A WebhookTLS specifies the path to a key and a cert so the poller can open
|
|
|
|
// a TLS listener
|
|
|
|
type WebhookTLS struct {
|
|
|
|
Key string
|
|
|
|
Cert string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A WebhookEndpoint describes the endpoint to which telegram will send its requests.
|
|
|
|
// This must be a public URL and can be a loadbalancer or something similar. If the
|
2020-05-31 13:18:33 +00:00
|
|
|
// endpoint uses TLS and the certificate is self-signed you have to add the certificate
|
2018-06-30 18:53:07 +00:00
|
|
|
// path of this certificate so telegram will trust it. This field can be ignored if you
|
2020-05-31 13:18:33 +00:00
|
|
|
// have a trusted certificate (letsencrypt, ...).
|
2018-06-30 18:53:07 +00:00
|
|
|
type WebhookEndpoint struct {
|
|
|
|
PublicURL string
|
|
|
|
Cert string
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Webhook configures the poller for webhooks. It opens a port on the given
|
2019-12-25 22:55:51 +00:00
|
|
|
// listen address. If TLS is filled, the listener will use the key and cert to open
|
2018-06-30 18:53:07 +00:00
|
|
|
// a secure port. Otherwise it will use plain HTTP.
|
2020-05-31 13:18:33 +00:00
|
|
|
//
|
2018-06-30 18:53:07 +00:00
|
|
|
// If you have a loadbalancer ore other infrastructure in front of your service, you
|
|
|
|
// must fill the Endpoint structure so this poller will send this data to telegram. If
|
2019-12-25 22:55:51 +00:00
|
|
|
// you leave these values empty, your local address will be sent to telegram which is mostly
|
2018-06-30 18:53:07 +00:00
|
|
|
// not what you want (at least while developing). If you have a single instance of your
|
|
|
|
// bot you should consider to use the LongPoller instead of a WebHook.
|
2020-05-31 13:18:33 +00:00
|
|
|
//
|
2018-07-01 17:05:38 +00:00
|
|
|
// You can also leave the Listen field empty. In this case it is up to the caller to
|
|
|
|
// add the Webhook to a http-mux.
|
2020-05-31 13:18:33 +00:00
|
|
|
//
|
2018-06-30 18:53:07 +00:00
|
|
|
type Webhook struct {
|
2020-05-05 17:41:30 +00:00
|
|
|
Listen string `json:"url"`
|
2020-05-21 09:02:35 +00:00
|
|
|
MaxConnections int `json:"max_connections"`
|
2020-05-05 17:41:30 +00:00
|
|
|
AllowedUpdates []string `json:"allowed_updates"`
|
|
|
|
|
|
|
|
// (WebhookInfo)
|
|
|
|
HasCustomCert bool `json:"has_custom_certificate"`
|
|
|
|
PendingUpdates int `json:"pending_update_count"`
|
|
|
|
ErrorUnixtime int64 `json:"last_error_date"`
|
|
|
|
ErrorMessage string `json:"last_error_message"`
|
2020-04-26 17:43:17 +00:00
|
|
|
|
2018-06-30 18:53:07 +00:00
|
|
|
TLS *WebhookTLS
|
|
|
|
Endpoint *WebhookEndpoint
|
2020-04-26 17:43:17 +00:00
|
|
|
|
|
|
|
dest chan<- Update
|
|
|
|
bot *Bot
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 11:39:07 +00:00
|
|
|
func (h *Webhook) getFiles() map[string]File {
|
|
|
|
m := make(map[string]File)
|
|
|
|
|
2018-06-30 18:53:07 +00:00
|
|
|
if h.TLS != nil {
|
2018-10-11 11:39:07 +00:00
|
|
|
m["certificate"] = FromDisk(h.TLS.Cert)
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
// check if it is overwritten by an endpoint
|
|
|
|
if h.Endpoint != nil {
|
|
|
|
if h.Endpoint.Cert == "" {
|
|
|
|
// this can be the case if there is a loadbalancer or reverseproxy in
|
|
|
|
// front with a public cert. in this case we do not need to upload it
|
|
|
|
// to telegram. we delete the certificate from the map, because someone
|
|
|
|
// can have an internal TLS listener with a private cert
|
|
|
|
delete(m, "certificate")
|
|
|
|
} else {
|
|
|
|
// someone configured a certificate
|
2018-10-11 11:39:07 +00:00
|
|
|
m["certificate"] = FromDisk(h.Endpoint.Cert)
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Webhook) getParams() map[string]string {
|
2020-04-26 17:43:17 +00:00
|
|
|
params := make(map[string]string)
|
|
|
|
|
|
|
|
if h.MaxConnections != 0 {
|
|
|
|
params["max_connections"] = strconv.Itoa(h.MaxConnections)
|
|
|
|
}
|
|
|
|
if len(h.AllowedUpdates) > 0 {
|
|
|
|
data, _ := json.Marshal(h.AllowedUpdates)
|
|
|
|
params["allowed_updates"] = string(data)
|
|
|
|
}
|
|
|
|
|
2018-06-30 18:53:07 +00:00
|
|
|
if h.TLS != nil {
|
2020-04-26 17:43:17 +00:00
|
|
|
params["url"] = "https://" + h.Listen
|
2018-06-30 18:53:07 +00:00
|
|
|
} else {
|
|
|
|
// this will not work with telegram, they want TLS
|
|
|
|
// but i allow this because telegram will send an error
|
|
|
|
// when you register this hook. in their docs they write
|
|
|
|
// that port 80/http is allowed ...
|
2020-04-26 17:43:17 +00:00
|
|
|
params["url"] = "http://" + h.Listen
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
if h.Endpoint != nil {
|
2020-04-26 17:43:17 +00:00
|
|
|
params["url"] = h.Endpoint.PublicURL
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
2020-04-26 17:43:17 +00:00
|
|
|
return params
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Webhook) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
2020-04-18 18:54:15 +00:00
|
|
|
if err := b.SetWebhook(h); err != nil {
|
2020-04-06 13:04:57 +00:00
|
|
|
b.debug(err)
|
2018-06-30 18:53:07 +00:00
|
|
|
close(stop)
|
|
|
|
return
|
|
|
|
}
|
2020-04-06 13:04:57 +00:00
|
|
|
|
2018-07-01 17:05:38 +00:00
|
|
|
// store the variables so the HTTP-handler can use 'em
|
|
|
|
h.dest = dest
|
|
|
|
h.bot = b
|
|
|
|
|
|
|
|
if h.Listen == "" {
|
|
|
|
h.waitForStop(stop)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-30 18:53:07 +00:00
|
|
|
s := &http.Server{
|
|
|
|
Addr: h.Listen,
|
|
|
|
Handler: h,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(stop chan struct{}) {
|
2018-07-01 17:05:38 +00:00
|
|
|
h.waitForStop(stop)
|
2018-06-30 18:53:07 +00:00
|
|
|
s.Shutdown(context.Background())
|
|
|
|
}(stop)
|
|
|
|
|
|
|
|
if h.TLS != nil {
|
|
|
|
s.ListenAndServeTLS(h.TLS.Cert, h.TLS.Key)
|
|
|
|
} else {
|
|
|
|
s.ListenAndServe()
|
|
|
|
}
|
2018-07-01 17:05:38 +00:00
|
|
|
}
|
2018-06-30 18:53:07 +00:00
|
|
|
|
2018-07-01 17:05:38 +00:00
|
|
|
func (h *Webhook) waitForStop(stop chan struct{}) {
|
|
|
|
<-stop
|
|
|
|
close(stop)
|
2018-06-30 18:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
var update Update
|
2020-06-19 20:34:37 +00:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(&update); err != nil {
|
2018-06-30 18:53:07 +00:00
|
|
|
h.bot.debug(fmt.Errorf("cannot decode update: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.dest <- update
|
|
|
|
}
|
2020-04-06 13:04:57 +00:00
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
// Webhook returns the current webhook status.
|
|
|
|
func (b *Bot) Webhook() (*Webhook, error) {
|
2020-05-05 17:41:30 +00:00
|
|
|
data, err := b.Raw("getWebhookInfo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
2020-05-05 20:10:39 +00:00
|
|
|
Result Webhook
|
2020-05-05 17:41:30 +00:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
2020-06-19 20:34:37 +00:00
|
|
|
return nil, wrapError(err)
|
2020-05-05 17:41:30 +00:00
|
|
|
}
|
2020-05-05 20:10:39 +00:00
|
|
|
return &resp.Result, nil
|
2020-05-05 17:41:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:12:31 +00:00
|
|
|
// SetWebhook configures a bot to receive incoming
|
|
|
|
// updates via an outgoing webhook.
|
2020-04-18 18:54:15 +00:00
|
|
|
func (b *Bot) SetWebhook(w *Webhook) error {
|
|
|
|
_, err := b.sendFiles("setWebhook", w.getFiles(), w.getParams())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:12:31 +00:00
|
|
|
// RemoveWebhook removes webhook integration.
|
2020-04-06 13:04:57 +00:00
|
|
|
func (b *Bot) RemoveWebhook() error {
|
|
|
|
_, err := b.Raw("deleteWebhook", nil)
|
|
|
|
return err
|
|
|
|
}
|