From 03886013cb39bc4c086483662f78b089c94f4b87 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 16 Oct 2026 00:14:05 -0600 Subject: [PATCH] Moving bot code into app. --- internal/app/bot.go | 244 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 internal/app/bot.go diff --git a/internal/app/bot.go b/internal/app/bot.go new file mode 100644 index 0000000..a9cb018 --- /dev/null +++ b/internal/app/bot.go @@ -0,0 +1,244 @@ +package app + +import ( + "context" + "fmt" + "net/http" + "net/url" + "os" + "os/signal" + "slices" + + "code.destrealm.net/zancarius/justusbot/internal/settings" + "github.com/mymmrac/telego" + th "github.com/mymmrac/telego/telegohandler" +) + +type bot struct { + settings *settings.Settings + tgbot *telego.Bot + handler *th.BotHandler + ctx context.Context + cancel context.CancelFunc + + chats *chats +} + +func NewBot(ctx context.Context, conf *settings.Settings) *bot { + b := &bot{ + settings: conf, + ctx: ctx, + } + + ctx, cancel := signal.NotifyContext(ctx, os.Interrupt) + b.ctx = ctx + b.cancel = cancel + + b.init() + + return b +} + +func (b *bot) init() { + tgbot, err := telego.NewBot(b.settings.Service.BotToken, telego.WithDefaultDebugLogger()) + if err != nil { + panic(err) + } + b.tgbot = tgbot + + err = b.tgbot.SetWebhook(b.ctx, &telego.SetWebhookParams{ + URL: b.settings.Service.Webhook.URI, + SecretToken: b.tgbot.SecretToken(), + }) + if err != nil { + panic(fmt.Sprintf("failed setting up webhook: %s", err)) + } + info, err := b.tgbot.GetWebhookInfo(b.ctx) + fmt.Println(info, err) + + /*c := cron.New() + c.AddFunc(fmt.Sprintf("TZ=%s %s", + b.settings.System.TimeZone, + b.settings.DailyScripture.Schedule), func() { b.SendScripture() }) + c.Start() + b.cron = c*/ + + switch b.settings.Service.Method { + case "polling": + err = b.SetPolling(b.ctx, b.settings) + case "webhook": + err = b.SetWebhook(b.ctx, b.settings) + } + + if err != nil { + panic(err) + } + + b.AttachHandlers() + b.SetCommands(b.ctx) +} + +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) + } + // Send a message with inline keyboard + /*_, _ = ctx.Bot().SendMessage(ctx, tu.Message( + tu.ID(message.Chat.ID), + fmt.Sprintf("Hello %s!", message.From.FirstName), + ).WithReplyMarkup(tu.InlineKeyboard( + tu.InlineKeyboardRow( + tu.InlineKeyboardButton("Go!").WithCallbackData("go"), + )), + ))*/ + return nil + }, th.CommandEqual("start")) +} + +func (b *bot) SendScripture() { + /*script, err := bible.OpenStore("scripture.json") + if err != nil { + fmt.Println(err) + return + } + v := script.GetVerse() + err = bible.SaveStore("scripture.json", script) + if err != nil { + fmt.Println(err) + return + } + for _, c := range b.Settings.Chat.Channels { + b.Bot.SendRichMessage(b.context, + &telego.SendRichMessageParams{ + ChatID: tu.ID(c), + RichMessage: tu.RichMessageMarkdown(fmt.Sprintf("**Daily Scripture:
%s**\n\n%s (*%s*)", v.Verse, v.Content, v.Translation)), + }) + //b.Bot.SendMessage(b.context, tu.Message(tu.ID(c), fmt.Sprintf("*%s*\n\n%s (%s)", v.Verse, v.Content, v.Translation))) + }*/ +} + +func (b *bot) SetCommands(ctx context.Context) (err error) { + commands, err := b.tgbot.GetMyCommands(ctx, &telego.GetMyCommandsParams{}) + fmt.Println(commands) + if len(commands) > 0 { + if b.settings.Debug.Commands.Reset || b.settings.Debug.Commands.Clear { + b.tgbot.DeleteMyCommands(ctx, &telego.DeleteMyCommandsParams{}) + } + for _, command := range commands { + fmt.Println(command) + } + return + } + if b.settings.Debug.Commands.Clear { + return + } + b.tgbot.SetMyCommands(ctx, &telego.SetMyCommandsParams{ + Commands: []telego.BotCommand{ + { + Command: "help", + Description: "Displays bot usage and help.", + IsEphemeral: true, + }, + { + Command: "settings", + Description: "Bot settings.", + IsEphemeral: true, + }, + { + Command: "start", + Description: "Starts the bot and links it to the current user's channel.", + IsEphemeral: true, + }, + { + Command: "announce", + Description: "Adds an announcement to the bot.", + }, + { + Command: "announcements", + Description: "Lists the current announcements for the week.", + }, + { + Command: "birthday", + Description: "Add, remove, or list birthdays.", + }, + { + Command: "verse", + Description: "Looks up a Bible passage. Optionally accepts a translation identifier as its second argument.", + }, + { + Command: "random", + Description: "Submits a random Bible passage to the current chat.", + }, + { + Command: "add", + Description: "Submits a passage to the bot's daily passage rotation.", + IsEphemeral: true, + }, + }, + }) + return +} + +func (b *bot) SetPolling(ctx context.Context, conf *settings.Settings) (err error) { + err = b.tgbot.DeleteWebhook(ctx, + &telego.DeleteWebhookParams{ + DropPendingUpdates: false, + }) + if err != nil { + return err + } + + updates, err := b.tgbot.UpdatesViaLongPolling(ctx, nil) + if err != nil { + return + } + + b.handler, err = th.NewBotHandler(b.tgbot, updates) + if err != nil { + return + } + + return +} + +func (b *bot) SetWebhook(ctx context.Context, conf *settings.Settings) (err error) { + u, err := url.Parse(conf.Service.Webhook.URI) + if err != nil { + return + } + + mux := http.NewServeMux() + updates, err := b.tgbot.UpdatesViaWebhook(ctx, + telego.WebhookHTTPServeMux( + mux, + u.Path, + b.tgbot.SecretToken())) + if err != nil { + return + } + + b.handler, err = th.NewBotHandler(b.tgbot, updates) + if err != nil { + return + } + + go func() { + _ = http.ListenAndServe( + conf.Service.Webhook.ListenAddr(), + mux) + }() + + return +} + +func (b *bot) Run() error { + defer func() { + b.handler.Stop() + b.cancel() + }() + return b.handler.Start() +}