typescript-telegram-bot-api
Advanced tools
Comparing version 0.1.17 to 0.1.18
@@ -23,3 +23,5 @@ "use strict"; | ||
this.autoRetryLimit = options.autoRetryLimit || 0; | ||
this.polling = new pooling_1.Polling(this, []); | ||
this.allowedUpdates = options.allowedUpdates || []; | ||
this.pollingTimeout = options.pollingTimeout || 50; | ||
this.polling = new pooling_1.Polling(this); | ||
} | ||
@@ -29,6 +31,6 @@ static isTelegramError(error) { | ||
} | ||
startPolling() { | ||
async startPolling() { | ||
this.polling.start(); | ||
} | ||
stopPolling() { | ||
async stopPolling() { | ||
this.polling.stop(); | ||
@@ -35,0 +37,0 @@ } |
import { TelegramBot } from './index'; | ||
import { UpdateType } from './types/'; | ||
export declare class Polling { | ||
private readonly telegramBot; | ||
private readonly allowedUpdates; | ||
private readonly abortController; | ||
private offset; | ||
constructor(telegramBot: TelegramBot, allowedUpdates: UpdateType[]); | ||
constructor(telegramBot: TelegramBot); | ||
private emitMessage; | ||
private emitUpdate; | ||
private poll; | ||
start(): void; | ||
stop(): void; | ||
start(): Promise<void>; | ||
stop(): Promise<void>; | ||
} |
@@ -5,6 +5,6 @@ "use strict"; | ||
const types_1 = require("./types/"); | ||
const errors_1 = require("./errors"); | ||
class Polling { | ||
constructor(telegramBot, allowedUpdates) { | ||
constructor(telegramBot) { | ||
this.telegramBot = telegramBot; | ||
this.allowedUpdates = allowedUpdates; | ||
this.abortController = new AbortController(); | ||
@@ -36,17 +36,31 @@ this.offset = 0; | ||
while (!this.abortController.signal.aborted) { | ||
const updates = await this.telegramBot.getUpdates({ | ||
offset: this.offset, | ||
allowed_updates: this.allowedUpdates, | ||
timeout: 50, | ||
}, this.abortController); | ||
for (const update of updates) { | ||
this.emitUpdate(update); | ||
this.offset = update.update_id + 1; | ||
try { | ||
const updates = await this.telegramBot.getUpdates({ | ||
offset: this.offset, | ||
allowed_updates: this.telegramBot.allowedUpdates, | ||
timeout: this.telegramBot.pollingTimeout, | ||
}, this.abortController); | ||
for (const update of updates) { | ||
this.emitUpdate(update); | ||
this.offset = update.update_id + 1; | ||
} | ||
} | ||
catch (error) { | ||
if (error instanceof errors_1.TelegramError) { | ||
if (error.response.error_code === 409) { | ||
/* eslint-disable no-console */ | ||
console.warn(error.message); | ||
/* eslint-enable no-console */ | ||
} | ||
else { | ||
throw error; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
start() { | ||
this.poll().catch(); | ||
async start() { | ||
return this.poll(); | ||
} | ||
stop() { | ||
async stop() { | ||
this.abortController.abort(); | ||
@@ -53,0 +67,0 @@ } |
{ | ||
"type": "commonjs", | ||
"name": "typescript-telegram-bot-api", | ||
"version": "0.1.17", | ||
"version": "0.1.18", | ||
"description": "Telegram Bot API wrapper for Node.js written in TypeScript", | ||
@@ -6,0 +6,0 @@ "repository": "github:Borodin/typescript-telegram-bot-api", |
@@ -25,5 +25,9 @@ # 📦 typescript-telegram-bot-api | ||
bot.on('message', (message) => { | ||
console.log(message); | ||
console.log('Received message:', message.text); | ||
}); | ||
bot.on('message:sticker', (message) => { | ||
console.log('Received sticker:', message.sticker.emoji); | ||
}); | ||
api.getMe() | ||
@@ -80,2 +84,19 @@ .then(console.log) | ||
``` | ||
## Events | ||
TelegramBot is an EventEmitter that emits the [Update](https://core.telegram.org/bots/api#update) event and also emits events for each type of [Message](https://core.telegram.org/bots/api#message), such as `message:audio`, when the `audio` field is present in the message object. | ||
```typescript | ||
bot.on('message', (message) => { | ||
console.log('Received message:', message.text); | ||
}); | ||
bot.on('message_reaction', (messageReactionUpdated) => { | ||
console.log('Received message_reaction:', messageReactionUpdated); | ||
}); | ||
bot.on('message:audio', (message) => { | ||
console.log('Received audio:', message.audio.file_id); | ||
}); | ||
``` | ||
## Tests | ||
@@ -82,0 +103,0 @@ ```bash |
Sorry, the diff of this file is too big to display
244647
5790
106