1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.actionssdk = void 0;
|
19 | const assistant_1 = require("../../assistant");
|
20 | const conversation_1 = require("./conversation");
|
21 | const conv_1 = require("./conv");
|
22 | const google_auth_library_1 = require("google-auth-library");
|
23 | const common = require("../../common");
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const actionssdk = (options = {}) => assistant_1.attach({
|
44 | _handlers: {
|
45 | intents: {},
|
46 | catcher: (conv, e) => {
|
47 | throw e;
|
48 | },
|
49 | },
|
50 | _middlewares: [],
|
51 | intent(intents, handler) {
|
52 | for (const intent of common.toArray(intents)) {
|
53 | this._handlers.intents[intent] = handler;
|
54 | }
|
55 | return this;
|
56 | },
|
57 | catch(catcher) {
|
58 | this._handlers.catcher = catcher;
|
59 | return this;
|
60 | },
|
61 | fallback(handler) {
|
62 | this._handlers.fallback = handler;
|
63 | return this;
|
64 | },
|
65 | middleware(middleware) {
|
66 | this._middlewares.push(middleware);
|
67 | return this;
|
68 | },
|
69 | init: options.init,
|
70 | verification: options.verification,
|
71 | _client: options.verification || options.clientId
|
72 | ? new google_auth_library_1.OAuth2Client(options.clientId)
|
73 | : undefined,
|
74 | auth: options.clientId
|
75 | ? {
|
76 | client: {
|
77 | id: options.clientId,
|
78 | },
|
79 | }
|
80 | : undefined,
|
81 | ordersv3: options.ordersv3 || false,
|
82 | async handler(body, headers, metadata = {}) {
|
83 | const { debug, init, verification, ordersv3 } = this;
|
84 | if (verification) {
|
85 | const { project, status = 403, error = (e) => e, } = typeof verification === 'string'
|
86 | ? { project: verification }
|
87 | : verification;
|
88 | const token = headers['authorization'];
|
89 | try {
|
90 | await this._client.verifyIdToken({
|
91 | idToken: token,
|
92 | audience: project,
|
93 | });
|
94 | }
|
95 | catch (e) {
|
96 | return {
|
97 | status,
|
98 | body: {
|
99 | error: typeof error === 'string'
|
100 | ? error
|
101 | : error(`ID token verification failed: ${e.stack || e.message || e}`),
|
102 | },
|
103 | };
|
104 | }
|
105 | }
|
106 | let conv = new conv_1.ActionsSdkConversation({
|
107 | body,
|
108 | headers,
|
109 | init: init && init(),
|
110 | debug,
|
111 | ordersv3,
|
112 | });
|
113 | if (conv.user.profile.token) {
|
114 | await conv.user._verifyProfile(this._client, this.auth.client.id);
|
115 | }
|
116 | for (const middleware of this._middlewares) {
|
117 | const result = middleware(conv, metadata);
|
118 | conv = (result instanceof conv_1.ActionsSdkConversation
|
119 | ? result
|
120 | : (await result) || conv);
|
121 | }
|
122 | const log = debug ? common.info : common.debug;
|
123 | log('Conversation', common.stringify(conv, 'request', 'headers', 'body'));
|
124 | const { intent } = conv;
|
125 | const traversed = {};
|
126 | let handler = intent;
|
127 | while (typeof handler !== 'function') {
|
128 | if (typeof handler === 'undefined') {
|
129 | if (!this._handlers.fallback) {
|
130 | throw new Error(`Actions SDK IntentHandler not found for intent: ${intent}`);
|
131 | }
|
132 | handler = this._handlers.fallback;
|
133 | continue;
|
134 | }
|
135 | if (traversed[handler]) {
|
136 | throw new Error(`Circular intent map detected: "${handler}" traversed twice`);
|
137 | }
|
138 | traversed[handler] = true;
|
139 | handler = this._handlers.intents[handler];
|
140 | }
|
141 | try {
|
142 | try {
|
143 | await handler(conv, conv.input.raw, conv.arguments.parsed.list[0], conv.arguments.status.list[0]);
|
144 | }
|
145 | catch (e) {
|
146 | await this._handlers.catcher(conv, e);
|
147 | }
|
148 | }
|
149 | catch (e) {
|
150 | if (e instanceof conversation_1.UnauthorizedError) {
|
151 | return {
|
152 | status: 401,
|
153 | headers: {},
|
154 | body: {},
|
155 | };
|
156 | }
|
157 | throw e;
|
158 | }
|
159 | return {
|
160 | status: 200,
|
161 | headers: {},
|
162 | body: conv.serialize(),
|
163 | };
|
164 | },
|
165 | }, options);
|
166 | exports.actionssdk = actionssdk;
|
167 |
|
\ | No newline at end of file |