UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var channel_1 = require("./channel");
14var logger_1 = require("./logger");
15var request = require("request");
16var Twilio = /** @class */ (function (_super) {
17 __extends(Twilio, _super);
18 function Twilio(bot) {
19 return _super.call(this, { bot: bot, log: new logger_1.TwilioLogger(bot) }) || this;
20 }
21 Twilio.prototype.execute = function (body, parameters, handler) {
22 var _this = this;
23 // log incoming
24 this.context.log.incoming(body);
25 return new Promise(function (resolve, reject) {
26 handler(_this.context.bot, body, "twilio", function (messages) {
27 var response = (messages || []).map(function (message) { return function () {
28 return new Promise(function (resolve, reject) {
29 var options = {
30 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",
31 method: 'POST',
32 json: true,
33 form: {
34 Body: message.text,
35 To: body.From,
36 From: body.To
37 },
38 headers: {
39 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')
40 },
41 };
42 request(options, function (err, response, result) {
43 _this.context.log.outgoing(options, result);
44 if (err) {
45 console.error(err);
46 return reject(err);
47 }
48 return resolve();
49 });
50 });
51 }; });
52 return response
53 .reduce(function (a, b) {
54 return a.then(function (messages) {
55 return b().then(function (message) {
56 return;
57 });
58 });
59 }, Promise.resolve()).then(function () {
60 resolve();
61 });
62 }, function (err) {
63 reject(err);
64 });
65 });
66 };
67 return Twilio;
68}(channel_1.Channel));
69exports.Twilio = Twilio;