diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 767a75e..9d378ef 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -4,6 +4,8 @@ import ( "context" "os" "os/signal" + "slices" + "sync" "github.com/joho/godotenv" "github.com/mymmrac/telego" @@ -13,7 +15,7 @@ import ( "fmt" - _ "git.destrealm.net/zancarius/justusbot/internal/bible" + "git.destrealm.net/zancarius/justusbot/internal/bible" "git.destrealm.net/zancarius/justusbot/internal/settings" ) @@ -24,62 +26,20 @@ func main() { panic(fmt.Sprintf("dotenv: %s", err)) } - conf, err := settings.LoadSettings("settings.toml") - - //wg := &sync.WaitGroup{} - - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) - defer cancel() - - bot, err := telego.NewBot(os.Getenv("TOKEN"), - telego.WithDefaultDebugLogger()) + b, c, err := NewBot() if err != nil { panic(err) } + _ = c - c := cron.New() - c.AddFunc(fmt.Sprintf("TZ=%s %s", - conf.System.TimeZone, - conf.DailyScripture.Schedule), func() { fmt.Println("testing at 2pm") }) - c.Start() - - updates, _ := bot.UpdatesViaLongPolling(ctx, nil) - bh, _ := th.NewBotHandler(bot, updates) - defer func() { - _ = bh.Stop() - }() - - bh.HandleMessage(func(ctx *th.Context, message telego.Message) error { - // 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")) - - bh.Start() - - /*wg.Add(1) - go func(){ - defer wg.Done() - for { - - } - }() - + wg := &sync.WaitGroup{} wg.Add(1) - go func(){ + go func() { defer wg.Done() - time.Sleep(1*time.Second) - fmt.Println(b) + err := b.Start() + fmt.Println(err) }() - - wg.Wait()*/ + wg.Wait() /*fmt.Println(bible.FindAlias("mat")) s, err := settings.LoadSettings("settings.toml") @@ -87,4 +47,104 @@ func main() { } type Bot struct { + Settings *settings.Settings + Cron *cron.Cron + Bot *telego.Bot + Handler *th.BotHandler + context context.Context + cancel context.CancelFunc +} + +func NewBot() (bot *Bot, errch chan error, err error) { + bot = &Bot{} + + conf, err := settings.LoadSettings("settings.toml") + if err != nil { + return + } + bot.Settings = conf + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + bot.context = ctx + bot.cancel = cancel + + b, err := telego.NewBot(os.Getenv("TOKEN"), telego.WithDefaultDebugLogger()) + if err != nil { + panic(err) + } + bot.Bot = b + + c := cron.New() + c.AddFunc(fmt.Sprintf("TZ=%s %s", + conf.System.TimeZone, + conf.DailyScripture.Schedule), func() { bot.SendScripture() }) + c.Start() + bot.Cron = c + + updates, err := b.UpdatesViaLongPolling(ctx, nil) + if err != nil { + fmt.Errorf("failed receiving updates: %s", err) + } + bot.Handler, err = th.NewBotHandler(b, updates) + if err != nil { + fmt.Errorf("failed configuring handlers: %s", err) + } + bot.AttachHandlers() + + return +} + +func (b *Bot) AttachHandlers() { + fmt.Println("add handlers") + b.Handler.HandleMessage(func(ctx *th.Context, message telego.Message) error { + 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) + b.SaveConfig() + } + // 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("**%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) SaveConfig() { + settings.SaveSettings("settings.toml", b.Settings) +} + +func (b *Bot) Start() error { + defer func() { + b.Handler.Stop() + b.cancel() + }() + return b.Handler.Start() }