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" tu "github.com/mymmrac/telego/telegoutil" ) 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 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) SendMarkdownMessage(chatid int64, message string) error { fmt.Println("bot:", b.tgbot) fmt.Println("bot context:", b.ctx) _, err := b.tgbot.SendRichMessage(b.ctx, &telego.SendRichMessageParams{ ChatID: tu.ID(chatid), RichMessage: tu.RichMessageMarkdown(message), }) return err /*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) 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{ Scope: &telego.BotCommandScopeAllChatAdministrators{ Type: "all_chat_administrators", }, }) 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{ Scope: &telego.BotCommandScopeAllChatAdministrators{ Type: "all_chat_administrators", }, Commands: []telego.BotCommand{ { Command: "settings", Description: "Bot settings.", IsEphemeral: true, }, { Command: "start", Description: "Starts the bot and links it to the current user's channel.", IsEphemeral: true, }, }, }) b.tgbot.SetMyCommands(ctx, &telego.SetMyCommandsParams{ Commands: []telego.BotCommand{ { Command: "help", Description: "Displays bot usage and help.", 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) { info, err := b.tgbot.GetWebhookInfo(b.ctx) if err != nil { fmt.Println("webhook info error:", err) return err } fmt.Println("webhook info:", info) if info.URL == "" { 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 } err = b.tgbot.SetWebhook(b.ctx, &telego.SetWebhookParams{ URL: b.settings.Service.Webhook.URI, SecretToken: b.tgbot.SecretToken(), }) if err != nil { return } info, err := b.tgbot.GetWebhookInfo(b.ctx) if err != nil { fmt.Println(info, err) 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 { fmt.Println("bot.Run()") defer func() { b.handler.Stop() b.cancel() }() return b.handler.Start() }