From a1b5275b85b05b6aba46cf431a79919746157bfc Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 17 Apr 2024 00:50:19 +0700 Subject: telegram/bot: move Webhook options initialization to separate method --- api/telegram/bot/options.go | 22 +++++----------------- api/telegram/bot/webhook.go | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/api/telegram/bot/options.go b/api/telegram/bot/options.go index 0fc15960..995f5228 100644 --- a/api/telegram/bot/options.go +++ b/api/telegram/bot/options.go @@ -23,11 +23,6 @@ const ( EnvWebhookURL = "TELEGRAM_WEBHOOK_URL" ) -const ( - defListenAddress = ":80" - defListenAddressTLS = ":443" -) - // UpdateHandler define the handler when Bot receiving updates. type UpdateHandler func(update Update) @@ -73,18 +68,11 @@ func (opts *Options) init() (err error) { if opts.Webhook == nil { return errors.New("empty Webhook URL") } - if len(opts.Webhook.URL) == 0 { - // Even thought empty URL is allowed by API, which - // means to clear the previous setWebhook, use the - // DeleteWebhook instead for consistency. - return errors.New("empty Webhook URL") - } - if len(opts.Webhook.ListenAddress) == 0 { - if opts.Webhook.ListenCertificate == nil { - opts.Webhook.ListenAddress = defListenAddress - } else { - opts.Webhook.ListenAddress = defListenAddressTLS - } + + err = opts.Webhook.init() + if err != nil { + return err } + return nil } diff --git a/api/telegram/bot/webhook.go b/api/telegram/bot/webhook.go index 66eda754..5a7387c2 100644 --- a/api/telegram/bot/webhook.go +++ b/api/telegram/bot/webhook.go @@ -4,7 +4,15 @@ package bot -import "crypto/tls" +import ( + "crypto/tls" + "errors" +) + +const ( + defListenAddress = `:80` + defListenAddressTLS = `:443` +) // Webhook contains options to set Webhook to receive updates. type Webhook struct { @@ -43,3 +51,21 @@ type Webhook struct { // values to increase your bot’s throughput. MaxConnections int } + +func (webhook *Webhook) init() (err error) { + if len(webhook.URL) == 0 { + // Even thought empty URL is allowed by API, which + // means to clear the previous setWebhook, use the + // DeleteWebhook instead for consistency. + return errors.New(`empty Webhook URL`) + } + + if len(webhook.ListenAddress) == 0 { + if webhook.ListenCertificate == nil { + webhook.ListenAddress = defListenAddress + } else { + webhook.ListenAddress = defListenAddressTLS + } + } + return nil +} -- cgit v1.3