refactor: migrate from go-telegram-bot-api/v5 to go-telegram/bot
- Replaced tgbotapi with go-telegram/bot (zero-dependency, context-aware, Bot API 10.0) - All handler methods now accept context.Context; threading ctx through all sends/edits - Changed from function-based (NewMessage/NewEditMessageText/NewInlineKeyboardMarkup) to struct-param API (SendMessageParams/EditMessageTextParams/InlineKeyboardMarkup) - Added colored buttons: DEL buttons in calendar use Style: 'danger' (red) - Both polling and webhook modes preserved with new library patterns - Context-based shutdown (signal.NotifyContext) replaces stop channel
This commit is contained in:
@@ -1,34 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
tgbot "github.com/go-telegram/bot"
|
||||
|
||||
"worktimeBot/internal/bot"
|
||||
)
|
||||
|
||||
// startWebhook registers the webhook with Telegram and starts the HTTPS/HTTP listener loop.
|
||||
func startWebhook(botAPI *tgbotapi.BotAPI, handler *bot.Handler, webhookURL string) {
|
||||
if _, err := botAPI.Request(tgbotapi.DeleteWebhookConfig{}); err != nil {
|
||||
slog.Error("delete webhook", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
func startWebhook(ctx context.Context, b *tgbot.Bot, h *bot.Handler, webhookURL string) {
|
||||
b.DeleteWebhook(ctx, &tgbot.DeleteWebhookParams{})
|
||||
|
||||
wh, err := tgbotapi.NewWebhook(webhookURL)
|
||||
if err != nil {
|
||||
slog.Error("new webhook", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if _, err := botAPI.Request(wh); err != nil {
|
||||
if _, err := b.SetWebhook(ctx, &tgbot.SetWebhookParams{URL: webhookURL}); err != nil {
|
||||
slog.Error("set webhook", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
updates := botAPI.ListenForWebhook("/webhook")
|
||||
|
||||
listenAddr := getEnvDefault("BOT_LISTEN", ":8080")
|
||||
tlsCert := os.Getenv("BOT_TLS_CERT")
|
||||
tlsKey := os.Getenv("BOT_TLS_KEY")
|
||||
@@ -36,7 +27,7 @@ func startWebhook(botAPI *tgbotapi.BotAPI, handler *bot.Handler, webhookURL stri
|
||||
if tlsCert != "" && tlsKey != "" {
|
||||
slog.Info("starting HTTPS webhook", "addr", listenAddr)
|
||||
go func() {
|
||||
if err := http.ListenAndServeTLS(listenAddr, tlsCert, tlsKey, nil); err != nil {
|
||||
if err := http.ListenAndServeTLS(listenAddr, tlsCert, tlsKey, b.WebhookHandler()); err != nil {
|
||||
slog.Error("HTTPS server", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -44,7 +35,7 @@ func startWebhook(botAPI *tgbotapi.BotAPI, handler *bot.Handler, webhookURL stri
|
||||
} else {
|
||||
slog.Info("starting HTTP webhook (TLS by reverse proxy)", "addr", listenAddr)
|
||||
go func() {
|
||||
if err := http.ListenAndServe(listenAddr, nil); err != nil {
|
||||
if err := http.ListenAndServe(listenAddr, b.WebhookHandler()); err != nil {
|
||||
slog.Error("HTTP server", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -52,13 +43,5 @@ func startWebhook(botAPI *tgbotapi.BotAPI, handler *bot.Handler, webhookURL stri
|
||||
}
|
||||
|
||||
slog.Info("webhook registered", "url", webhookURL)
|
||||
|
||||
for update := range updates {
|
||||
if update.Message != nil {
|
||||
handler.HandleMessage(update)
|
||||
}
|
||||
if update.CallbackQuery != nil {
|
||||
handler.HandleCallback(update)
|
||||
}
|
||||
}
|
||||
b.StartWebhook(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user