fix(telegram): filter self inbound messages by bot user id

This commit is contained in:
mosir 2026-03-01 13:35:56 +08:00
parent 8072a0c299
commit 777b252a23

View file

@ -44,6 +44,7 @@ type TelegramChannel struct {
bh *telegohandler.BotHandler
commands TelegramCommander
config *config.Config
botUserID int64
chatIDs map[string]int64
ctx context.Context
cancel context.CancelFunc
@ -100,6 +101,12 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
logger.InfoC("telegram", "Starting Telegram bot (polling mode)...")
c.ctx, c.cancel = context.WithCancel(ctx)
me, err := c.bot.GetMe(c.ctx)
if err != nil {
c.cancel()
return fmt.Errorf("failed to get bot identity: %w", err)
}
c.botUserID = me.ID
updates, err := c.bot.UpdatesViaLongPolling(c.ctx, &telego.GetUpdatesParams{
Timeout: 30,
@ -138,7 +145,8 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
c.SetRunning(true)
logger.InfoCF("telegram", "Telegram bot connected", map[string]any{
"username": c.bot.Username(),
"username": me.Username,
"user_id": me.ID,
})
go bh.Start()
@ -355,6 +363,12 @@ func (c *TelegramChannel) handleMessage(ctx context.Context, message *telego.Mes
if user == nil {
return fmt.Errorf("message sender (user) is nil")
}
if c.botUserID != 0 && user.ID == c.botUserID {
logger.DebugCF("telegram", "Ignoring self message by ID", map[string]any{
"user_id": user.ID,
})
return nil
}
if user.IsBot {
logger.DebugCF("telegram", "Ignoring bot-originated message", map[string]any{
"user_id": user.ID,