-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from NivEz/long-polling
Long polling
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const Telenode = require('../src/bot'); | ||
|
||
const bot = new Telenode({ | ||
apiToken: process.env.API_TOKEN, | ||
}); | ||
|
||
bot.startLongPolling({ pollingDelay: 2000 }); // default is 1000ms | ||
|
||
// The usage is the same as using the webhook | ||
|
||
bot.onTextMessage('hello', async messageBody => { | ||
await bot.sendReplyKeyboard( | ||
messageBody.chat.id, | ||
'Click on one of the keyboard buttons...', | ||
inlineKeyboard, | ||
true, | ||
); | ||
}); | ||
|
||
const inlineKeyboard = [ | ||
[ | ||
{ | ||
text: 'Hi!', | ||
}, | ||
{ | ||
text: 'Bye!', | ||
}, | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const axios = require('axios'); | ||
|
||
const longPoll = async ({ bot, pollingDelay = 1000, url }) => { | ||
if (pollingDelay < 50) { | ||
throw new Error('Polling delay must be at least 50ms'); | ||
} | ||
let offset = 0; | ||
while (true) { | ||
try { | ||
const res = await axios.get(url + '/getUpdates', { | ||
params: { offset }, | ||
}); | ||
const updates = res.data.result; | ||
updates.forEach(update => { | ||
bot.telenodeHandler(update); | ||
offset = update.update_id + 1; | ||
}); | ||
} catch (err) { | ||
if (err.name === 'AxiosError') { | ||
console.error( | ||
'Request error in long polling:\n', | ||
err.message, | ||
'\n', | ||
err.response.data, | ||
); | ||
} else { | ||
// If the handlers throw an error and it is not async, it will be caught here | ||
console.error(err); | ||
} | ||
} | ||
await sleep(pollingDelay); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
longPoll, | ||
}; | ||
|
||
const sleep = ms => new Promise(r => setTimeout(r, ms)); |