recime-bot-runtime
Version:
This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.
61 lines (60 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var request = require("request");
var Telegram = /** @class */ (function () {
function Telegram(bot) {
this.bot = bot;
}
Telegram.prototype.send = function (message, filter) {
var _this = this;
var response = filter.map(function (id) { return function () {
return _this.sendToReceiver(message, id);
}; });
return response
.reduce(function (a, b) {
return a.then(function (messages) {
return b().then(function (message) {
return;
});
});
}, Promise.resolve([]));
};
Telegram.prototype.sendToReceiver = function (message, receiver) {
var _this = this;
message.chat_id = receiver;
var method = function () {
if (message.type
&& [
'message',
'photo'
].indexOf(message.type.toLowerCase()) >= 0) {
var method_1 = message.type.toLowerCase();
return "send" + method_1.substr(0, 1).toUpperCase() + method_1.substr(1, method_1.length - 1);
}
return "sendMessage";
};
var payload = message.payload;
payload.chat_id = receiver;
return new Promise(function (resolve, reject) {
request({
json: true,
method: "POST",
url: "https://api.telegram.org/bot" + (process.env.RECIME_TELEGRAM_ACCESS_TOKEN || _this.bot.config.RECIME_TELEGRAM_ACCESS_TOKEN) + "/" + method(),
body: payload
}, function (err, response, body) {
if (err) {
return reject(err);
}
if (!body.ok) {
return reject({
code: body.error_code,
message: body.description
});
}
resolve();
});
});
};
return Telegram;
}());
exports.Telegram = Telegram;