2016-04-19 08:58:30 +00:00
|
|
|
if (!global._babelPolyfill) {
|
2016-04-16 13:34:19 +00:00
|
|
|
require('babel-polyfill');
|
|
|
|
}
|
2015-07-05 13:36:27 +00:00
|
|
|
import API from './functions/api';
|
|
|
|
import webhook from './functions/webhook';
|
|
|
|
import poll from './functions/poll';
|
|
|
|
import argumentParser from './functions/argument-parser';
|
2016-03-28 16:10:59 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2015-07-09 09:56:17 +00:00
|
|
|
import Message from './types/Message';
|
2016-03-27 20:29:00 +00:00
|
|
|
import File from './types/File';
|
|
|
|
import Keyboard from './types/Keyboard';
|
|
|
|
import BulkMessage from './types/BulkMessage';
|
|
|
|
import Question from './types/Question';
|
|
|
|
import Forward from './types/Forward';
|
2015-06-26 17:20:54 +00:00
|
|
|
|
|
|
|
const DEFAULTS = {
|
|
|
|
update: {
|
|
|
|
offset: 0,
|
2015-07-05 11:11:22 +00:00
|
|
|
timeout: 20,
|
2015-06-26 17:20:54 +00:00
|
|
|
limit: 100
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-09 11:16:27 +00:00
|
|
|
const REQUIRED = 0;
|
2016-03-27 20:29:00 +00:00
|
|
|
|
2015-06-28 22:42:48 +00:00
|
|
|
/**
|
|
|
|
* Bot class used to connect to a new bot
|
|
|
|
* Bots have an api property which gives access to all Telegram API methods,
|
|
|
|
* see API class
|
|
|
|
*/
|
|
|
|
export default class Bot extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* Create and connect to a new bot
|
|
|
|
* @param {object} options Bot properties.
|
|
|
|
*/
|
2016-03-28 16:10:59 +00:00
|
|
|
constructor(options = { update: {} }) {
|
2015-06-28 22:42:48 +00:00
|
|
|
super();
|
|
|
|
|
2015-06-26 17:20:54 +00:00
|
|
|
if (!options.token) {
|
|
|
|
throw new Error('Token cannot be empty');
|
|
|
|
}
|
2015-06-28 22:42:48 +00:00
|
|
|
|
2015-06-26 17:20:54 +00:00
|
|
|
this.token = options.token;
|
|
|
|
this.update = Object.assign(options.update || {}, DEFAULTS.update);
|
|
|
|
|
|
|
|
this.api = new API(this.token);
|
|
|
|
|
2015-06-26 22:33:30 +00:00
|
|
|
this.msg = {};
|
|
|
|
|
2015-06-26 17:20:54 +00:00
|
|
|
// EventEmitter
|
|
|
|
this._events = {};
|
2015-06-26 22:33:30 +00:00
|
|
|
this._userEvents = [];
|
2015-07-04 11:18:07 +00:00
|
|
|
|
|
|
|
this.setMaxListeners(100);
|
2015-06-26 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 22:42:48 +00:00
|
|
|
/**
|
2015-06-29 22:20:34 +00:00
|
|
|
* Gets information about the bot and then
|
|
|
|
* 1) starts polling updates from API
|
|
|
|
* 2) sets a webhook as defined by the first parameter and listens for updates
|
2015-06-28 22:42:48 +00:00
|
|
|
* Emits an `update` event after polling with the response from server
|
|
|
|
* Returns a promise which is resolved after the bot information is received
|
|
|
|
* and set to it's `info` property i.e. bot.info
|
2015-06-29 22:20:34 +00:00
|
|
|
*
|
|
|
|
* @param {object} hook An object containg options passed to webhook
|
|
|
|
* properties:
|
|
|
|
* - url: HTTPS url to listen on POST requests coming
|
|
|
|
* from the Telegram API
|
|
|
|
* - port: the port to listen to, defaults to 443
|
|
|
|
* - server: An object passed to https.createServer
|
|
|
|
*
|
2015-06-28 22:42:48 +00:00
|
|
|
* @return {promise} A promise which is resolved with the response of getMe
|
|
|
|
*/
|
2015-06-29 22:20:34 +00:00
|
|
|
start(hook) {
|
|
|
|
if (hook) {
|
|
|
|
return webhook(hook, this);
|
|
|
|
}
|
2015-06-26 23:56:28 +00:00
|
|
|
return this.api.getMe().then(response => {
|
2015-06-26 22:33:30 +00:00
|
|
|
this.info = response.result;
|
2015-06-29 22:20:34 +00:00
|
|
|
|
|
|
|
this.on('update', this._update);
|
|
|
|
|
|
|
|
if (hook) {
|
|
|
|
return webhook(hook, this);
|
|
|
|
}
|
2016-03-28 16:10:59 +00:00
|
|
|
|
|
|
|
return poll(this);
|
2015-06-26 22:33:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-28 22:42:48 +00:00
|
|
|
/**
|
|
|
|
* Listens on specific message matching the pattern which can be an string
|
|
|
|
* or a regexp.
|
|
|
|
* @param {string/regex} pattern
|
|
|
|
* @param {function} listener function to call when a message matching the
|
|
|
|
* pattern is found, gets the Update
|
|
|
|
* In case of string, the message should start
|
|
|
|
* with the string i.e. /^yourString/
|
|
|
|
* @return {object} returns the bot object
|
|
|
|
*/
|
|
|
|
get(pattern, listener) {
|
|
|
|
if (typeof pattern === 'string') {
|
|
|
|
pattern = new RegExp(`^${pattern}`);
|
2015-06-26 22:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._userEvents.push({
|
2015-06-28 22:42:48 +00:00
|
|
|
pattern, listener
|
2015-06-26 22:33:30 +00:00
|
|
|
});
|
2015-06-28 22:42:48 +00:00
|
|
|
|
|
|
|
return this;
|
2015-06-26 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 22:42:48 +00:00
|
|
|
/**
|
|
|
|
* Listens on a command
|
2015-07-05 13:36:27 +00:00
|
|
|
* @param {string} command the command string, should not include slash (/)
|
2015-06-28 22:42:48 +00:00
|
|
|
* @param {function} listener function to call when the command is received,
|
|
|
|
* gets the update
|
|
|
|
* @return {object} returns the bot object
|
|
|
|
*/
|
2017-09-12 14:09:09 +00:00
|
|
|
command(command, listener, customMessage = {}) {
|
2015-07-05 13:36:27 +00:00
|
|
|
const regex = /[^\s]+/;
|
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
const cmd = command.match(regex)[0].trim();
|
2015-07-05 13:36:27 +00:00
|
|
|
|
2015-06-26 22:33:30 +00:00
|
|
|
this._userEvents.push({
|
2015-07-04 19:27:00 +00:00
|
|
|
pattern: new RegExp(`^/${cmd}`),
|
2015-07-05 13:36:27 +00:00
|
|
|
parse: argumentParser.bind(null, command),
|
2017-09-12 14:09:09 +00:00
|
|
|
listener,
|
|
|
|
customMessage
|
2015-06-26 22:33:30 +00:00
|
|
|
});
|
2015-06-28 22:42:48 +00:00
|
|
|
|
|
|
|
return this;
|
2015-06-26 17:20:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 22:42:48 +00:00
|
|
|
/**
|
|
|
|
* Sends the message provided
|
|
|
|
* @param {object} message The message to send. Gets it's send method called
|
|
|
|
* @return {unknown} returns the result of calling message's send method
|
|
|
|
*/
|
2015-06-28 01:04:18 +00:00
|
|
|
send(message) {
|
2015-06-29 01:00:29 +00:00
|
|
|
return message.send(this).catch(console.error);
|
2015-06-26 17:20:54 +00:00
|
|
|
}
|
2015-06-29 01:00:29 +00:00
|
|
|
|
2015-11-08 03:22:06 +00:00
|
|
|
/**
|
|
|
|
* Stops the bot, deattaching all listeners and polling
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
this._stop = true;
|
|
|
|
|
|
|
|
if (this._webhookServer) {
|
|
|
|
this._webhookServer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.removeListener('update', this._update);
|
|
|
|
this._events = {};
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:20:34 +00:00
|
|
|
/**
|
|
|
|
* The internal update event listener, used to parse messages and fire
|
|
|
|
* command/get events - YOU SHOULD NOT USE THIS
|
|
|
|
*
|
|
|
|
* @param {object} update
|
|
|
|
*/
|
|
|
|
_update(update) {
|
|
|
|
if (!this.update.offset) {
|
|
|
|
const updateId = update[update.length - 1].update_id;
|
|
|
|
this.update.offset = updateId;
|
|
|
|
}
|
|
|
|
if (this.update) {
|
|
|
|
this.update.offset += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
update.forEach(res => {
|
2016-12-14 15:09:14 +00:00
|
|
|
const msg = res.message || res.edited_message || res.channel_post || res.edited_channel_post;
|
2016-05-27 07:26:05 +00:00
|
|
|
let text = msg.text;
|
2016-03-27 20:29:00 +00:00
|
|
|
if (!text) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-06 22:43:33 +00:00
|
|
|
|
2015-07-04 19:55:07 +00:00
|
|
|
const selfUsername = `@${this.info.username}`;
|
|
|
|
|
|
|
|
if (text.startsWith('/') && text.indexOf(selfUsername) > -1) {
|
2015-06-29 22:20:34 +00:00
|
|
|
// Commands are sent in /command@thisusername format in groups
|
2015-07-04 19:35:58 +00:00
|
|
|
const regex = new RegExp(`(/.*)@${this.info.username}`);
|
|
|
|
text = text.replace(regex, '$1');
|
2016-05-27 07:26:05 +00:00
|
|
|
msg.text = text;
|
2015-06-29 22:20:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 16:10:59 +00:00
|
|
|
const ev = this._userEvents.find(({ pattern }) => pattern.test(text));
|
2015-06-29 22:20:34 +00:00
|
|
|
|
|
|
|
if (!ev) {
|
2016-05-27 07:26:05 +00:00
|
|
|
this.emit('command-notfound', msg);
|
2015-06-29 22:20:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-04 19:27:00 +00:00
|
|
|
|
2015-07-09 11:16:27 +00:00
|
|
|
if (!ev.parse) {
|
2016-05-27 07:26:05 +00:00
|
|
|
ev.listener(msg);
|
2015-07-09 11:16:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-27 07:26:05 +00:00
|
|
|
const { params, args } = ev.parse(msg.text);
|
|
|
|
msg.args = args;
|
2015-07-09 11:16:27 +00:00
|
|
|
|
2016-03-28 16:10:59 +00:00
|
|
|
const requiredParams = Object.keys(params).filter(param =>
|
|
|
|
params[param] === REQUIRED && !args[param]
|
|
|
|
);
|
2015-07-09 11:16:27 +00:00
|
|
|
|
|
|
|
if (!requiredParams.length) {
|
2016-05-27 07:26:05 +00:00
|
|
|
ev.listener(msg);
|
2015-07-09 11:16:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bot = this;
|
|
|
|
function* getAnswer() {
|
2017-09-12 09:09:58 +00:00
|
|
|
const customMessage = ev.customMessage;
|
|
|
|
|
2016-03-28 16:10:59 +00:00
|
|
|
for (const param of requiredParams) {
|
2017-09-12 15:56:28 +00:00
|
|
|
const ga = new Message()
|
|
|
|
.to(msg.chat.id)
|
|
|
|
.text(customMessage[param] || `Enter value for ${param}`);
|
2016-05-27 07:26:05 +00:00
|
|
|
yield bot.send(ga).then(answer => {
|
2015-07-09 11:16:27 +00:00
|
|
|
args[param] = answer.text;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const iterator = getAnswer();
|
|
|
|
(function loop() {
|
|
|
|
const next = iterator.next();
|
|
|
|
if (next.done) {
|
2016-05-27 07:26:05 +00:00
|
|
|
ev.listener(msg);
|
2015-07-09 09:56:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-09 11:16:27 +00:00
|
|
|
|
|
|
|
next.value.then(loop);
|
|
|
|
}());
|
2015-06-29 22:20:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-03-27 20:29:00 +00:00
|
|
|
|
|
|
|
export {
|
|
|
|
File,
|
|
|
|
Message,
|
|
|
|
BulkMessage,
|
|
|
|
Forward,
|
|
|
|
Question,
|
|
|
|
Keyboard
|
|
|
|
};
|