Switch from restler to unirest

restler is buggy and doesn't support utf8 multipart requests
This commit is contained in:
Mahdi Dibaiee 2015-07-06 04:35:25 +04:30
parent bd4f0ed027
commit b53817ced7
10 changed files with 81 additions and 73 deletions

View File

@ -43,8 +43,13 @@ var ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
*/ */
function argumentParser(format, string) { function argumentParser(format, string) {
string = string.replace(/[^\s]+/, ''); string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, ''); format = format.replace(/[^\s]+/, '').trim();
if (!string || !format) {
return {};
}
var indexes = []; var indexes = [];
format = format.replace(/\s/g, '\\s*'); format = format.replace(/\s/g, '\\s*');

View File

@ -8,25 +8,49 @@ exports.getBody = getBody;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _restler = require('restler'); var _unirest = require('unirest');
var _restler2 = _interopRequireDefault(_restler); var _unirest2 = _interopRequireDefault(_unirest);
function fetch(path) { function fetch(path) {
var data = arguments[1] === undefined ? {} : arguments[1]; var data = arguments[1] === undefined ? {} : arguments[1];
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var method = Object.keys(data).length ? 'POST' : 'GET'; var files = {};
var multipart = method === 'POST' ? true : false;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
_restler2['default'].request('https://api.telegram.org/bot' + path, {
data: data, method: method, multipart: multipart
}).on('complete', function (response) {
try { try {
var json = JSON.parse(response); for (var _iterator = Object.keys(data)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
resolve(json); var key = _step.value;
} catch (e) {
reject(e); if (data[key].file) {
files[key] = data[key].file;
delete data[key];
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
_unirest2['default'].post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) {
if (response.statusType === 4 || response.statusType === 5) {
reject(response);
} else {
resolve(response.body);
} }
}); });
}); });

View File

