fix(telegram): filter self inbound messages by bot user id
This commit is contained in:
parent
8072a0c299
commit
777b252a23
1 changed files with 22 additions and 8 deletions
|
|
@ -44,6 +44,7 @@ type TelegramChannel struct {
|
||||||
bh *telegohandler.BotHandler
|
bh *telegohandler.BotHandler
|
||||||
commands TelegramCommander
|
commands TelegramCommander
|
||||||
config *config.Config
|
config *config.Config
|
||||||
|
botUserID int64
|
||||||
chatIDs map[string]int64
|
chatIDs map[string]int64
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
|
|
@ -100,6 +101,12 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
|
||||||
logger.InfoC("telegram", "Starting Telegram bot (polling mode)...")
|
logger.InfoC("telegram", "Starting Telegram bot (polling mode)...")
|
||||||
|
|
||||||
c.ctx, c.cancel = context.WithCancel(ctx)
|
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{
|
updates, err := c.bot.UpdatesViaLongPolling(c.ctx, &telego.GetUpdatesParams{
|
||||||
Timeout: 30,
|
Timeout: 30,
|
||||||
|
|
@ -138,7 +145,8 @@ func (c *TelegramChannel) Start(ctx context.Context) error {
|
||||||
|
|
||||||
c.SetRunning(true)
|
c.SetRunning(true)
|
||||||
logger.InfoCF("telegram", "Telegram bot connected", map[string]any{
|
logger.InfoCF("telegram", "Telegram bot connected", map[string]any{
|
||||||
"username": c.bot.Username(),
|
"username": me.Username,
|
||||||
|
"user_id": me.ID,
|
||||||
})
|
})
|
||||||
|
|
||||||
go bh.Start()
|
go bh.Start()
|
||||||
|
|
@ -355,6 +363,12 @@ func (c *TelegramChannel) handleMessage(ctx context.Context, message *telego.Mes
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return fmt.Errorf("message sender (user) is 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 {
|
if user.IsBot {
|
||||||
logger.DebugCF("telegram", "Ignoring bot-originated message", map[string]any{
|
logger.DebugCF("telegram", "Ignoring bot-originated message", map[string]any{
|
||||||
"user_id": user.ID,
|
"user_id": user.ID,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue