UNPKG

teleapiwrapperwrapper

Version:

Wraps a longpoller and Event Emitter around teleapiwrapper

78 lines (75 loc) 3.04 kB
'use strict'; const EventEmitter = require('events').EventEmitter; const BotAPI = require('teleapiwrapper').BotAPI; module.exports = { createBot: token => { const bot = new BotAPI(token); const ee = new EventEmitter(); let offset = 0; for(let p in ee) bot[p] = ee[p]; bot.getUpdates().then(function _update(updates) { updates.forEach(update => { offset = update.update_id + 1; bot.emit('update', update); const u = 'undefined'; if (typeof update.message !== u) { const message = update.message; bot.emit('message', message); if (typeof message.text !== u) bot.emit('text', message); if (typeof message.audio !== u) bot.emit('audio', message); if (typeof message.document !== u) bot.emit('document', message); if (typeof message.photo !== u) bot.emit('photo', message); if (typeof message.sticker !== u) bot.emit('sticker', message); if (typeof message.video !== u) bot.emit('video', message); if (typeof message.voice !== u) bot.emit('video', message); if (typeof message.contact !== u) bot.emit('contact', message); if (typeof message.location !== u) bot.emit('location', message); if (typeof message.venue !== u) bot.emit('venue', message); if (typeof message.new_chat_member !== u) bot.emit('new_chat_member', message); if (typeof message.left_chat_member !== u) bot.emit('left_chat_member', message); if (typeof message.new_chat_title !== u) bot.emit('new_chat_title', message); if (typeof message.new_chat_photo !== u) bot.emit('new_chat_photo', message); if (typeof message.delete_chat_photo !== u) bot.emit('delete_chat_photo', message); if (typeof message.group_chat_created !== u) bot.emit('group_chat_created', message); if (typeof message.supergroup_chat_created !== u) bot.emit('supergroup_chat_created', message); if (typeof message.channel_chat_created !== u) bot.emit('channel_chat_created', message); if (typeof message.migrate_to_chat_id !== u) bot.emit('migrate_to_chat_id', message); if (typeof message.migrate_from_chat_id !== u) bot.emit('migrate_from_chat_id', message); if (typeof message.pinned_message !== u) bot.emit('pinned_message', message); } else if (typeof update.edited_message !== u) bot.emit('edited_message', update.edited_message); else if (typeof update.inline_query !== u) bot.emit('inline_query', update.inline_query); else if (typeof update.chosen_inline_result !== u) bot.emit('chosen_inline_result', update.chosen_inline_result); else if (typeof update.callback_query !== u) bot.emit('callback_query', update.callback_query); }); bot.getUpdates({ offset, timeout: bot.requestTimeout }).then(_update).catch(err => bot.emit('error', err)); }).catch(err => bot.emit('error', err)) return bot; } };