chore(lint): update eslint configuration, re-lint
chore(gitignore): don't ignore src/types
This commit is contained in:
@ -20,7 +20,7 @@ export default class API {
|
||||
}
|
||||
|
||||
API.prototype.request = function request(method, data) {
|
||||
return fetch(this.token + '/' + method, data);
|
||||
return fetch(`${this.token}/${method}`, data);
|
||||
};
|
||||
|
||||
const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto',
|
||||
@ -29,7 +29,7 @@ const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto',
|
||||
'getUpdates', 'setWebhook'];
|
||||
|
||||
methods.forEach(method => {
|
||||
API.prototype[method] = function(data) {
|
||||
API.prototype[method] = function(data) { //eslint-disable-line
|
||||
return this.request(method, data);
|
||||
};
|
||||
});
|
||||
|
@ -8,6 +8,32 @@ const REQUIRED = 0;
|
||||
const OPTIONAL = 1;
|
||||
const REST = 2;
|
||||
|
||||
function escape(symbols, append = '') {
|
||||
return symbols.split('').map(symbol =>
|
||||
(ESCAPABLE.indexOf(symbol) ? `\\${symbol}` : symbol) + append
|
||||
).join('');
|
||||
}
|
||||
|
||||
const TYPES = {
|
||||
number: '\\d',
|
||||
word: '\\S'
|
||||
};
|
||||
|
||||
function getFormat(type = 'word', param = 'required') {
|
||||
const t = TYPES[type];
|
||||
|
||||
switch (param) { // eslint-disable-line
|
||||
case 'required':
|
||||
return `(${t}+)`;
|
||||
case 'optional':
|
||||
return `(${t}+)?`;
|
||||
case 'rest':
|
||||
return '(.*)';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a message for arguments, based on format
|
||||
*
|
||||
@ -41,74 +67,49 @@ export default function argumentParser(format, string) {
|
||||
format = format.replace(/[^\s]+/, '').trim();
|
||||
|
||||
if (!format) {
|
||||
return {args: {}, params: {}};
|
||||
return { args: {}, params: {} };
|
||||
}
|
||||
|
||||
let indexes = [],
|
||||
params = {};
|
||||
let indexes = [];
|
||||
const params = {};
|
||||
|
||||
format = format.replace(/\s/g, '\\s*');
|
||||
format = format.replace(FORMAT_REQUIRED,
|
||||
(f, symbols, arg, type = 'word', offset) => {
|
||||
indexes.push({arg, offset});
|
||||
indexes.push({ arg, offset });
|
||||
params[arg] = REQUIRED;
|
||||
return (escape(symbols) + getFormat(type, 'required')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_OPTIONAL,
|
||||
(f, symbols, arg, type = 'word', offset) => {
|
||||
indexes.push({arg, offset});
|
||||
indexes.push({ arg, offset });
|
||||
params[arg] = OPTIONAL;
|
||||
return (escape(symbols, '?') + getFormat(type, 'optional')).trim();
|
||||
});
|
||||
format = format.replace(FORMAT_REST, (full, arg, offset) => {
|
||||
indexes.push({offset, arg});
|
||||
indexes.push({ offset, arg });
|
||||
params[arg] = REST;
|
||||
return getFormat(null, 'rest');
|
||||
});
|
||||
|
||||
if (!string) {
|
||||
return {args: {}, params};
|
||||
return { args: {}, params };
|
||||
}
|
||||
|
||||
indexes = indexes.sort((a, b) => {
|
||||
return a.offset < b.offset ? -1 : 1;
|
||||
});
|
||||
indexes = indexes.sort((a, b) =>
|
||||
(a.offset < b.offset ? -1 : 1)
|
||||
);
|
||||
|
||||
const regex = new RegExp(format);
|
||||
|
||||
const matched = regex.exec(string).slice(1);
|
||||
|
||||
const object = {};
|
||||
for (let [index, match] of matched.entries()) {
|
||||
for (const [index, match] of matched.entries()) {
|
||||
const argument = indexes[index];
|
||||
|
||||
object[argument.arg] = match;
|
||||
}
|
||||
|
||||
return {args: object, params};
|
||||
}
|
||||
|
||||
function escape(symbols, append = '') {
|
||||
return symbols.split('').map(symbol => {
|
||||
return (ESCAPABLE.indexOf(symbol) ? `\\${symbol}` : symbol) + append;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
|
||||
const TYPES = {
|
||||
'number': '\\d',
|
||||
'word': '\\S'
|
||||
};
|
||||
|
||||
function getFormat(type = 'word', param = 'required') {
|
||||
const t = TYPES[type];
|
||||
|
||||
switch (param) {
|
||||
case 'required':
|
||||
return `(${t}+)`;
|
||||
case 'optional':
|
||||
return `(${t}+)?`;
|
||||
case 'rest':
|
||||
return `(.*)`;
|
||||
}
|
||||
return { args: object, params };
|
||||
}
|
||||
|
@ -4,14 +4,14 @@ export default function fetch(path, data = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const files = {};
|
||||
|
||||
for (let key of Object.keys(data)) {
|
||||
for (const key of Object.keys(data)) {
|
||||
if (data[key].file) {
|
||||
files[key] = data[key].file;
|
||||
delete data[key];
|
||||
}
|
||||
}
|
||||
|
||||
unirest.post('https://api.telegram.org/bot' + path)
|
||||
unirest.post(`https://api.telegram.org/bot${path}`)
|
||||
.field(data)
|
||||
.attach(files)
|
||||
.end(response => {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import https from 'http';
|
||||
import qs from 'qs';
|
||||
import {getBody} from './fetch';
|
||||
import { getBody } from './fetch';
|
||||
|
||||
const DEFAULTS = {
|
||||
server: {},
|
||||
@ -11,14 +11,12 @@ export default function webhook(options = {}, bot) {
|
||||
options = Object.assign(DEFAULTS, options);
|
||||
|
||||
return bot.api.setWebhook(options.url).then(() => {
|
||||
|
||||
bot._webhookServer = https.createServer(options.server, (req, res) => {
|
||||
return getBody(req).then(data => {
|
||||
bot._webhookServer = https.createServer(options.server, (req, res) =>
|
||||
getBody(req).then(data => {
|
||||
bot.emit('update', qs.parse(data).result);
|
||||
|
||||
res.end('OK');
|
||||
});
|
||||
}).listen(options.port);
|
||||
|
||||
})
|
||||
).listen(options.port);
|
||||
});
|
||||
}
|
||||
|
20
src/index.js
20
src/index.js
@ -3,7 +3,7 @@ import API from './functions/api';
|
||||
import webhook from './functions/webhook';
|
||||
import poll from './functions/poll';
|
||||
import argumentParser from './functions/argument-parser';
|
||||
import {EventEmitter} from 'events';
|
||||
import { EventEmitter } from 'events';
|
||||
import Message from './types/Message';
|
||||
import File from './types/File';
|
||||
import Keyboard from './types/Keyboard';
|
||||
@ -31,7 +31,7 @@ export default class Bot extends EventEmitter {
|
||||
* Create and connect to a new bot
|
||||
* @param {object} options Bot properties.
|
||||
*/
|
||||
constructor(options = {update: {}}) {
|
||||
constructor(options = { update: {} }) {
|
||||
super();
|
||||
|
||||
if (!options.token) {
|
||||
@ -80,9 +80,9 @@ export default class Bot extends EventEmitter {
|
||||
|
||||
if (hook) {
|
||||
return webhook(hook, this);
|
||||
} else {
|
||||
return poll(this);
|
||||
}
|
||||
|
||||
return poll(this);
|
||||
});
|
||||
}
|
||||
|
||||
@ -182,7 +182,7 @@ export default class Bot extends EventEmitter {
|
||||
res.message.text = text;
|
||||
}
|
||||
|
||||
let ev = this._userEvents.find(({pattern}) => pattern.test(text));
|
||||
const ev = this._userEvents.find(({ pattern }) => pattern.test(text));
|
||||
|
||||
if (!ev) {
|
||||
this.emit('command-notfound', res.message);
|
||||
@ -194,12 +194,12 @@ export default class Bot extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
let {params, args} = ev.parse(res.message.text);
|
||||
const { params, args } = ev.parse(res.message.text);
|
||||
res.message.args = args;
|
||||
|
||||
const requiredParams = Object.keys(params).filter(param => {
|
||||
return params[param] === REQUIRED && !args[param];
|
||||
});
|
||||
const requiredParams = Object.keys(params).filter(param =>
|
||||
params[param] === REQUIRED && !args[param]
|
||||
);
|
||||
|
||||
if (!requiredParams.length) {
|
||||
ev.listener(res.message);
|
||||
@ -208,7 +208,7 @@ export default class Bot extends EventEmitter {
|
||||
|
||||
const bot = this;
|
||||
function* getAnswer() {
|
||||
for (let param of requiredParams) {
|
||||
for (const param of requiredParams) {
|
||||
const msg = new Message().to(res.message.chat.id)
|
||||
.text(`Enter value for ${param}`);
|
||||
yield bot.send(msg).then(answer => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {EventEmitter} from 'events';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
const ANSWER_THRESHOLD = 10;
|
||||
|
||||
@ -29,8 +29,8 @@ export default class Base extends EventEmitter {
|
||||
*/
|
||||
send(bot) {
|
||||
if (this._keyboard) {
|
||||
const reply_markup = JSON.stringify(this._keyboard.getProperties());
|
||||
this.properties.reply_markup = reply_markup;
|
||||
const replyMarkup = JSON.stringify(this._keyboard.getProperties());
|
||||
this.properties.reply_markup = replyMarkup;
|
||||
}
|
||||
|
||||
let messageId;
|
||||
@ -49,15 +49,15 @@ export default class Base extends EventEmitter {
|
||||
bot.on('update', function listener(result) {
|
||||
answers += result.length;
|
||||
|
||||
const update = result.find(({message}) => {
|
||||
const update = result.find(({ message }) => {
|
||||
// if in a group, there will be a reply to this message
|
||||
if (chat < 0) {
|
||||
return message.chat.id === chat
|
||||
&& message.reply_to_message
|
||||
&& message.reply_to_message.message_id === messageId;
|
||||
} else {
|
||||
return message.chat.id === chat;
|
||||
}
|
||||
|
||||
return message.chat.id === chat;
|
||||
});
|
||||
|
||||
if (update) {
|
||||
|
@ -20,9 +20,7 @@ export default class BulkMessage extends Message {
|
||||
* @return {object} returns the message object
|
||||
*/
|
||||
to(...args) {
|
||||
const chats = args.reduce((a, b) => {
|
||||
return a.concat(b);
|
||||
}, []);
|
||||
const chats = args.reduce((a, b) => a.concat(b), []);
|
||||
|
||||
this.chats = chats;
|
||||
return this;
|
||||
|
@ -37,12 +37,12 @@ export default class File extends Base {
|
||||
*/
|
||||
file(file, fileType) {
|
||||
if (fileType) {
|
||||
this.properties[fileType] = {file: file};
|
||||
this.properties[fileType] = { file };
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
let [type, extension] = mime.lookup(file).split('/');
|
||||
let [type, extension] = mime.lookup(file).split('/'); // eslint-disable-line
|
||||
if (type === 'image') {
|
||||
type = 'photo';
|
||||
}
|
||||
@ -55,7 +55,7 @@ export default class File extends Base {
|
||||
type = 'document';
|
||||
}
|
||||
|
||||
this.properties[type] = {file: file};
|
||||
this.properties[type] = { file };
|
||||
|
||||
this.method = `send${type[0].toUpperCase() + type.slice(1)}`;
|
||||
|
||||
|
@ -15,7 +15,7 @@ export default class Question extends Message {
|
||||
constructor(options = {}) {
|
||||
super(options);
|
||||
|
||||
let kb = new Keyboard().force().oneTime().selective();
|
||||
const kb = new Keyboard().force().oneTime().selective();
|
||||
this.keyboard(kb);
|
||||
|
||||
this.answers(options.answers);
|
||||
@ -60,10 +60,10 @@ export default class Question extends Message {
|
||||
if (answer) {
|
||||
this.emit('question:answer', answer, message);
|
||||
return message;
|
||||
} else {
|
||||
this.emit('question:invalid', message);
|
||||
throw message;
|
||||
}
|
||||
|
||||
this.emit('question:invalid', message);
|
||||
throw message;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user