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

@ -33,8 +33,13 @@ const ESCAPABLE = '.^$*+?()[{\\|}]'.split('');
* @return {object} Parsed arguments
*/
export default function argumentParser(format, string) {
string = string.replace(/[^\s]+/, '');
format = format.replace(/[^\s]+/, '');
string = string.replace(/[^\s]+/, '').trim();
format = format.replace(/[^\s]+/, '').trim();
if (!string || !format) {
return {};
}
let indexes = [];
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 = {}) {
return new Promise((resolve, reject) => {
const method = Object.keys(data).length ? 'POST' : 'GET';
const multipart = method === 'POST' ? true : false;
const files = {};
restler.request('https://api.telegram.org/bot' + path, {
data, method, multipart
}).on('complete', response => {
try {
let json = JSON.parse(response);
resolve(json);
} catch(e) {
reject(e);
for (let 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)
.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) {
const regex = /[^\s]+/;
const cmd = command.match(regex)[0];
const cmd = command.match(regex)[0].trim();
this._userEvents.push({
pattern: new RegExp(`^/${cmd}`),

View File

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