node-telegram-api/lib/functions/fetch.js
Mahdi Dibaiee c1c80f1b81 Fix #8
2015-07-27 09:25:27 +04:30

43 lines
883 B
JavaScript

import unirest from 'unirest';
export default function fetch(path, data = {}) {
return new Promise((resolve, reject) => {
const files = {};
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 ||
!response.body || !response.body.ok) {
reject(response);
} else {
resolve(response.body);
}
});
});
}
export function getBody(stream) {
let data = '';
return new Promise((resolve, reject) => {
stream.on('data', chunk => {
data += chunk;
});
stream.on('end', () => {
resolve(data);
});
stream.on('error', reject);
});
}