146 lines
2.9 KiB
Go
146 lines
2.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/robfig/cron/v3"
|
|
"github.com/urfave/cli/v3"
|
|
"go.destrealm.net/logging/v3"
|
|
"go.destrealm.net/vfs"
|
|
|
|
"code.destrealm.net/zancarius/justusbot/internal/bible"
|
|
"code.destrealm.net/zancarius/justusbot/internal/models"
|
|
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
|
)
|
|
|
|
type App struct {
|
|
bot *bot
|
|
ctx context.Context
|
|
db *models.Database
|
|
fs vfs.VFS
|
|
logger *logging.Log
|
|
settings *settings.Settings
|
|
txns *bible.Translations
|
|
|
|
cron *cron.Cron
|
|
}
|
|
|
|
func NewApp(ctx context.Context, root vfs.VFS, conf *settings.Settings) (*App, error) {
|
|
app := &App{
|
|
ctx: ctx,
|
|
db: models.OpenDatabase(ctx, conf),
|
|
fs: root,
|
|
logger: logging.Logger(),
|
|
settings: conf,
|
|
}
|
|
|
|
if err := app.db.Open(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if txns, err := bible.LoadTranslations(root); err != nil {
|
|
return nil, err
|
|
} else {
|
|
app.txns = txns
|
|
}
|
|
|
|
return app, nil
|
|
}
|
|
|
|
func (a *App) loadCron() error {
|
|
a.cron = cron.New()
|
|
|
|
// Scripture schedule.
|
|
a.cron.AddFunc(fmt.Sprintf("TZ=%s %s",
|
|
a.settings.System.TimeZone,
|
|
a.settings.DailyScripture.Schedule), a.scripture)
|
|
|
|
fmt.Println("using schedule:", fmt.Sprintf("TZ=%s %s", a.settings.System.TimeZone, a.settings.DailyScripture.Schedule))
|
|
a.cron.Start()
|
|
fmt.Println("cron started")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) loadEnv() error {
|
|
// Load .env from VFS.
|
|
fp, err := a.fs.Open(".env")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf, err := io.ReadAll(fp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := godotenv.UnmarshalBytes(buf)
|
|
for k, v := range env {
|
|
switch k {
|
|
case "TOKEN":
|
|
a.settings.Service.BotToken = v
|
|
}
|
|
}
|
|
|
|
//if chats, err := a.db.
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) scripture() {
|
|
fmt.Println("scripture send triggered")
|
|
fmt.Println("sending to chats:")
|
|
chats, err := a.db.Chats().ListChats()
|
|
if err == nil {
|
|
for _, chat := range chats {
|
|
fmt.Println(chat)
|
|
}
|
|
} else {
|
|
fmt.Println(" !!! error !!!")
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
vv, err := a.db.Verses().RandomVerseRotate()
|
|
if err != nil {
|
|
fmt.Println("error retrieving random verse:", err)
|
|
return
|
|
}
|
|
ref := bible.NewBibleRef()
|
|
ref.Book = vv.Book
|
|
ref.Chapter = vv.Chapter
|
|
ref.Verses.Start = vv.VerseStart
|
|
ref.Verses.Stop = vv.VerseStop
|
|
vv.Translation = "bsb"
|
|
|
|
verses, err := a.txns.LoadRef(vv.Translation, ref)
|
|
verses.SkipLineBreaks = true
|
|
verses.IncludeVerseNum = false
|
|
fmt.Println(verses.String())
|
|
fmt.Println("to bot:", a.bot)
|
|
|
|
for _, chat := range chats {
|
|
a.bot.SendMarkdownMessage(chat.ID,
|
|
fmt.Sprintf("**<u>Daily Scripture:</u><br>%s**\n\n%s (*%s*)", ref.String(), verses.String(), strings.ToUpper(vv.Translation)))
|
|
}
|
|
}
|
|
|
|
func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
|
|
if err := a.loadEnv(); err != nil {
|
|
return err
|
|
}
|
|
if err := a.loadCron(); err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Println("context:", ctx, a.ctx)
|
|
a.bot = NewBot(ctx, a.settings)
|
|
a.bot.Run()
|
|
|
|
return nil
|
|
}
|
|
|
|
type chats struct {
|
|
}
|