node-telegram-api/lib/index.js

152 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-06-28 22:42:48 +00:00
import 'babel/polyfill';
2015-06-26 17:20:54 +00:00
import API from './api';
import {EventEmitter} from 'events';
const DEFAULTS = {
update: {
offset: 0,
timeout: 0.5,
limit: 100
}
};
process.on('uncaughtException', function(err) {
console.error(err.stack);
});
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.
*/
2015-06-26 17:20:54 +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);
this.msg = {};
2015-06-26 17:20:54 +00:00
// EventEmitter
this._events = {};
this._userEvents = [];
2015-06-26 17:20:54 +00:00
}
2015-06-28 22:42:48 +00:00
/**
* Gets information about the bot and then starts polling updates from API
* 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
* @return {promise} A promise which is resolved with the response of getMe
*/
2015-06-26 17:20:54 +00:00
start() {
let poll = function() {
return this.api.getUpdates(this.update).then(response => {
const again = wait(this.update.timeout * 1000).then(poll);
2015-06-26 17:20:54 +00:00
const result = response.result;
if (!result.length) {
return again;
2015-06-26 17:20:54 +00:00
}
if (!this.update.offset) {
const updateId = result[result.length - 1].update_id;
this.update.offset = updateId;
}
if (this.update) {
this.update.offset += 1;
}
this.emit('update', result);
2015-06-26 17:20:54 +00:00
result.forEach(res => {
let text = res.message.text;
2015-06-28 22:42:48 +00:00
if (text.startsWith('/')) {
// Commands are sent in /command@botusername format in groups
const regex = new RegExp(`@${this.info.username}$`);
text = text.replace(regex, '');
2015-06-26 17:20:54 +00:00
}
2015-06-28 22:42:48 +00:00
let ev = this._userEvents.find(({pattern}) => pattern.test(text));
if (!ev) {
return;
}
ev.listener(res.message);
2015-06-26 17:20:54 +00:00
});
return again;
2015-06-26 17:20:54 +00:00
});
}.bind(this);
2015-06-26 23:56:28 +00:00
return this.api.getMe().then(response => {
this.info = response.result;
return poll();
});
}
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}`);
}
this._userEvents.push({
2015-06-28 22:42:48 +00:00
pattern, listener
});
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
* @param {string} cmd the command string, should not include slash (/)
* @param {function} listener function to call when the command is received,
* gets the update
* @return {object} returns the bot object
*/
2015-06-26 17:20:54 +00:00
command(cmd, listener) {
this._userEvents.push({
2015-06-28 22:42:48 +00:00
pattern: new RegExp(`/${cmd}`),
listener
});
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) {
return message.send(this).catch(console.error);
2015-06-26 17:20:54 +00:00
}
}
const wait = (miliseconds) => {
return new Promise(resolve => {
setTimeout(resolve, miliseconds);
});
};