1
0
telegram/set_webhook.go

111 lines
3.3 KiB
Go
Raw Normal View History

2017-10-11 11:58:11 +00:00
package telegram
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"os"
"strconv"
"strings"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
2017-10-11 11:58:11 +00:00
type SetWebhookParameters struct {
// HTTPS url to send updates to. Use an empty string to remove webhook
// integration
URL string `json:"url"`
// Upload your public key certificate so that the root certificate in use can
// be checked. See our self-signed guide for details.
Certificate *InputFile `json:"certificate,omitempty"`
// Maximum allowed number of simultaneous HTTPS connections to the webhook
// for update delivery, 1-100. Defaults to 40. Use lower values to limit the
// load on your bots server, and higher values to increase your bots
// throughput.
MaxConnections int `json:"max_connections,omitempty"`
// List the types of updates you want your bot to receive. For example,
// specify [“message”, “edited_channel_post”, “callback_query”] to only
// receive updates of these types. See Update for a complete list of
// available update types. Specify an empty list to receive all updates
// regardless of type (default). If not specified, the previous setting will
// be used.
//
// Please note that this parameter doesn't affect updates created before the
// call to the setWebhook, so unwanted updates may be received for a short
// period of time.
AllowedUpdates []string `json:"allowed_updates,omitempty"`
}
2017-10-17 11:08:09 +00:00
func NewWebhook(url string, file interface{}) *SetWebhookParameters {
params := &SetWebhookParameters{URL: url}
if file != nil {
var input InputFile = file
params.Certificate = &input
}
return params
}
2017-10-11 11:58:11 +00:00
// SetWebhook specify a url and receive incoming updates via an outgoing webhook.
// Whenever there is an update for the bot, we will send an HTTPS POST request to
// the specified url, containing a JSON-serialized Update. In case of an
// unsuccessful request, we will give up after a reasonable amount of attempts.
// Returns true.
//
// If you'd like to make sure that the Webhook request comes from Telegram, we
// recommend using a secret path in the URL, e.g. https://www.example.com/<token>.
// Since nobody else knows your bots token, you can be pretty sure its us.
func (bot *Bot) SetWebhook(params *SetWebhookParameters) (bool, error) {
var args http.Args
args.Add("url", params.URL)
if len(params.AllowedUpdates) > 0 {
args.Add("allowed_updates", fmt.Sprint(`["`, strings.Join(params.AllowedUpdates, `","`), `"]`))
}
if params.MaxConnections > 0 {
args.Add("max_connections", strconv.Itoa(params.MaxConnections))
}
var buffer bytes.Buffer
multi := multipart.NewWriter(&buffer)
if params.Certificate != nil {
cert := *params.Certificate
switch file := cert.(type) {
case string:
f, err := os.Open(file)
if err != nil {
return false, err
}
defer f.Close()
formFile, err := multi.CreateFormFile("certificate", f.Name())
if err != nil {
return false, err
}
if _, err = io.Copy(formFile, f); err != nil {
return false, err
}
multi.Close()
default:
return false, errors.New("use string only (for current version of go-telegram)")
}
2017-10-11 11:58:11 +00:00
}
resp, err := bot.upload(buffer.Bytes(), multi.Boundary(), "setWebhook", &args)
2017-10-11 11:58:11 +00:00
if err != nil {
return false, err
}
var data bool
err = json.Unmarshal(*resp.Result, &data)
return data, err
}