UNPKG

recime-bot-runtime

Version:

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

118 lines (117 loc) 5.43 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 request = require("request"); var channel_1 = require("./channel"); var logger_1 = require("./logger"); var MaxDuration = 15; var Viber = /** @class */ (function (_super) { __extends(Viber, _super); function Viber(bot) { return _super.call(this, { bot: bot, log: new logger_1.ViberLogger(bot) }) || this; } Viber.prototype.accountInfo = function () { var _this = this; return new Promise(function (resolve, reject) { var url = "https://chatapi.viber.com/pa/get_account_info"; var options = { json: true, url: url, headers: { "X-Viber-Auth-Token": process.env.RECIME_VIBER_ACCESS_TOKEN || _this.context.bot.config.RECIME_VIBER_ACCESS_TOKEN }, body: {} }; request(options, function (err, response, body) { if (body && body.length > 0) { if (body[0].status !== 'ok') { return reject(body); } } resolve(body); }); }); }; Viber.prototype.execute = function (body, parameters, handler) { var _this = this; return new Promise(function (resolve) { if (body.message && body.message.text && body.message.text.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig)) { return resolve(); } _this.accountInfo().then(function (user) { _this.context.log.incoming(body); handler(_this.context.bot, body, "viber", function (messages) { // force to array. var response = (messages || []).map(function (message) { return function () { return new Promise(function (resolve, reject) { var method = "send_message"; var sender = body.sender || body.user; message.receiver = sender.id; message.tracking_data = _this.context.bot.id; message.sender = { name: user.name, avatar: "https://icons.recime.io/" + _this.context.bot.id + ".png" }; message.type = message.type || "text"; if (message.type === 'typing') { var duration = parseInt(message.duration); setTimeout(function () { return resolve(); }, (duration === MaxDuration ? duration - 1 : duration) * 1000); } else { var url = "https://chatapi.viber.com/pa/" + method; var options_1 = { json: true, url: url, headers: { "X-Viber-Auth-Token": process.env.RECIME_VIBER_ACCESS_TOKEN || _this.context.bot.config.RECIME_VIBER_ACCESS_TOKEN }, body: message }; request(options_1, function (err, response, r) { _this.context.log.outgoing(options_1, r); if (err) { console.error(err); return reject(err); } else if (r.status !== 0) { 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({ succcess: true }); }); }); }); }); }; return Viber; }(channel_1.Channel)); exports.Viber = Viber;