2015-07-06 00:05:25 +00:00
|
|
|
import unirest from 'unirest';
|
2015-07-02 00:40:13 +00:00
|
|
|
|
|
|
|
export default function fetch(path, data = {}) {
|
2015-06-26 17:20:54 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2015-07-06 00:05:25 +00:00
|
|
|
const files = {};
|
2015-07-02 21:49:36 +00:00
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
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 => {
|
2015-07-13 01:06:36 +00:00
|
|
|
if (response.statusType === 4 || response.statusType === 5 ||
|
2015-07-27 04:55:27 +00:00
|
|
|
!response.body || !response.body.ok) {
|
2015-07-06 00:05:25 +00:00
|
|
|
reject(response);
|
|
|
|
} else {
|
|
|
|
resolve(response.body);
|
2015-06-26 17:20:54 +00:00
|
|
|
}
|
2015-07-02 00:40:13 +00:00
|
|
|
});
|
2015-06-26 17:20:54 +00:00
|
|
|
});
|
|
|
|
}
|
2015-06-29 22:20:34 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|