From 7234cb2668b931ad6d5c8e285e7016abfa8dccbc Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Thu, 10 Sep 2026 22:31:23 -0600 Subject: [PATCH] Changing API library to telego. --- cmd/bot/main.go | 71 ++++++++++++++++++++--------------- go.mod | 17 ++++++++- go.sum | 44 ++++++++++++++++++++++ internal/bible/types.go | 55 ++++++++++++++++++++++++--- internal/settings/settings.go | 34 +++++++++++++++++ settings.sample.toml | 5 ++- 6 files changed, 189 insertions(+), 37 deletions(-) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 2903929..767a75e 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -4,18 +4,17 @@ import ( "context" "os" "os/signal" - "sync" - "time" - "github.com/go-telegram/bot" - "github.com/go-telegram/bot/models" "github.com/joho/godotenv" + "github.com/mymmrac/telego" + th "github.com/mymmrac/telego/telegohandler" + tu "github.com/mymmrac/telego/telegoutil" "github.com/robfig/cron/v3" "fmt" _ "git.destrealm.net/zancarius/justusbot/internal/bible" - _ "git.destrealm.net/zancarius/justusbot/internal/settings" + "git.destrealm.net/zancarius/justusbot/internal/settings" ) // Send any text message to the bot after the bot has been started @@ -25,29 +24,52 @@ func main() { panic(fmt.Sprintf("dotenv: %s", err)) } - wg := &sync.WaitGroup{} + conf, err := settings.LoadSettings("settings.toml") + + //wg := &sync.WaitGroup{} ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() - opts := []bot.Option{ - bot.WithDefaultHandler(handler), + bot, err := telego.NewBot(os.Getenv("TOKEN"), + telego.WithDefaultDebugLogger()) + if err != nil { + panic(err) } - b, err := bot.New(os.Getenv("TOKEN"), opts...) - if nil != err { - fmt.Errorf("%v", err) - } - b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, startHandler) - c := cron.New() - c.AddFunc("TZ=America/Denver 7 14 * * *", func(){fmt.Println("testing at 2pm")}) + c.AddFunc(fmt.Sprintf("TZ=%s %s", + conf.System.TimeZone, + conf.DailyScripture.Schedule), func() { fmt.Println("testing at 2pm") }) c.Start() - wg.Add(1) + 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() - b.Start(ctx) + for { + + } }() wg.Add(1) @@ -57,23 +79,12 @@ func main() { fmt.Println(b) }() - wg.Wait() + wg.Wait()*/ /*fmt.Println(bible.FindAlias("mat")) s, err := settings.LoadSettings("settings.toml") fmt.Println(s, err)*/ } -func handler(ctx context.Context, b *bot.Bot, update *models.Update) { - fmt.Println(b.GetMe(ctx)) - /*if update.Message != nil { - b.SendMessage(ctx, &bot.SendMessageParams{ - ChatID: update.Message.Chat.ID, - Text: update.Message.Text, - }) - }*/ -} - -func startHandler(ctx context.Context, b *bot.Bot, update *models.Update) { - fmt.Println("received start:", b, update) +type Bot struct { } diff --git a/go.mod b/go.mod index 8eede84..a834ebe 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,26 @@ module git.destrealm.net/zancarius/justusbot -go 1.26.5 +go 1.26.7 require ( git.destrealm.net/go/errors/v2 v2.0.0-20260910000608-dd2acb7a5f0c // indirect + github.com/andybalholm/brotli v1.2.2 // indirect + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.3 // indirect + github.com/bytedance/sonic/loader v0.5.2 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect github.com/go-telegram/bot v1.25.0 // indirect + github.com/grbit/go-json v0.11.0 // indirect github.com/joho/godotenv v1.5.1 // indirect + github.com/klauspost/compress v1.19.1 // indirect + github.com/klauspost/cpuid/v2 v2.2.9 // indirect + github.com/mymmrac/telego v1.12.1 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.73.0 // indirect + github.com/valyala/fastjson v1.6.10 // indirect + golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect + golang.org/x/sys v0.47.0 // indirect ) diff --git a/go.sum b/go.sum index ee88eaa..beb48fa 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,54 @@ git.destrealm.net/go/errors/v2 v2.0.0-20260910000608-dd2acb7a5f0c h1:naHN9B76A0xalBoYD+vA+dONEPXV9VaM2mHZJUk5bEU= git.destrealm.net/go/errors/v2 v2.0.0-20260910000608-dd2acb7a5f0c/go.mod h1:p+SgrYYiPpLrdGsBbqfH8tOrtISwZUAGtseJXHkLvpY= +github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM= +github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.3 h1:P3akjLPBtV/i6bHC6LbcLjY3KuoOvfiqF8wFHeP5IhY= +github.com/bytedance/sonic v1.15.3/go.mod h1:8e51yTPdY8M6t+vvGL1c2Y1xL9i+frEeIAQAEl75NUc= +github.com/bytedance/sonic/loader v0.5.2 h1:0QtP1gevc1OZ6/H8Lb9BRZiCXd1Ftjd3OKuj1T1lBIo= +github.com/bytedance/sonic/loader v0.5.2/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-telegram/bot v1.25.0 h1:C072msZtr+GjJiQsX/UJLirz+jzSNZVWkckxWufR7Ww= github.com/go-telegram/bot v1.25.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= +github.com/grbit/go-json v0.11.0 h1:bAbyMdYrYl/OjYsSqLH99N2DyQ291mHy726Mx+sYrnc= +github.com/grbit/go-json v0.11.0/go.mod h1:IYpHsdybQ386+6g3VE6AXQ3uTGa5mquBme5/ZWmtzek= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk= +github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY= +github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= +github.com/mymmrac/telego v1.12.1 h1:yx1T5pPSNsU3BjLR7jnfY0D4dtL9caH58Y9e8uzjR88= +github.com/mymmrac/telego v1.12.1/go.mod h1:K4z3Z3Qr6AA8yEjSry3JGScu506NlLl1O4Gqascmop4= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.73.0 h1:ocTOORnBWtJ+P8t/6wAjdkchMzdfHmWx2VD/DPbgZ7s= +github.com/valyala/fasthttp v1.73.0/go.mod h1:EtXQDHaR+5P18p8wqDRFpUhxr108Ga9mXvVJXHRrN2k= +github.com/valyala/fastjson v1.6.10 h1:/yjJg8jaVQdYR3arGxPE2X5z89xrlhS0eGXdv+ADTh4= +github.com/valyala/fastjson v1.6.10/go.mod h1:e6FubmQouUNP73jtMLmcbxS6ydWIpOfhz34TSfO3JaE= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/bible/types.go b/internal/bible/types.go index 4b35f5c..ff5f98d 100644 --- a/internal/bible/types.go +++ b/internal/bible/types.go @@ -1,21 +1,66 @@ package bible -type Bible struct { +import ( + "encoding/json" + "os" +) +type Bible struct { } type Metadata struct { - } type Book struct { - } type Chapter struct { - } type VerseSpec struct { +} -} \ No newline at end of file +type Scripture struct { + Verse string + Translation string + Content string +} + +type ScriptureStore struct { + Verses []*Scripture + Offset int +} + +func (s *ScriptureStore) GetVerse() *Scripture { + if s.Offset < len(s.Verses) { + s.Offset++ + return s.Verses[s.Offset] + } + if len(s.Verses) > 0 { + s.Offset = 1 + return s.Verses[0] + } + return &Scripture{ + Verse: "3 John 11", + Translation: "BSB", + Content: "Beloved, do not imitate what is evil, but what is good. The one who does good is of God; the one who does evil has not seen God.", + } +} + +func OpenStore(name string) (*ScriptureStore, error) { + s := &ScriptureStore{} + buf, err := os.ReadFile(name) + if err != nil { + return nil, err + } + err = json.Unmarshal(buf, *s) + return s, err +} + +func SaveStore(name string, s *ScriptureStore) error { + buf, err := json.Marshal(s) + if err != nil { + return err + } + return os.WriteFile(name, buf, 0o644) +} diff --git a/internal/settings/settings.go b/internal/settings/settings.go index acb4ae4..d14cdf6 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -1,10 +1,13 @@ package settings import ( + "bytes" "io/fs" "os" "github.com/pelletier/go-toml" + + "git.destrealm.net/go/cwrap" ) type Settings struct { @@ -12,6 +15,7 @@ type Settings struct { DailyScripture DailyScripture `toml:"daily_scripture"` System System Translations Translations + Chat Chat } type API struct { @@ -21,8 +25,15 @@ type API struct { User string } +type Chat struct { + Channels []string `toml:"channels"` +} + type DailyScripture struct { UserSubmitted bool `toml:"user_submitted"` + Schedule string + Rotation string + User string } type System struct { @@ -34,6 +45,21 @@ type Translations struct { Active []string `toml:"active"` } +func (c *Settings) Marshal() ([]byte, error) { + buf := &bytes.Buffer{} + + enc := toml.NewEncoder(cwrap.New(buf)) + enc.ArraysWithOneElementPerLine(true) + enc.Indentation(" ") + enc.Order(toml.OrderPreserve) + enc.PromoteAnonymous(false) + + if err := enc.Encode(c); err != nil { + return nil, err + } + return buf.Bytes(), nil +} + func LoadSettings(name string) (*Settings, error) { conf := &Settings{} @@ -52,3 +78,11 @@ func LoadSettings(name string) (*Settings, error) { return conf, nil } + +func SaveSettings(name string, settings *Settings) error { + buf, err := settings.Marshal() + if err != nil { + return err + } + return os.WriteFile(name, buf, 0o644) +} diff --git a/settings.sample.toml b/settings.sample.toml index f03c897..4875f88 100644 --- a/settings.sample.toml +++ b/settings.sample.toml @@ -14,4 +14,7 @@ active = [ ] [system] -timezone = "America/Denver" \ No newline at end of file +timezone = "America/Denver" + +[chat] +channels = [] \ No newline at end of file