@ -154,7 +154,7 @@ var Bot = (function (_EventEmitter) {
value: function command(_command, listener) { value: function command(_command, listener) {
var regex = /[^\s]+/; var regex = /[^\s]+/;
var cmd = _command.match(regex)[0]; var cmd = _command.match(regex)[0].trim();
this._userEvents.push({ this._userEvents.push({
pattern: new RegExp('^/' + cmd), pattern: new RegExp('^/' + cmd),

View File

@ -24,18 +24,6 @@ var _mime = require('mime');
var _mime2 = _interopRequireDefault(_mime); var _mime2 = _interopRequireDefault(_mime);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _restler = require('restler');
var _restler2 = _interopRequireDefault(_restler);
var TYPES = ['photo', 'video', 'document', 'audio']; var TYPES = ['photo', 'video', 'document', 'audio'];
/** /**
@ -85,14 +73,11 @@ var File = (function (_Base) {
*/ */
value: function file(_file, fileType) { value: function file(_file, fileType) {
if (fileType) { if (fileType) {
this.properties[fileType] = _file; this.properties[fileType] = { file: _file };
return this; return this;
} }
var stat = _fs2['default'].statSync(_file);
var name = _path2['default'].basename(_file);
var _mime$lookup$split = _mime2['default'].lookup(_file).split('/'); var _mime$lookup$split = _mime2['default'].lookup(_file).split('/');
var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2);
@ -112,7 +97,7 @@ var File = (function (_Base) {
type = 'document'; type = 'document';
} }
this.properties[type] = _restler2['default'].file(_file, name, stat.size, 'utf-8'); this.properties[type] = { file: _file };
this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); this.method = 'send' + (type[0].toUpperCase() + type.slice(1));

View File

@ -33,8 +33,13 @@ const ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
* @return {object} Parsed arguments * @return {object} Parsed arguments
*/ */
export default function argumentParser(format, string) { export default function argumentParser(format, string) {
string = string.replace(/[^\s]+/, ''); string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, ''); format = format.replace(/[^\s]+/, '').trim();
if (!string || !format) {
return {};
}
let indexes = []; let indexes = [];
format = format.replace(/\s/g, '\\s*'); format = format.replace(/\s/g, '\\s*');

View File

@ -1,18 +1,24 @@
import restler from 'restler'; import unirest from 'unirest';
export default function fetch(path, data = {}) { export default function fetch(path, data = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const method = Object.keys(data).length ? 'POST' : 'GET'; const files = {};
const multipart = method === 'POST' ? true : false;
restler.request('https://api.telegram.org/bot' + path, { for (let key of Object.keys(data)) {
data, method, multipart if (data[key].file) {
}).on('complete', response => { files[key] = data[key].file;
try { delete data[key];
let json = JSON.parse(response); }
resolve(json); }
} catch(e) {
reject(e); unirest.post('https://api.telegram.org/bot' + path)
.field(data)
.attach(files)
.end(response => {
if (response.statusType === 4 || response.statusType === 5) {
reject(response);
} else {
resolve(response.body);
} }
}); });
}); });

View File

@ -109,7 +109,7 @@ export default class Bot extends EventEmitter {
command(command, listener) { command(command, listener) {
const regex = /[^\s]+/; const regex = /[^\s]+/;
const cmd = command.match(regex)[0]; const cmd = command.match(regex)[0].trim();
this._userEvents.push({ this._userEvents.push({
pattern: new RegExp(`^/${cmd}`), pattern: new RegExp(`^/${cmd}`),

View File

@ -1,8 +1,5 @@
import Base from './Base'; import Base from './Base';
import mime from 'mime'; import mime from 'mime';
import fs from 'fs';
import path from 'path';
import restler from 'restler';
const TYPES = ['photo', 'video', 'document', 'audio']; const TYPES = ['photo', 'video', 'document', 'audio'];
@ -40,14 +37,11 @@ export default class File extends Base {
*/ */
file(file, fileType) { file(file, fileType) {
if (fileType) { if (fileType) {
this.properties[fileType] = file; this.properties[fileType] = {file: file};
return this; return this;
} }
const stat = fs.statSync(file);
const name = path.basename(file);
let [type, extension] = mime.lookup(file).split('/'); let [type, extension] = mime.lookup(file).split('/');
if (type === 'image') { if (type === 'image') {
type = 'photo'; type = 'photo';
@ -61,7 +55,7 @@ export default class File extends Base {
type = 'document'; type = 'document';
} }
this.properties[type] = restler.file(file, name, stat.size, 'utf-8'); this.properties[type] = {file: file};
this.method = `send${type[0].toUpperCase() + type.slice(1)}`; this.method = `send${type[0].toUpperCase() + type.slice(1)}`;

View File

@ -1,6 +1,6 @@
{ {
"name": "telegram-api", "name": "telegram-api",
"version": "0.4.65", "version": "0.4.70",
"description": "Control Telegram bots easily using the new Telegram API", "description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -21,7 +21,11 @@
"bugs": { "bugs": {
"url": "https://github.com/mdibaiee/node-telegram-api/issues" "url": "https://github.com/mdibaiee/node-telegram-api/issues"
}, },
"files": ["types", "build", "index.js"], "files": [
"types",
"build",
"index.js"
],
"directories": { "directories": {
"lib": "lib" "lib": "lib"
}, },
@ -38,6 +42,6 @@
"grunt-eslint": "^16.0.0", "grunt-eslint": "^16.0.0",
"mime": "^1.3.4", "mime": "^1.3.4",
"qs": "^4.0.0", "qs": "^4.0.0",
"restler": "^3.3.0" "unirest": "^0.4.2"
} }
} }

View File

@ -24,18 +24,6 @@ var _mime = require('mime');
var _mime2 = _interopRequireDefault(_mime); var _mime2 = _interopRequireDefault(_mime);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _restler = require('restler');
var _restler2 = _interopRequireDefault(_restler);
var TYPES = ['photo', 'video', 'document', 'audio']; var TYPES = ['photo', 'video', 'document', 'audio'];
/** /**
@ -85,14 +73,11 @@ var File = (function (_Base) {
*/ */
value: function file(_file, fileType) { value: function file(_file, fileType) {
if (fileType) { if (fileType) {
this.properties[fileType] = _file; this.properties[fileType] = { file: _file };
return this; return this;
} }
var stat = _fs2['default'].statSync(_file);
var name = _path2['default'].basename(_file);
var _mime$lookup$split = _mime2['default'].lookup(_file).split('/'); var _mime$lookup$split = _mime2['default'].lookup(_file).split('/');
var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2); var _mime$lookup$split2 = _slicedToArray(_mime$lookup$split, 2);
@ -112,7 +97,7 @@ var File = (function (_Base) {
type = 'document'; type = 'document';
} }
this.properties[type] = _restler2['default'].file(_file, name, stat.size, 'utf-8'); this.properties[type] = { file: _file };
this.method = 'send' + (type[0].toUpperCase() + type.slice(1)); this.method = 'send' + (type[0].toUpperCase() + type.slice(1));