Skip to content

Commit

Permalink
Merge pull request #10 from NivEz/improve-long-polling
Browse files Browse the repository at this point in the history
Improve long polling
  • Loading branch information
NivEz committed Oct 25, 2023
2 parents faaaee2 + f5a5786 commit 6debcc3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ If you prefer to use long polling method over creating a server with webhook you
<br>
The method accepts `pollingDelay` - a number that represents milliseconds (must be at least 50ms).
<br>
By default the long polling mechanism will ignore all previous updates (`cleanPreviousUpdates` by default is `true`). You can toggle off `cleanPreviousUpdates` if you want to by setting it as `false` when you call `startLongPolling`.
<br>
You also can stop the long polling while it's running by setting `bot.useLongPolling` to `false`.
<br>
You can view an example of long polling usage [here](https://github.com/NivEz/telenode/tree/main/examples/long-polling.js).

Note that long polling is usually not recommended and webhook is preferred for most use cases.
Expand Down
5 changes: 5 additions & 0 deletions examples/long-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ const inlineKeyboard = [
},
],
];

// After 30 seconds the long polling will stop
setTimeout(() => {
bot.useLongPolling = false;
}, 30000);
8 changes: 7 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ class Telenode {
this.buttonHandlers = {};
this.#baseUrl = 'https://api.telegram.org/bot' + apiToken;
this.#secretToken = secretToken;
this.useLongPolling = false;
}

createServer({ port, unauthorizedCallback } = {}) {
this.unauthorizedHandler = unauthorizedCallback;
if (this.useLongPolling) {
throw Error('Cannot start server while long polling is active');
}
runServer(this, port);
}

startLongPolling({ pollingDelay } = {}) {
startLongPolling({ pollingDelay, cleanPreviousUpdates } = {}) {
this.useLongPolling = true;
longPoll({
bot: this,
pollingDelay,
cleanPreviousUpdates,
url: this.#baseUrl,
});
}
Expand Down
21 changes: 15 additions & 6 deletions src/longPolling.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const axios = require('axios');

const longPoll = async ({ bot, pollingDelay = 1000, url }) => {
const longPoll = async ({ bot, pollingDelay = 1000, cleanPreviousUpdates = true, url }) => {
if (pollingDelay < 50) {
throw new Error('Polling delay must be at least 50ms');
}
let offset = 0;
while (true) {
if (cleanPreviousUpdates) {
const recentUpdates = await getUpdates(url, -1);
offset = recentUpdates[0]?.update_id + 1 || 0;
}
while (bot.useLongPolling) {
try {
const res = await axios.get(url + '/getUpdates', {
params: { offset },
});
const updates = res.data.result;
const updates = await getUpdates(url, offset);
updates.forEach(update => {
bot.telenodeHandler(update);
offset = update.update_id + 1;
Expand All @@ -30,6 +31,14 @@ const longPoll = async ({ bot, pollingDelay = 1000, url }) => {
}
await sleep(pollingDelay);
}
console.log('Long polling stopped...');
};

const getUpdates = async (url, offset) => {
const res = await axios.get(url + '/getUpdates', {
params: { offset },
});
return res.data.result;
};

module.exports = {
Expand Down

0 comments on commit 6debcc3

Please sign in to comment.