2026-10-16 00:14:05 -06:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"slices"
|
2026-10-20 22:36:00 -06:00
|
|
|
"time"
|
2026-10-16 00:14:05 -06:00
|
|
|
|
|
|
|
|
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
|
th "github.com/mymmrac/telego/telegohandler"
|
2026-10-20 20:17:29 -06:00
|
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
2026-10-21 19:45:01 -06:00
|
|
|
"go.destrealm.net/logging/v3"
|
2026-10-16 00:14:05 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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() {
|
2026-10-21 19:45:01 -06:00
|
|
|
logger := logging.Logger().Get("bot")
|
|
|
|
|
logger.SetLevel(logging.Debug)
|
|
|
|
|
|
|
|
|
|
tgbot, err := telego.NewBot(b.settings.Service.BotToken,
|
|
|
|
|
telego.WithLogger(logger))
|
|
|
|
|
//telego.WithDefaultDebugLogger())
|
2026-10-16 00:14:05 -06:00
|
|
|
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"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 20:17:29 -06:00
|
|
|
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("**<u>Daily Scripture:</u><br>%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)))
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-16 00:14:05 -06:00
|
|
|
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("**<u>Daily Scripture:</u><br>%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 {
|
2026-10-21 19:45:01 -06:00
|
|
|
if b.settings.Debug.Commands.Reset {
|
2026-10-19 23:15:12 -06:00
|
|
|
b.tgbot.DeleteMyCommands(ctx, &telego.DeleteMyCommandsParams{
|
|
|
|
|
Scope: &telego.BotCommandScopeAllChatAdministrators{
|
|
|
|
|
Type: "all_chat_administrators",
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-10-16 00:14:05 -06:00
|
|
|
b.tgbot.DeleteMyCommands(ctx, &telego.DeleteMyCommandsParams{})
|
|
|
|
|
}
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
fmt.Println(command)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-21 19:45:01 -06:00
|
|
|
if !b.settings.Debug.Commands.Register {
|
2026-10-16 00:14:05 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b.tgbot.SetMyCommands(ctx, &telego.SetMyCommandsParams{
|
2026-10-19 23:15:12 -06:00
|
|
|
Scope: &telego.BotCommandScopeAllChatAdministrators{
|
|
|
|
|
Type: "all_chat_administrators",
|
|
|
|
|
},
|
2026-10-16 00:14:05 -06:00
|
|
|
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,
|
|
|
|
|
},
|
2026-10-19 23:15:12 -06:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
b.tgbot.SetMyCommands(ctx, &telego.SetMyCommandsParams{
|
|
|
|
|
Commands: []telego.BotCommand{
|
|
|
|
|
{
|
|
|
|
|
Command: "help",
|
|
|
|
|
Description: "Displays bot usage and help.",
|
|
|
|
|
IsEphemeral: true,
|
|
|
|
|
},
|
2026-10-16 00:14:05 -06:00
|
|
|
{
|
|
|
|
|
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) {
|
2026-10-20 20:17:29 -06:00
|
|
|
info, err := b.tgbot.GetWebhookInfo(b.ctx)
|
2026-10-16 00:14:05 -06:00
|
|
|
if err != nil {
|
2026-10-20 20:17:29 -06:00
|
|
|
fmt.Println("webhook info error:", err)
|
2026-10-16 00:14:05 -06:00
|
|
|
return err
|
|
|
|
|
}
|
2026-10-20 20:17:29 -06:00
|
|
|
fmt.Println("webhook info:", info)
|
|
|
|
|
|
|
|
|
|
if info.URL == "" {
|
|
|
|
|
err = b.tgbot.DeleteWebhook(ctx,
|
|
|
|
|
&telego.DeleteWebhookParams{
|
|
|
|
|
DropPendingUpdates: false,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-16 00:14:05 -06:00
|
|
|
|
2026-10-20 22:36:00 -06:00
|
|
|
updates, err := b.tgbot.UpdatesViaLongPolling(ctx, &telego.GetUpdatesParams{
|
|
|
|
|
Timeout: 300,
|
|
|
|
|
}, telego.WithLongPollingUpdateInterval(time.Second*30))
|
2026-10-16 00:14:05 -06:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 20:17:29 -06:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-16 00:14:05 -06:00
|
|
|
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 {
|
2026-10-20 20:17:29 -06:00
|
|
|
fmt.Println("bot.Run()")
|
2026-10-16 00:14:05 -06:00
|
|
|
defer func() {
|
|
|
|
|
b.handler.Stop()
|
|
|
|
|
b.cancel()
|
|
|
|
|
}()
|
|
|
|
|
return b.handler.Start()
|
|
|
|
|
}
|