UNPKG

recime-bot-runtime

Version:

This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.

70 lines (69 loc) 3.25 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var channel_1 = require("./channel"); var logger_1 = require("./logger"); var request = require("request"); var Twilio = /** @class */ (function (_super) { __extends(Twilio, _super); function Twilio(bot) { return _super.call(this, { bot: bot, log: new logger_1.TwilioLogger(bot) }) || this; } Twilio.prototype.execute = function (body, parameters, handler) { var _this = this; // log incoming this.context.log.incoming(body); return new Promise(function (resolve, reject) { handler(_this.context.bot, body, "twilio", function (messages) { var response = (messages || []).map(function (message) { return function () { return new Promise(function (resolve, reject) { var options = { url: "https://api.twilio.com/2010-04-01/Accounts/" + (process.env.RECIME_TWILIO_ACCOUNT_SID || _this.context.bot.config.RECIME_TWILIO_ACCOUNT_SID) + "/Messages.json", method: 'POST', json: true, form: { Body: message.text, To: body.From, From: body.To }, headers: { authorization: "Basic " + new Buffer((process.env.RECIME_TWILIO_ACCOUNT_SID || _this.context.bot.config.RECIME_TWILIO_ACCOUNT_SID) + ":" + (process.env.RECIME_TWILIO_AUTH_TOKEN || _this.context.bot.config.RECIME_TWILIO_AUTH_TOKEN)).toString('base64') }, }; request(options, function (err, response, result) { _this.context.log.outgoing(options, result); if (err) { console.error(err); return reject(err); } return resolve(); }); }); }; }); return response .reduce(function (a, b) { return a.then(function (messages) { return b().then(function (message) { return; }); }); }, Promise.resolve()).then(function () { resolve(); }); }, function (err) { reject(err); }); }); }; return Twilio; }(channel_1.Channel)); exports.Twilio = Twilio;