58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package notificationimp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/tech/sendico/notification/internal/server/notificationimp/telegram"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const telegramWebhookMaxBody = 1 << 20
|
|
|
|
func (a *NotificationAPI) handleTelegramWebhook(w http.ResponseWriter, r *http.Request) {
|
|
if a == nil {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
if a.confirm == nil {
|
|
if a.logger != nil {
|
|
a.logger.Warn("Telegram webhook ignored: confirmation manager is not configured")
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
var update telegram.Update
|
|
dec := json.NewDecoder(io.LimitReader(r.Body, telegramWebhookMaxBody))
|
|
if err := dec.Decode(&update); err != nil {
|
|
if a.logger != nil {
|
|
a.logger.Warn("Failed to decode telegram webhook update", zap.Error(err))
|
|
}
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if a.logger != nil {
|
|
fields := []zap.Field{
|
|
zap.Int64("update_id", update.UpdateID),
|
|
zap.Bool("has_message", update.Message != nil),
|
|
}
|
|
if update.Message != nil {
|
|
fields = append(fields,
|
|
zap.Int64("message_id", update.Message.MessageID),
|
|
zap.Int64("chat_id", update.Message.Chat.ID),
|
|
zap.Int("text_length", len(update.Message.Text)),
|
|
)
|
|
if update.Message.From != nil {
|
|
fields = append(fields, zap.Int64("from_user_id", update.Message.From.ID))
|
|
}
|
|
if update.Message.ReplyToMessage != nil {
|
|
fields = append(fields, zap.Int64("reply_to_message_id", update.Message.ReplyToMessage.MessageID))
|
|
}
|
|
}
|
|
a.logger.Info("Telegram webhook update received", fields...)
|
|
}
|
|
a.confirm.HandleUpdate(r.Context(), &update)
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|