Cleanup from prototype. WIP.
This commit is contained in:
parent
9f8811d6d3
commit
56d4d7c6bf
1 changed files with 74 additions and 8 deletions
|
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"slices"
|
||||
|
|
@ -10,12 +12,10 @@ import (
|
|||
"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"
|
||||
)
|
||||
|
||||
|
|
@ -74,6 +74,16 @@ func NewBot() (bot *Bot, errch chan error, err error) {
|
|||
}
|
||||
bot.Bot = b
|
||||
|
||||
err = b.SetWebhook(ctx, &telego.SetWebhookParams{
|
||||
URL: conf.Service.Webhook.URI,
|
||||
SecretToken: b.SecretToken(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed setting up webhook: %w", err))
|
||||
}
|
||||
info, err := b.GetWebhookInfo(ctx)
|
||||
fmt.Println(info, err)
|
||||
|
||||
c := cron.New()
|
||||
c.AddFunc(fmt.Sprintf("TZ=%s %s",
|
||||
conf.System.TimeZone,
|
||||
|
|
@ -81,14 +91,17 @@ func NewBot() (bot *Bot, errch chan error, err error) {
|
|||
c.Start()
|
||||
bot.Cron = c
|
||||
|
||||
updates, err := b.UpdatesViaLongPolling(ctx, nil)
|
||||
if err != nil {
|
||||
fmt.Errorf("failed receiving updates: %s", err)
|
||||
switch conf.Service.Method {
|
||||
case "polling":
|
||||
err = bot.SetPolling(ctx, conf)
|
||||
case "webhook":
|
||||
err = bot.SetWebhook(ctx, conf)
|
||||
}
|
||||
bot.Handler, err = th.NewBotHandler(b, updates)
|
||||
|
||||
if err != nil {
|
||||
fmt.Errorf("failed configuring handlers: %s", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bot.AttachHandlers()
|
||||
|
||||
return
|
||||
|
|
@ -97,6 +110,7 @@ func NewBot() (bot *Bot, errch chan error, err error) {
|
|||
func (b *Bot) AttachHandlers() {
|
||||
fmt.Println("add handlers")
|
||||
b.Handler.HandleMessage(func(ctx *th.Context, message telego.Message) error {
|
||||
fmt.Println("handling message")
|
||||
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)
|
||||
|
|
@ -116,7 +130,7 @@ func (b *Bot) AttachHandlers() {
|
|||
}
|
||||
|
||||
func (b *Bot) SendScripture() {
|
||||
script, err := bible.OpenStore("scripture.json")
|
||||
/*script, err := bible.OpenStore("scripture.json")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
|
@ -134,7 +148,59 @@ func (b *Bot) SendScripture() {
|
|||
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) SetPolling(ctx context.Context, conf *settings.Settings) (err error) {
|
||||
err = b.Bot.DeleteWebhook(ctx,
|
||||
&telego.DeleteWebhookParams{
|
||||
DropPendingUpdates: false,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (b *Bot) SaveConfig() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue