2015-06-28 22:42:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, '__esModule', {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports['default'] = fetch;
|
2015-06-29 22:20:34 +00:00
|
|
|
exports.getBody = getBody;
|
2015-06-28 22:42:48 +00:00
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
|
|
|
|
|
|
|
var _https = require('https');
|
|
|
|
|
|
|
|
var _https2 = _interopRequireDefault(_https);
|
|
|
|
|
|
|
|
var _qs = require('qs');
|
|
|
|
|
|
|
|
var _qs2 = _interopRequireDefault(_qs);
|
|
|
|
|
|
|
|
function fetch(path, data) {
|
|
|
|
var post = _qs2['default'].stringify(data);
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var req = _https2['default'].request({
|
|
|
|
hostname: 'api.telegram.org',
|
|
|
|
method: data ? 'POST' : 'GET',
|
|
|
|
path: '/bot' + path,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
}, function (response) {
|
2015-06-29 22:20:34 +00:00
|
|
|
return getBody(response).then(function (res) {
|
2015-06-28 22:42:48 +00:00
|
|
|
try {
|
|
|
|
var json = JSON.parse(res);
|
|
|
|
resolve(json);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2015-06-29 22:20:34 +00:00
|
|
|
})['catch'](reject);
|
|
|
|
});
|
2015-06-28 22:42:48 +00:00
|
|
|
|
|
|
|
if (post) {
|
|
|
|
req.write(post);
|
|
|
|
}
|
|
|
|
req.end();
|
2015-06-29 01:01:41 +00:00
|
|
|
})['catch'](function (err) {
|
|
|
|
console.error('Error sending request', err);
|
|
|
|
});
|
2015-06-28 22:42:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:20:34 +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);
|
|
|
|
});
|
|
|
|
}
|