From a9c58cba45bbb3c56ad51ed75e8e57856902e38c Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 2 Oct 2026 15:31:53 -0600 Subject: [PATCH] Updated to webhooks. --- cmd/bot/main.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 42ac30b..709f1d7 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -2,6 +2,8 @@ package main import ( "context" + "net/http" + "net/url" "os" "os/signal" "slices" @@ -55,6 +57,14 @@ type Bot struct { cancel context.CancelFunc } +type interceptor struct { + mux *http.ServeMux +} + +func (i *interceptor) ServeHTTP(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL) +} + func NewBot() (bot *Bot, errch chan error, err error) { bot = &Bot{} @@ -74,6 +84,21 @@ func NewBot() (bot *Bot, errch chan error, err error) { } bot.Bot = b + u, err := url.Parse(conf.Service.Webhook.URI) + if err != nil { + panic(fmt.Sprintf("malformed URI: %s", conf.Service.Webhook.URI)) + } + + err = b.SetWebhook(ctx, &telego.SetWebhookParams{ + URL: conf.Service.Webhook.URI, + SecretToken: b.SecretToken(), + }) + if err != nil { + panic(fmt.Sprintf("failed setting up webhook: %w", err)) + } + info, err := b.GetWebhookInfo(ctx) + fmt.Println(info, err) + c := cron.New() c.AddFunc(fmt.Sprintf("TZ=%s %s", conf.System.TimeZone, @@ -81,10 +106,23 @@ func NewBot() (bot *Bot, errch chan error, err error) { c.Start() bot.Cron = c - updates, err := b.UpdatesViaLongPolling(ctx, nil) + /*updates, err := b.UpdatesViaLongPolling(ctx, nil) if err != nil { fmt.Errorf("failed receiving updates: %s", err) + }*/ + + mux := http.NewServeMux() + updates, err := b.UpdatesViaWebhook(ctx, + telego.WebhookHTTPServeMux(mux, u.Path, b.SecretToken())) + /*bot.Handler, err = th.NewBotHandler(b, updates) + if err != nil { + fmt.Errorf("failed configuring handlers: %s", err) } + bot.AttachHandlers()*/ + go func() { + _ = http.ListenAndServe(":3117", &interceptor{mux}) + }() + bot.Handler, err = th.NewBotHandler(b, updates) if err != nil { fmt.Errorf("failed configuring handlers: %s", err) @@ -97,6 +135,7 @@ func NewBot() (bot *Bot, errch chan error, err error) { func (b *Bot) AttachHandlers() { fmt.Println("add handlers") b.Handler.HandleMessage(func(ctx *th.Context, message telego.Message) error { + fmt.Println("handling message") if !slices.Contains(b.Settings.Chat.Channels, message.Chat.ID) { fmt.Println("adding chat:", message.Chat.ID) b.Settings.Chat.Channels = append(b.Settings.Chat.Channels, message.Chat.ID)