WIP.
This commit is contained in:
parent
098201418b
commit
58a01bff56
5 changed files with 99 additions and 26 deletions
|
|
@ -45,11 +45,14 @@ func NewApp(ctx context.Context, root vfs.VFS, conf *settings.Settings) (*App, e
|
|||
|
||||
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)
|
||||
|
||||
a.cron.Start()
|
||||
fmt.Println("cron started")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -92,6 +95,7 @@ func (a *App) scripture() {
|
|||
}
|
||||
|
||||
for _, chat := range chats {
|
||||
fmt.Println("chat:", chat)
|
||||
vv, err := a.db.Verses().RandomVerseForChat(chat.ID)
|
||||
if err != nil {
|
||||
fmt.Println("error retrieving random verse:", err)
|
||||
|
|
@ -133,6 +137,11 @@ func (a *App) scripture() {
|
|||
verse.SkipLineBreaks = true
|
||||
verse.IncludeVerseNum = false
|
||||
fmt.Println(verse.String())
|
||||
|
||||
fmt.Println("sending chat:", chat.ID)
|
||||
fmt.Println("to bot:", a.bot)
|
||||
a.bot.SendMarkdownMessage(chat.ID,
|
||||
fmt.Sprintf("**<u>Daily Scripture:</u><br>%s**\n\n%s (*%s*)", ref.String(), verse.String(), vv.Translation))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -144,8 +153,9 @@ func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
|
|||
return err
|
||||
}
|
||||
|
||||
bot := NewBot(ctx, a.settings)
|
||||
bot.Run()
|
||||
fmt.Println("context:", ctx, a.ctx)
|
||||
a.bot = NewBot(ctx, a.settings)
|
||||
a.bot.Run()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
||||
"github.com/mymmrac/telego"
|
||||
th "github.com/mymmrac/telego/telegohandler"
|
||||
tu "github.com/mymmrac/telego/telegoutil"
|
||||
)
|
||||
|
||||
type bot struct {
|
||||
|
|
@ -46,23 +47,6 @@ func (b *bot) init() {
|
|||
}
|
||||
b.tgbot = tgbot
|
||||
|
||||
err = b.tgbot.SetWebhook(b.ctx, &telego.SetWebhookParams{
|
||||
URL: b.settings.Service.Webhook.URI,
|
||||
SecretToken: b.tgbot.SecretToken(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed setting up webhook: %s", err))
|
||||
}
|
||||
info, err := b.tgbot.GetWebhookInfo(b.ctx)
|
||||
fmt.Println(info, err)
|
||||
|
||||
/*c := cron.New()
|
||||
c.AddFunc(fmt.Sprintf("TZ=%s %s",
|
||||
b.settings.System.TimeZone,
|
||||
b.settings.DailyScripture.Schedule), func() { b.SendScripture() })
|
||||
c.Start()
|
||||
b.cron = c*/
|
||||
|
||||
switch b.settings.Service.Method {
|
||||
case "polling":
|
||||
err = b.SetPolling(b.ctx, b.settings)
|
||||
|
|
@ -99,6 +83,36 @@ func (b *bot) AttachHandlers() {
|
|||
}, th.CommandEqual("start"))
|
||||
}
|
||||
|
||||
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)))
|
||||
}*/
|
||||
}
|
||||
|
||||
func (b *bot) SendScripture() {
|
||||
/*script, err := bible.OpenStore("scripture.json")
|
||||
if err != nil {
|
||||
|
|
@ -196,13 +210,22 @@ func (b *bot) SetCommands(ctx context.Context) (err error) {
|
|||
}
|
||||
|
||||
func (b *bot) SetPolling(ctx context.Context, conf *settings.Settings) (err error) {
|
||||
err = b.tgbot.DeleteWebhook(ctx,
|
||||
&telego.DeleteWebhookParams{
|
||||
DropPendingUpdates: false,
|
||||
})
|
||||
info, err := b.tgbot.GetWebhookInfo(b.ctx)
|
||||
if err != nil {
|
||||
fmt.Println("webhook info error:", err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("webhook info:", info)
|
||||
|
||||
if info.URL == "" {
|
||||
err = b.tgbot.DeleteWebhook(ctx,
|
||||
&telego.DeleteWebhookParams{
|
||||
DropPendingUpdates: false,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
updates, err := b.tgbot.UpdatesViaLongPolling(ctx, nil)
|
||||
if err != nil {
|
||||
|
|
@ -223,6 +246,19 @@ func (b *bot) SetWebhook(ctx context.Context, conf *settings.Settings) (err erro
|
|||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
updates, err := b.tgbot.UpdatesViaWebhook(ctx,
|
||||
telego.WebhookHTTPServeMux(
|
||||
|
|
@ -248,6 +284,7 @@ func (b *bot) SetWebhook(ctx context.Context, conf *settings.Settings) (err erro
|
|||
}
|
||||
|
||||
func (b *bot) Run() error {
|
||||
fmt.Println("bot.Run()")
|
||||
defer func() {
|
||||
b.handler.Stop()
|
||||
b.cancel()
|
||||
|
|
|
|||
Loading…
Reference in a new issue