2015-07-05 13:36:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-27 20:29:00 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", {
|
2015-07-05 13:36:27 +00:00
|
|
|
value: true
|
|
|
|
});
|
2016-03-27 20:29:00 +00:00
|
|
|
exports.default = fetch;
|
2015-07-05 13:36:27 +00:00
|
|
|
exports.getBody = getBody;
|
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
var _unirest = require('unirest');
|
2015-07-05 13:36:27 +00:00
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
var _unirest2 = _interopRequireDefault(_unirest);
|
2015-07-05 13:36:27 +00:00
|
|
|
|
2016-03-27 20:29:00 +00:00
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2015-07-05 13:36:27 +00:00
|
|
|
function fetch(path) {
|
2016-03-27 20:29:00 +00:00
|
|
|
var data = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
2015-07-05 13:36:27 +00:00
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2015-07-06 00:05:25 +00:00
|
|
|
var files = {};
|
2015-07-05 13:36:27 +00:00
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
var _iteratorNormalCompletion = true;
|
|
|
|
var _didIteratorError = false;
|
|
|
|
var _iteratorError = undefined;
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (var _iterator = Object.keys(data)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
|
|
var key = _step.value;
|
|
|
|
|
|
|
|
if (data[key].file) {
|
|
|
|
files[key] = data[key].file;
|
|
|
|
delete data[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
_didIteratorError = true;
|
|
|
|
_iteratorError = err;
|
|
|
|
} finally {
|
2015-07-05 13:36:27 +00:00
|
|
|
try {
|
2016-03-27 20:29:00 +00:00
|
|
|
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
|
|
_iterator.return();
|
2015-07-06 00:05:25 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (_didIteratorError) {
|
|
|
|
throw _iteratorError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:29:00 +00:00
|
|
|
_unirest2.default.post('https://api.telegram.org/bot' + path).field(data).attach(files).end(function (response) {
|
2015-07-27 05:38:52 +00:00
|
|
|
if (response.statusType === 4 || response.statusType === 5 || !response.body || !response.body.ok) {
|
2015-07-06 00:05:25 +00:00
|
|
|
reject(response);
|
|
|
|
} else {
|
|
|
|
resolve(response.body);
|
2015-07-05 13:36:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBody(stream) {
|
|
|
|
var data = '';
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
stream.on('data', function (chunk) {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('end', function () {
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('error', reject);
|
|
|
|
});
|
|
|
|
}
|