UNPKG

@botocrat/poller

Version:

Telegram server poller for Botocrat

35 lines (30 loc) 893 B
const nullFunction = () => void 0; module.exports = (bot, client, poll_timeout = 1000, onError = nullFunction) => { let offset = 0, timeout const processUpdates = (updates) => updates.forEach(({ update_id, ...update }) => { offset = update_id + 1 bot._processUpdate(update, client) }); const poll = () => client .getUpdates({ offset }) .then(processUpdates) .catch(onError) .finally((e) => timeout ? timeout.refresh() : (timeout = setTimeout(poll, poll_timeout))) return { async poll() { const me = bot.me= await client.getMe() console.log("Polling for @"+me.username+"\nWarning: Don't use 'poller' in production!\n\n") bot._init() client .deleteWebhook() .then(poll) .catch((err) => console.log(err)) }, stop: () => clearTimeout(timeout) } }