UNPKG

8.88 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");
16// bot-framework { type: 'conversationUpdate',
17// id: '8QyCyDKfLGA',
18// timestamp: '2018-02-24T19:44:52.061453Z',
19// serviceUrl : body.ser: 'https://webchat.botframework.com/',
20// channelId: 'webchat',
21// from: { id: 'a701a0b1b43b4dd180f9568be35d8877' },
22// conversation: { id: 'a701a0b1b43b4dd180f9568be35d8877' },
23// recipient:
24// { id: 'CoveredCalifornia@CDjy6ApT8qk',
25// name: 'CoveredCalifornia' },
26// membersAdded: [ { id: 'ARe2TU6wUf5', name: 'You' } ] }
27var BOT_FRAMEWORK = 'bot-framework';
28var FACEBOOK = 'facebook';
29var BotFramework = /** @class */ (function (_super) {
30 __extends(BotFramework, _super);
31 function BotFramework(bot) {
32 return _super.call(this, { bot: bot, log: new logger_1.BotFrameworkLogger(bot) }) || this;
33 }
34 BotFramework.prototype.execute = function (body, parameters, handler) {
35 var _this = this;
36 return this.token().then(function (auth) {
37 _this.context.log.incoming(body);
38 return new Promise(function (resolve, reject) {
39 var types = ['conversationUpdate', 'message', 'contactRelationUpdate'];
40 if (types.indexOf(body.type) >= 0) {
41 switch (body.type) {
42 case 'conversationUpdate': {
43 if ((body.membersAdded && body.membersAdded.length > 0)) {
44 if (body.membersAdded.find(function (member) {
45 return member.id !== body.recipient.id;
46 })) {
47 return resolve();
48 }
49 }
50 break;
51 }
52 case 'contactRelationUpdate': {
53 if (!(body.channelId === 'skype' && body.action === 'add')) {
54 return resolve();
55 }
56 break;
57 }
58 default: {
59 // todo:
60 }
61 }
62 var supportedChannelData_1 = ['facebook'];
63 var input_1 = (function () {
64 if (supportedChannelData_1.find(function (channel) {
65 return channel === body.channelId;
66 })) {
67 return {
68 channel: body.channelId,
69 data: body.channelData,
70 isNative: false,
71 };
72 }
73 else {
74 return {
75 channel: BOT_FRAMEWORK,
76 data: body,
77 isNative: true
78 };
79 }
80 ;
81 })();
82 handler(_this.context.bot, input_1.data, input_1.channel, function (messages) {
83 messages = messages.length ? messages : [messages];
84 var response = messages.map(function (message) { return function () {
85 var payload = {
86 id: body.id,
87 serviceUrl: body.serviceUrl,
88 conversation: body.conversation,
89 from: body.recipient,
90 recipient: body.from,
91 message: input_1.isNative ? message : null,
92 channelData: !input_1.isNative ? message : null
93 };
94 return _this.sendMessage(auth.access_token, payload);
95 }; });
96 return _this.sendTyping(auth.access_token, {
97 id: body.id,
98 serviceUrl: body.serviceUrl,
99 conversation: body.conversation,
100 from: body.recipient,
101 recipient: body.from
102 }).then(function () {
103 return response
104 .reduce(function (a, b) {
105 return a.then(function () {
106 return b();
107 });
108 }, Promise.resolve())
109 .then(function () {
110 resolve();
111 });
112 });
113 }, function (err) {
114 console.error(err);
115 return reject(err);
116 });
117 return;
118 }
119 });
120 }).catch(function (err) {
121 console.error(err);
122 return Promise.reject(err);
123 });
124 };
125 BotFramework.prototype.sendTyping = function (accessToken, payload) {
126 var _this = this;
127 return new Promise(function (resolve, reject) {
128 var options = {
129 uri: payload.serviceUrl + "/v3/conversations/" + payload.conversation.id + "/activities/" + payload.id,
130 json: {
131 type: 'typing',
132 from: payload.from,
133 recipient: payload.recipient,
134 },
135 headers: {
136 authorization: "Bearer " + accessToken
137 },
138 method: 'POST'
139 };
140 request(options, function (err, response, body) {
141 _this.context.log.outgoing(options.json, body);
142 if (err) {
143 return reject(err);
144 }
145 resolve(body);
146 });
147 });
148 };
149 BotFramework.prototype.sendMessage = function (accessToken, payload) {
150 var _this = this;
151 return new Promise(function (resolve, reject) {
152 var options = {
153 uri: payload.serviceUrl + "/v3/conversations/" + payload.conversation.id + "/activities/" + payload.id,
154 json: {
155 type: 'message',
156 from: payload.from,
157 recipient: payload.recipient,
158 text: payload.message && payload.message.text,
159 attachmentLayout: payload.message && payload.message.attachmentLayout,
160 attachments: payload.message && payload.message.attachments,
161 channelData: payload.channelData
162 },
163 headers: {
164 authorization: "Bearer " + accessToken
165 },
166 method: 'POST'
167 };
168 request(options, function (err, response, body) {
169 _this.context.log.outgoing(options.json, body);
170 if (err) {
171 return reject(err);
172 }
173 resolve(body);
174 });
175 });
176 };
177 BotFramework.prototype.token = function () {
178 var _this = this;
179 return new Promise(function (resolve, reject) {
180 var options = {
181 uri: 'https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token',
182 formData: {
183 client_id: process.env.RECIME_MICROSOFT_APP_ID || _this.context.bot.config.RECIME_MICROSOFT_APP_ID,
184 client_secret: process.env.RECIME_MICROSOFT_APP_PASSWORD || _this.context.bot.config.RECIME_MICROSOFT_APP_PASSWORD,
185 grant_type: 'client_credentials',
186 scope: 'https://api.botframework.com/.default'
187 },
188 json: true,
189 method: 'POST'
190 };
191 request(options, function (err, response, body) {
192 if (err) {
193 return reject(err);
194 }
195 resolve(body);
196 });
197 });
198 };
199 return BotFramework;
200}(channel_1.Channel));
201exports.BotFramework = BotFramework;