UNPKG

5.43 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 request = require("request");
14var channel_1 = require("./channel");
15var logger_1 = require("./logger");
16var MaxDuration = 15;
17var Viber = /** @class */ (function (_super) {
18 __extends(Viber, _super);
19 function Viber(bot) {
20 return _super.call(this, { bot: bot, log: new logger_1.ViberLogger(bot) }) || this;
21 }
22 Viber.prototype.accountInfo = function () {
23 var _this = this;
24 return new Promise(function (resolve, reject) {
25 var url = "https://chatapi.viber.com/pa/get_account_info";
26 var options = {
27 json: true,
28 url: url,
29 headers: {
30 "X-Viber-Auth-Token": process.env.RECIME_VIBER_ACCESS_TOKEN || _this.context.bot.config.RECIME_VIBER_ACCESS_TOKEN
31 },
32 body: {}
33 };
34 request(options, function (err, response, body) {
35 if (body && body.length > 0) {
36 if (body[0].status !== 'ok') {
37 return reject(body);
38 }
39 }
40 resolve(body);
41 });
42 });
43 };
44 Viber.prototype.execute = function (body, parameters, handler) {
45 var _this = this;
46 return new Promise(function (resolve) {
47 if (body.message
48 && body.message.text
49 && body.message.text.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig)) {
50 return resolve();
51 }
52 var sender = (body.sender || body.user).id;
53 handler(_this.context.bot, body, "viber", function (messages) {
54 _this.context.log.incoming(body);
55 _this.send({
56 messages: messages,
57 sender: sender
58 })
59 .then(resolve);
60 });
61 });
62 };
63 Viber.prototype.send = function (body) {
64 var _this = this;
65 return new Promise(function (resolve) {
66 _this.accountInfo().then(function (user) {
67 var response = (body.messages || []).map(function (message) { return function () {
68 return new Promise(function (resolve) {
69 var method = "send_message";
70 message.receiver = body.sender;
71 message.tracking_data = _this.context.bot.id;
72 message.sender = {
73 name: user.name,
74 avatar: "https://icons.recime.io/" + _this.context.bot.id + ".png"
75 };
76 message.type = message.type || "text";
77 if (message.type === 'typing') {
78 var duration = parseInt(message.duration);
79 setTimeout(function () {
80 return resolve();
81 }, (duration === MaxDuration ? duration - 1 : duration) * 1000);
82 }
83 else {
84 var url = "https://chatapi.viber.com/pa/" + method;
85 var options_1 = {
86 json: true,
87 url: url,
88 headers: {
89 "X-Viber-Auth-Token": process.env.RECIME_VIBER_ACCESS_TOKEN || _this.context.bot.config.RECIME_VIBER_ACCESS_TOKEN
90 },
91 body: message
92 };
93 request(options_1, function (err, response, r) {
94 _this.context.log.outgoing(options_1, r);
95 if (err) {
96 console.error(err);
97 resolve();
98 }
99 else if (r.status !== 0) {
100 console.error(r);
101 resolve();
102 }
103 return resolve();
104 });
105 }
106 });
107 }; });
108 return response
109 .reduce(function (a, b) {
110 return a.then(function (messages) {
111 return b().then(function (message) {
112 return;
113 });
114 });
115 }, Promise.resolve())
116 .then(function () {
117 resolve({
118 succcess: true
119 });
120 });
121 });
122 });
123 };
124 return Viber;
125}(channel_1.Channel));
126exports.Viber = Viber;