2026-09-09 16:19:41 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-10-02 18:00:08 -06:00
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2026-09-10 15:44:40 -06:00
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
2026-09-11 15:19:49 -06:00
|
|
|
"slices"
|
|
|
|
|
"sync"
|
2026-09-09 16:19:41 -06:00
|
|
|
|
2026-09-10 15:44:40 -06:00
|
|
|
"github.com/joho/godotenv"
|
2026-09-10 22:31:23 -06:00
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
|
th "github.com/mymmrac/telego/telegohandler"
|
2026-09-10 15:44:40 -06:00
|
|
|
"github.com/robfig/cron/v3"
|
2026-09-09 16:19:41 -06:00
|
|
|
|
|
|
|
|
"fmt"
|
2026-09-09 22:17:51 -06:00
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
"git.destrealm.net/zancarius/justusbot/internal/settings"
|
2026-09-09 16:19:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Send any text message to the bot after the bot has been started
|
|
|
|
|
|
|
|
|
|
func main() {
|
2026-09-10 15:44:40 -06:00
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
|
panic(fmt.Sprintf("dotenv: %s", err))
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 15:19:49 -06:00
|
|
|
b, c, err := NewBot()
|
2026-09-10 22:31:23 -06:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
2026-09-11 15:19:49 -06:00
|
|
|
_ = c
|
2026-09-09 16:19:41 -06:00
|
|
|
|
2026-09-11 15:19:49 -06:00
|
|
|
wg := &sync.WaitGroup{}
|
2026-09-10 15:44:40 -06:00
|
|
|
wg.Add(1)
|
2026-09-11 15:19:49 -06:00
|
|
|
go func() {
|
2026-09-10 15:44:40 -06:00
|
|
|
defer wg.Done()
|
2026-09-11 15:19:49 -06:00
|
|
|
err := b.Start()
|
|
|
|
|
fmt.Println(err)
|
2026-09-10 15:44:40 -06:00
|
|
|
}()
|
2026-09-11 15:19:49 -06:00
|
|
|
wg.Wait()
|
2026-09-10 15:44:40 -06:00
|
|
|
|
|
|
|
|
/*fmt.Println(bible.FindAlias("mat"))
|
2026-09-09 22:17:51 -06:00
|
|
|
s, err := settings.LoadSettings("settings.toml")
|
2026-09-10 15:44:40 -06:00
|
|
|
fmt.Println(s, err)*/
|
2026-09-09 16:19:41 -06:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 22:31:23 -06:00
|
|
|
type Bot struct {
|
2026-09-11 15:19:49 -06:00
|
|
|
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
|
|
|
|
|
|
2026-10-02 18:00:08 -06:00
|
|
|
err = b.SetWebhook(ctx, &telego.SetWebhookParams{
|
|
|
|
|
URL: conf.Service.Webhook.URI,
|
|
|
|
|
SecretToken: b.SecretToken(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2026-10-03 15:20:24 -06:00
|
|
|
panic(fmt.Sprintf("failed setting up webhook: %s", err))
|
2026-10-02 18:00:08 -06:00
|
|
|
}
|
|
|
|
|
info, err := b.GetWebhookInfo(ctx)
|
|
|
|
|
fmt.Println(info, err)
|
|
|
|
|
|
2026-09-11 15:19:49 -06:00
|
|
|
c := cron.New()
|
|
|
|
|
c.AddFunc(fmt.Sprintf("TZ=%s %s",
|
|
|
|
|
conf.System.TimeZone,
|
|
|
|
|
conf.DailyScripture.Schedule), func() { bot.SendScripture() })
|
|
|
|
|
c.Start()
|
|
|
|
|
bot.Cron = c
|
|
|
|
|
|
2026-10-02 18:00:08 -06:00
|
|
|
switch conf.Service.Method {
|
|
|
|
|
case "polling":
|
|
|
|
|
err = bot.SetPolling(ctx, conf)
|
|
|
|
|
case "webhook":
|
|
|
|
|
err = bot.SetWebhook(ctx, conf)
|
2026-09-11 15:19:49 -06:00
|
|
|
}
|
2026-10-02 18:00:08 -06:00
|
|
|
|
2026-09-11 15:19:49 -06:00
|
|
|
if err != nil {
|
2026-10-02 18:00:08 -06:00
|
|
|
panic(err)
|
2026-09-11 15:19:49 -06:00
|
|
|
}
|
2026-10-02 18:00:08 -06:00
|
|
|
|
2026-09-11 15:19:49 -06:00
|
|
|
bot.AttachHandlers()
|
2026-10-02 23:43:23 -06:00
|
|
|
bot.SetCommands(ctx)
|
2026-09-11 15:19:49 -06:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Bot) AttachHandlers() {
|
|
|
|
|
fmt.Println("add handlers")
|
|
|
|
|
b.Handler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
2026-10-02 18:00:08 -06:00
|
|
|
fmt.Println("handling message")
|
2026-09-11 15:19:49 -06:00
|
|
|
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() {
|
2026-10-02 18:00:08 -06:00
|
|
|
/*script, err := bible.OpenStore("scripture.json")
|
2026-09-11 15:19:49 -06:00
|
|
|
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),
|
2026-09-12 08:54:34 -06:00
|
|
|
RichMessage: tu.RichMessageMarkdown(fmt.Sprintf("**<u>Daily Scripture:</u><br>%s**\n\n%s (*%s*)", v.Verse, v.Content, v.Translation)),
|
2026-09-11 15:19:49 -06:00
|
|
|
})
|
|
|
|
|
//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-02 18:00:08 -06:00
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 23:43:23 -06:00
|
|
|
func (b *Bot) SetCommands(ctx context.Context) (err error) {
|
|
|
|
|
commands, err := b.Bot.GetMyCommands(ctx, &telego.GetMyCommandsParams{})
|
|
|
|
|
fmt.Println(commands)
|
|
|
|
|
if len(commands) > 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b.Bot.SetMyCommands(ctx, &telego.SetMyCommandsParams{
|
|
|
|
|
Commands: []telego.BotCommand{
|
|
|
|
|
{
|
|
|
|
|
Command: "help",
|
|
|
|
|
Description: "Displays bot usage and help.",
|
|
|
|
|
IsEphemeral: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Command: "settings",
|
|
|
|
|
Description: "Bot settings.",
|
|
|
|
|
IsEphemeral: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Command: "start",
|
|
|
|
|
Description: "Starts the bot and links it to the current user's channel.",
|
|
|
|
|
IsEphemeral: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 18:00:08 -06:00
|
|
|
func (b *Bot) SetPolling(ctx context.Context, conf *settings.Settings) (err error) {
|
|
|
|
|
err = b.Bot.DeleteWebhook(ctx,
|
|
|
|
|
&telego.DeleteWebhookParams{
|
|
|
|
|
DropPendingUpdates: false,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-09-11 15:19:49 -06:00
|
|
|
}
|
2026-10-02 18:00:08 -06:00
|
|
|
|
|
|
|
|
updates, err := b.Bot.UpdatesViaLongPolling(ctx, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.Handler, err = th.NewBotHandler(b.Bot, 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
updates, err := b.Bot.UpdatesViaWebhook(ctx,
|
|
|
|
|
telego.WebhookHTTPServeMux(
|
|
|
|
|
mux,
|
|
|
|
|
u.Path,
|
|
|
|
|
b.Bot.SecretToken()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.Handler, err = th.NewBotHandler(b.Bot, updates)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
_ = http.ListenAndServe(
|
|
|
|
|
conf.Service.Webhook.ListenAddr(),
|
|
|
|
|
mux)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return
|
2026-09-11 15:19:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
2026-09-10 15:44:40 -06:00
|
|
|
}
|