Updated to webhooks.
This commit is contained in:
parent
295ac3169d
commit
a9c58cba45
1 changed files with 40 additions and 1 deletions
|
|
@ -2,6 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"slices"
|
||||
|
|
@ -55,6 +57,14 @@ type Bot struct {
|
|||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
type interceptor struct {
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
func (i *interceptor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.URL)
|
||||
}
|
||||
|
||||
func NewBot() (bot *Bot, errch chan error, err error) {
|
||||
bot = &Bot{}
|
||||
|
||||
|
|
@ -74,6 +84,21 @@ func NewBot() (bot *Bot, errch chan error, err error) {
|
|||
}
|
||||
bot.Bot = b
|
||||
|
||||
u, err := url.Parse(conf.Service.Webhook.URI)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("malformed URI: %s", conf.Service.Webhook.URI))
|
||||
}
|
||||
|
||||
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,10 +106,23 @@ func NewBot() (bot *Bot, errch chan error, err error) {
|
|||
c.Start()
|
||||
bot.Cron = c
|
||||
|
||||
updates, err := b.UpdatesViaLongPolling(ctx, nil)
|
||||
/*updates, err := b.UpdatesViaLongPolling(ctx, nil)
|
||||
if err != nil {
|
||||
fmt.Errorf("failed receiving updates: %s", err)
|
||||
}*/
|
||||
|
||||
mux := http.NewServeMux()
|
||||
updates, err := b.UpdatesViaWebhook(ctx,
|
||||
telego.WebhookHTTPServeMux(mux, u.Path, b.SecretToken()))
|
||||
/*bot.Handler, err = th.NewBotHandler(b, updates)
|
||||
if err != nil {
|
||||
fmt.Errorf("failed configuring handlers: %s", err)
|
||||
}
|
||||
bot.AttachHandlers()*/
|
||||
go func() {
|
||||
_ = http.ListenAndServe(":3117", &interceptor{mux})
|
||||
}()
|
||||
|
||||
bot.Handler, err = th.NewBotHandler(b, updates)
|
||||
if err != nil {
|
||||
fmt.Errorf("failed configuring handlers: %s", err)
|
||||
|
|
@ -97,6 +135,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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue