Skip to content

Commit

Permalink
fix(telegram): polling when stops bot
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Aug 30, 2023
1 parent a3ea0c8 commit e60f010
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions adapters/telegram/src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = new Logger('telegram')

export class HttpPolling extends Adapter.Client<TelegramBot> {
private offset = 0
private timeout: NodeJS.Timeout

async start(bot: TelegramBot<TelegramBot.BaseConfig & HttpPolling.Config>) {
bot.initialize(async () => {
Expand All @@ -28,15 +29,14 @@ export class HttpPolling extends Adapter.Client<TelegramBot> {
})

const polling = async () => {
if (bot.status === 'disconnect') {
return bot.offline()
}

try {
const updates = await bot.internal.getUpdates({
offset: this.offset + 1,
timeout: Math.ceil(bot.config.pollingTimeout / 1000), // in seconds
})
if (bot.status === 'disconnect') {
return bot.offline()
}
bot.online()
_retryCount = 0
_initial = false
Expand All @@ -62,15 +62,17 @@ export class HttpPolling extends Adapter.Client<TelegramBot> {
}
_retryCount++
bot.status = 'reconnect'
setTimeout(() => polling(), retryInterval)
this.timeout = setTimeout(() => polling(), retryInterval)
}
}
polling()
logger.debug('listening updates %c', 'telegram: ' + bot.selfId)
})
}

async stop() {}
async stop() {
clearTimeout(this.timeout)
}
}

export namespace HttpPolling {
Expand Down

0 comments on commit e60f010

Please sign in to comment.