summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-04-17 00:50:19 +0700
committerShulhan <ms@kilabit.info>2024-04-17 00:50:19 +0700
commita1b5275b85b05b6aba46cf431a79919746157bfc (patch)
treed38efa9916e7e8fe38e7e10bee80d19490e95b19
parentd07c7fa5d474e95a3bcae01bdb385cb5727fb970 (diff)
downloadpakakeh.go-a1b5275b85b05b6aba46cf431a79919746157bfc.tar.xz
telegram/bot: move Webhook options initialization to separate method
-rw-r--r--api/telegram/bot/options.go22
-rw-r--r--api/telegram/bot/webhook.go28
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
+}