@botonic/core 0.30.2 → 0.31.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/hubtype-service.d.ts +10 -22
- package/lib/cjs/hubtype-service.js +15 -20
- package/lib/cjs/hubtype-service.js.map +1 -1
- package/lib/esm/hubtype-service.d.ts +10 -22
- package/lib/esm/hubtype-service.js +15 -20
- package/lib/esm/hubtype-service.js.map +1 -1
- package/package.json +1 -1
- package/src/hubtype-service.ts +49 -38
|
@@ -11,18 +11,18 @@ interface BotonicHeaders {
|
|
|
11
11
|
'X-BOTONIC-LAST-MESSAGE-ID': string;
|
|
12
12
|
'X-BOTONIC-LAST-MESSAGE-UPDATE-DATE': string;
|
|
13
13
|
}
|
|
14
|
-
interface ServerConfig {
|
|
14
|
+
export interface ServerConfig {
|
|
15
15
|
activityTimeout?: number;
|
|
16
16
|
pongTimeout?: number;
|
|
17
17
|
}
|
|
18
18
|
interface HubtypeServiceArgs {
|
|
19
19
|
appId: string;
|
|
20
20
|
user: SessionUser;
|
|
21
|
-
lastMessageId
|
|
22
|
-
lastMessageUpdateDate
|
|
21
|
+
lastMessageId?: string;
|
|
22
|
+
lastMessageUpdateDate?: string;
|
|
23
23
|
onEvent: any;
|
|
24
24
|
unsentInputs: () => UnsentInput[];
|
|
25
|
-
server
|
|
25
|
+
server?: ServerConfig;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Calls Hubtype APIs from Webchat
|
|
@@ -30,22 +30,15 @@ interface HubtypeServiceArgs {
|
|
|
30
30
|
export declare class HubtypeService {
|
|
31
31
|
appId: string;
|
|
32
32
|
user: SessionUser;
|
|
33
|
-
lastMessageId
|
|
34
|
-
lastMessageUpdateDate
|
|
35
|
-
onEvent: any;
|
|
33
|
+
lastMessageId?: string;
|
|
34
|
+
lastMessageUpdateDate?: string;
|
|
35
|
+
onEvent: (event: any) => void;
|
|
36
36
|
unsentInputs: () => UnsentInput[];
|
|
37
37
|
pusher: Pusher | null;
|
|
38
38
|
channel: Channel;
|
|
39
|
-
server
|
|
40
|
-
PUSHER_CONNECT_TIMEOUT_MS: number;
|
|
39
|
+
server?: ServerConfig;
|
|
41
40
|
constructor({ appId, user, lastMessageId, lastMessageUpdateDate, onEvent, unsentInputs, server, }: HubtypeServiceArgs);
|
|
42
|
-
|
|
43
|
-
* @returns {Promise<void>}
|
|
44
|
-
*/
|
|
45
|
-
init(user: any, lastMessageId: any, lastMessageUpdateDate: any): Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
* @returns {Promise<void>}
|
|
48
|
-
*/
|
|
41
|
+
init(user?: SessionUser, lastMessageId?: string, lastMessageUpdateDate?: string): Promise<void>;
|
|
49
42
|
_initPusher(): Promise<void>;
|
|
50
43
|
constructHeaders(): BotonicHeaders;
|
|
51
44
|
resolveServerConfig(): ServerConfig;
|
|
@@ -55,13 +48,8 @@ export declare class HubtypeService {
|
|
|
55
48
|
get pusherChannel(): string;
|
|
56
49
|
handleSentInput(message: any): void;
|
|
57
50
|
handleUnsentInput(message: any): void;
|
|
58
|
-
/**
|
|
59
|
-
* @return {Promise<void>}
|
|
60
|
-
*/
|
|
61
51
|
postMessage(user: SessionUser, message: any): Promise<void>;
|
|
62
|
-
static getWebchatVisibility(
|
|
63
|
-
appId: string;
|
|
64
|
-
}): Promise<AxiosResponse<any>>;
|
|
52
|
+
static getWebchatVisibility(appId: string): Promise<AxiosResponse<any>>;
|
|
65
53
|
destroyPusher(): void;
|
|
66
54
|
onConnectionRegained(): Promise<void>;
|
|
67
55
|
resendUnsentInputs(): Promise<void>;
|
|
@@ -7,6 +7,7 @@ const pusher_js_1 = tslib_1.__importDefault(require("pusher-js"));
|
|
|
7
7
|
const pusher_utils_1 = require("./pusher-utils");
|
|
8
8
|
const WEBCHAT_PUSHER_KEY = process.env.WEBCHAT_PUSHER_KEY || '434ca667c8e6cb3f641c'; // pragma: allowlist secret
|
|
9
9
|
const HUBTYPE_API_URL = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com';
|
|
10
|
+
const PUSHER_CONNECT_TIMEOUT_MS = 10000;
|
|
10
11
|
const ACTIVITY_TIMEOUT = 20 * 1000; // https://pusher.com/docs/channels/using_channels/connection#activitytimeout-integer-
|
|
11
12
|
const PONG_TIMEOUT = 5 * 1000; // https://pusher.com/docs/channels/using_channels/connection#pongtimeout-integer-
|
|
12
13
|
/**
|
|
@@ -14,7 +15,6 @@ const PONG_TIMEOUT = 5 * 1000; // https://pusher.com/docs/channels/using_channel
|
|
|
14
15
|
*/
|
|
15
16
|
class HubtypeService {
|
|
16
17
|
constructor({ appId, user, lastMessageId, lastMessageUpdateDate, onEvent, unsentInputs, server, }) {
|
|
17
|
-
this.PUSHER_CONNECT_TIMEOUT_MS = 10000;
|
|
18
18
|
this.appId = appId;
|
|
19
19
|
this.user = user || {};
|
|
20
20
|
this.lastMessageId = lastMessageId;
|
|
@@ -26,25 +26,21 @@ class HubtypeService {
|
|
|
26
26
|
// It's safe not awaiting Promise because:
|
|
27
27
|
// * it will never be called from AWS lambda
|
|
28
28
|
// * though init() is called again from postMesage, it does nothing if Pusher already created
|
|
29
|
-
// @ts-ignore
|
|
30
29
|
this.init();
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
|
-
/**
|
|
34
|
-
* @returns {Promise<void>}
|
|
35
|
-
*/
|
|
36
32
|
init(user, lastMessageId, lastMessageUpdateDate) {
|
|
37
|
-
if (user)
|
|
33
|
+
if (user) {
|
|
38
34
|
this.user = user;
|
|
39
|
-
|
|
35
|
+
}
|
|
36
|
+
if (lastMessageId) {
|
|
40
37
|
this.lastMessageId = lastMessageId;
|
|
41
|
-
|
|
38
|
+
}
|
|
39
|
+
if (lastMessageUpdateDate) {
|
|
42
40
|
this.lastMessageUpdateDate = lastMessageUpdateDate;
|
|
41
|
+
}
|
|
43
42
|
return this._initPusher();
|
|
44
43
|
}
|
|
45
|
-
/**
|
|
46
|
-
* @returns {Promise<void>}
|
|
47
|
-
*/
|
|
48
44
|
_initPusher() {
|
|
49
45
|
if (this.pusher)
|
|
50
46
|
return Promise.resolve();
|
|
@@ -63,7 +59,7 @@ class HubtypeService {
|
|
|
63
59
|
this.destroyPusher();
|
|
64
60
|
reject(msg);
|
|
65
61
|
};
|
|
66
|
-
const connectTimeout = setTimeout(() => cleanAndReject('Connection Timeout'),
|
|
62
|
+
const connectTimeout = setTimeout(() => cleanAndReject('Connection Timeout'), PUSHER_CONNECT_TIMEOUT_MS);
|
|
67
63
|
this.channel.bind('pusher:subscription_succeeded', () => {
|
|
68
64
|
// Once subscribed, we know that authentication has been done: https://pusher.com/docs/channels/server_api/authenticating-users
|
|
69
65
|
this.onConnectionRegained();
|
|
@@ -105,12 +101,15 @@ class HubtypeService {
|
|
|
105
101
|
}
|
|
106
102
|
constructHeaders() {
|
|
107
103
|
const headers = {};
|
|
108
|
-
if (this.user && this.user.id)
|
|
104
|
+
if (this.user && this.user.id) {
|
|
109
105
|
headers['X-BOTONIC-USER-ID'] = this.user.id;
|
|
110
|
-
|
|
106
|
+
}
|
|
107
|
+
if (this.lastMessageId) {
|
|
111
108
|
headers['X-BOTONIC-LAST-MESSAGE-ID'] = this.lastMessageId;
|
|
112
|
-
|
|
109
|
+
}
|
|
110
|
+
if (this.lastMessageUpdateDate) {
|
|
113
111
|
headers['X-BOTONIC-LAST-MESSAGE-UPDATE-DATE'] = this.lastMessageUpdateDate;
|
|
112
|
+
}
|
|
114
113
|
return headers;
|
|
115
114
|
}
|
|
116
115
|
resolveServerConfig() {
|
|
@@ -149,13 +148,9 @@ class HubtypeService {
|
|
|
149
148
|
message: { id: message.id, ack: 0, unsentInput: message },
|
|
150
149
|
});
|
|
151
150
|
}
|
|
152
|
-
/**
|
|
153
|
-
* @return {Promise<void>}
|
|
154
|
-
*/
|
|
155
151
|
postMessage(user, message) {
|
|
156
152
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
157
153
|
try {
|
|
158
|
-
// @ts-ignore
|
|
159
154
|
yield this.init(user);
|
|
160
155
|
yield axios_1.default.post(`${HUBTYPE_API_URL}/v1/provider_accounts/webhooks/webchat/${this.appId}/`, {
|
|
161
156
|
sender: this.user,
|
|
@@ -171,7 +166,7 @@ class HubtypeService {
|
|
|
171
166
|
return Promise.resolve();
|
|
172
167
|
});
|
|
173
168
|
}
|
|
174
|
-
static getWebchatVisibility(
|
|
169
|
+
static getWebchatVisibility(appId) {
|
|
175
170
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
176
171
|
return axios_1.default.get(`${HUBTYPE_API_URL}/v1/provider_accounts/${appId}/visibility/`);
|
|
177
172
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubtype-service.js","sourceRoot":"","sources":["../../src/hubtype-service.ts"],"names":[],"mappings":";;;;AAAA,0DAA4C;AAC5C,kEAAwD;AAIxD,iDAA+C;
|
|
1
|
+
{"version":3,"file":"hubtype-service.js","sourceRoot":"","sources":["../../src/hubtype-service.ts"],"names":[],"mappings":";;;;AAAA,0DAA4C;AAC5C,kEAAwD;AAIxD,iDAA+C;AA6B/C,MAAM,kBAAkB,GACtB,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,sBAAsB,CAAA,CAAC,2BAA2B;AAEtF,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,yBAAyB,CAAA;AAEhF,MAAM,yBAAyB,GAAG,KAAK,CAAA;AACvC,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CAAA,CAAC,sFAAsF;AACzH,MAAM,YAAY,GAAG,CAAC,GAAG,IAAI,CAAA,CAAC,kFAAkF;AAEhH;;GAEG;AACH,MAAa,cAAc;IAWzB,YAAY,EACV,KAAK,EACL,IAAI,EACJ,aAAa,EACb,qBAAqB,EACrB,OAAO,EACP,YAAY,EACZ,MAAM,GACa;QACnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,IA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AAC,aAAa,CAAC,CAAA;QAC3C,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;QACxB,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,EAAc,CAAA;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;IACpB,CAAC;IAEK,oBAAoB;;YACxB,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAA;QACjC,CAAC;KAAA;IAEK,kBAAkB;;YACtB,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,EAAE,EAAE;gBACzC,OAAO,CAAC,WAAW;oBACjB,CAAC,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAA;aAC3D;QACH,CAAC;KAAA;CACF;AAtOD,wCAsOC"}
|
|
@@ -11,18 +11,18 @@ interface BotonicHeaders {
|
|
|
11
11
|
'X-BOTONIC-LAST-MESSAGE-ID': string;
|
|
12
12
|
'X-BOTONIC-LAST-MESSAGE-UPDATE-DATE': string;
|
|
13
13
|
}
|
|
14
|
-
interface ServerConfig {
|
|
14
|
+
export interface ServerConfig {
|
|
15
15
|
activityTimeout?: number;
|
|
16
16
|
pongTimeout?: number;
|
|
17
17
|
}
|
|
18
18
|
interface HubtypeServiceArgs {
|
|
19
19
|
appId: string;
|
|
20
20
|
user: SessionUser;
|
|
21
|
-
lastMessageId
|
|
22
|
-
lastMessageUpdateDate
|
|
21
|
+
lastMessageId?: string;
|
|
22
|
+
lastMessageUpdateDate?: string;
|
|
23
23
|
onEvent: any;
|
|
24
24
|
unsentInputs: () => UnsentInput[];
|
|
25
|
-
server
|
|
25
|
+
server?: ServerConfig;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Calls Hubtype APIs from Webchat
|
|
@@ -30,22 +30,15 @@ interface HubtypeServiceArgs {
|
|
|
30
30
|
export declare class HubtypeService {
|
|
31
31
|
appId: string;
|
|
32
32
|
user: SessionUser;
|
|
33
|
-
lastMessageId
|
|
34
|
-
lastMessageUpdateDate
|
|
35
|
-
onEvent: any;
|
|
33
|
+
lastMessageId?: string;
|
|
34
|
+
lastMessageUpdateDate?: string;
|
|
35
|
+
onEvent: (event: any) => void;
|
|
36
36
|
unsentInputs: () => UnsentInput[];
|
|
37
37
|
pusher: Pusher | null;
|
|
38
38
|
channel: Channel;
|
|
39
|
-
server
|
|
40
|
-
PUSHER_CONNECT_TIMEOUT_MS: number;
|
|
39
|
+
server?: ServerConfig;
|
|
41
40
|
constructor({ appId, user, lastMessageId, lastMessageUpdateDate, onEvent, unsentInputs, server, }: HubtypeServiceArgs);
|
|
42
|
-
|
|
43
|
-
* @returns {Promise<void>}
|
|
44
|
-
*/
|
|
45
|
-
init(user: any, lastMessageId: any, lastMessageUpdateDate: any): Promise<void>;
|
|
46
|
-
/**
|
|
47
|
-
* @returns {Promise<void>}
|
|
48
|
-
*/
|
|
41
|
+
init(user?: SessionUser, lastMessageId?: string, lastMessageUpdateDate?: string): Promise<void>;
|
|
49
42
|
_initPusher(): Promise<void>;
|
|
50
43
|
constructHeaders(): BotonicHeaders;
|
|
51
44
|
resolveServerConfig(): ServerConfig;
|
|
@@ -55,13 +48,8 @@ export declare class HubtypeService {
|
|
|
55
48
|
get pusherChannel(): string;
|
|
56
49
|
handleSentInput(message: any): void;
|
|
57
50
|
handleUnsentInput(message: any): void;
|
|
58
|
-
/**
|
|
59
|
-
* @return {Promise<void>}
|
|
60
|
-
*/
|
|
61
51
|
postMessage(user: SessionUser, message: any): Promise<void>;
|
|
62
|
-
static getWebchatVisibility(
|
|
63
|
-
appId: string;
|
|
64
|
-
}): Promise<AxiosResponse<any>>;
|
|
52
|
+
static getWebchatVisibility(appId: string): Promise<AxiosResponse<any>>;
|
|
65
53
|
destroyPusher(): void;
|
|
66
54
|
onConnectionRegained(): Promise<void>;
|
|
67
55
|
resendUnsentInputs(): Promise<void>;
|
|
@@ -4,6 +4,7 @@ import Pusher from 'pusher-js';
|
|
|
4
4
|
import { decompressData } from './pusher-utils';
|
|
5
5
|
const WEBCHAT_PUSHER_KEY = process.env.WEBCHAT_PUSHER_KEY || '434ca667c8e6cb3f641c'; // pragma: allowlist secret
|
|
6
6
|
const HUBTYPE_API_URL = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com';
|
|
7
|
+
const PUSHER_CONNECT_TIMEOUT_MS = 10000;
|
|
7
8
|
const ACTIVITY_TIMEOUT = 20 * 1000; // https://pusher.com/docs/channels/using_channels/connection#activitytimeout-integer-
|
|
8
9
|
const PONG_TIMEOUT = 5 * 1000; // https://pusher.com/docs/channels/using_channels/connection#pongtimeout-integer-
|
|
9
10
|
/**
|
|
@@ -11,7 +12,6 @@ const PONG_TIMEOUT = 5 * 1000; // https://pusher.com/docs/channels/using_channel
|
|
|
11
12
|
*/
|
|
12
13
|
export class HubtypeService {
|
|
13
14
|
constructor({ appId, user, lastMessageId, lastMessageUpdateDate, onEvent, unsentInputs, server, }) {
|
|
14
|
-
this.PUSHER_CONNECT_TIMEOUT_MS = 10000;
|
|
15
15
|
this.appId = appId;
|
|
16
16
|
this.user = user || {};
|
|
17
17
|
this.lastMessageId = lastMessageId;
|
|
@@ -23,25 +23,21 @@ export class HubtypeService {
|
|
|
23
23
|
// It's safe not awaiting Promise because:
|
|
24
24
|
// * it will never be called from AWS lambda
|
|
25
25
|
// * though init() is called again from postMesage, it does nothing if Pusher already created
|
|
26
|
-
// @ts-ignore
|
|
27
26
|
this.init();
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
|
-
/**
|
|
31
|
-
* @returns {Promise<void>}
|
|
32
|
-
*/
|
|
33
29
|
init(user, lastMessageId, lastMessageUpdateDate) {
|
|
34
|
-
if (user)
|
|
30
|
+
if (user) {
|
|
35
31
|
this.user = user;
|
|
36
|
-
|
|
32
|
+
}
|
|
33
|
+
if (lastMessageId) {
|
|
37
34
|
this.lastMessageId = lastMessageId;
|
|
38
|
-
|
|
35
|
+
}
|
|
36
|
+
if (lastMessageUpdateDate) {
|
|
39
37
|
this.lastMessageUpdateDate = lastMessageUpdateDate;
|
|
38
|
+
}
|
|
40
39
|
return this._initPusher();
|
|
41
40
|
}
|
|
42
|
-
/**
|
|
43
|
-
* @returns {Promise<void>}
|
|
44
|
-
*/
|
|
45
41
|
_initPusher() {
|
|
46
42
|
if (this.pusher)
|
|
47
43
|
return Promise.resolve();
|
|
@@ -60,7 +56,7 @@ export class HubtypeService {
|
|
|
60
56
|
this.destroyPusher();
|
|
61
57
|
reject(msg);
|
|
62
58
|
};
|
|
63
|
-
const connectTimeout = setTimeout(() => cleanAndReject('Connection Timeout'),
|
|
59
|
+
const connectTimeout = setTimeout(() => cleanAndReject('Connection Timeout'), PUSHER_CONNECT_TIMEOUT_MS);
|
|
64
60
|
this.channel.bind('pusher:subscription_succeeded', () => {
|
|
65
61
|
// Once subscribed, we know that authentication has been done: https://pusher.com/docs/channels/server_api/authenticating-users
|
|
66
62
|
this.onConnectionRegained();
|
|
@@ -102,12 +98,15 @@ export class HubtypeService {
|
|
|
102
98
|
}
|
|
103
99
|
constructHeaders() {
|
|
104
100
|
const headers = {};
|
|
105
|
-
if (this.user && this.user.id)
|
|
101
|
+
if (this.user && this.user.id) {
|
|
106
102
|
headers['X-BOTONIC-USER-ID'] = this.user.id;
|
|
107
|
-
|
|
103
|
+
}
|
|
104
|
+
if (this.lastMessageId) {
|
|
108
105
|
headers['X-BOTONIC-LAST-MESSAGE-ID'] = this.lastMessageId;
|
|
109
|
-
|
|
106
|
+
}
|
|
107
|
+
if (this.lastMessageUpdateDate) {
|
|
110
108
|
headers['X-BOTONIC-LAST-MESSAGE-UPDATE-DATE'] = this.lastMessageUpdateDate;
|
|
109
|
+
}
|
|
111
110
|
return headers;
|
|
112
111
|
}
|
|
113
112
|
resolveServerConfig() {
|
|
@@ -146,13 +145,9 @@ export class HubtypeService {
|
|
|
146
145
|
message: { id: message.id, ack: 0, unsentInput: message },
|
|
147
146
|
});
|
|
148
147
|
}
|
|
149
|
-
/**
|
|
150
|
-
* @return {Promise<void>}
|
|
151
|
-
*/
|
|
152
148
|
postMessage(user, message) {
|
|
153
149
|
return __awaiter(this, void 0, void 0, function* () {
|
|
154
150
|
try {
|
|
155
|
-
// @ts-ignore
|
|
156
151
|
yield this.init(user);
|
|
157
152
|
yield axios.post(`${HUBTYPE_API_URL}/v1/provider_accounts/webhooks/webchat/${this.appId}/`, {
|
|
158
153
|
sender: this.user,
|
|
@@ -168,7 +163,7 @@ export class HubtypeService {
|
|
|
168
163
|
return Promise.resolve();
|
|
169
164
|
});
|
|
170
165
|
}
|
|
171
|
-
static getWebchatVisibility(
|
|
166
|
+
static getWebchatVisibility(appId) {
|
|
172
167
|
return __awaiter(this, void 0, void 0, function* () {
|
|
173
168
|
return axios.get(`${HUBTYPE_API_URL}/v1/provider_accounts/${appId}/visibility/`);
|
|
174
169
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hubtype-service.js","sourceRoot":"","sources":["../../src/hubtype-service.ts"],"names":[],"mappings":";AAAA,OAAO,KAAwB,MAAM,OAAO,CAAA;AAC5C,OAAO,MAAgC,MAAM,WAAW,CAAA;AAIxD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"hubtype-service.js","sourceRoot":"","sources":["../../src/hubtype-service.ts"],"names":[],"mappings":";AAAA,OAAO,KAAwB,MAAM,OAAO,CAAA;AAC5C,OAAO,MAAgC,MAAM,WAAW,CAAA;AAIxD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AA6B/C,MAAM,kBAAkB,GACtB,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,sBAAsB,CAAA,CAAC,2BAA2B;AAEtF,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,yBAAyB,CAAA;AAEhF,MAAM,yBAAyB,GAAG,KAAK,CAAA;AACvC,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CAA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,MAAM,CAAO,oBAAoB,CAC/B,KAAa;;YAEb,OAAO,KAAK,CAAC,GAAG,CACd,GAAG,eAAe,yBAAyB,KAAK,cAAc,CAC/D,CAAA;QACH,CAAC;KAAA;IAED,aAAa;QACX,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAM;QACxB,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;QACxB,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;QAC3C,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;QACxB,IAAI,CAAC,MAAM,CAAC,QAAQ,GAAG,EAAc,CAAA;QACrC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;IACpB,CAAC;IAEK,oBAAoB;;YACxB,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAA;QACjC,CAAC;KAAA;IAEK,kBAAkB;;YACtB,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,YAAY,EAAE,EAAE;gBACzC,OAAO,CAAC,WAAW;oBACjB,CAAC,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAA;aAC3D;QACH,CAAC;KAAA;CACF"}
|
package/package.json
CHANGED
package/src/hubtype-service.ts
CHANGED
|
@@ -10,23 +10,26 @@ interface UnsentInput {
|
|
|
10
10
|
ack: number
|
|
11
11
|
unsentInput: Input
|
|
12
12
|
}
|
|
13
|
+
|
|
13
14
|
interface BotonicHeaders {
|
|
14
15
|
'X-BOTONIC-USER-ID': string
|
|
15
16
|
'X-BOTONIC-LAST-MESSAGE-ID': string
|
|
16
17
|
'X-BOTONIC-LAST-MESSAGE-UPDATE-DATE': string
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
export interface ServerConfig {
|
|
19
21
|
activityTimeout?: number
|
|
20
22
|
pongTimeout?: number
|
|
21
23
|
}
|
|
24
|
+
|
|
22
25
|
interface HubtypeServiceArgs {
|
|
23
26
|
appId: string
|
|
24
27
|
user: SessionUser
|
|
25
|
-
lastMessageId
|
|
26
|
-
lastMessageUpdateDate
|
|
28
|
+
lastMessageId?: string
|
|
29
|
+
lastMessageUpdateDate?: string
|
|
27
30
|
onEvent: any
|
|
28
31
|
unsentInputs: () => UnsentInput[]
|
|
29
|
-
server
|
|
32
|
+
server?: ServerConfig
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
const WEBCHAT_PUSHER_KEY =
|
|
@@ -34,6 +37,7 @@ const WEBCHAT_PUSHER_KEY =
|
|
|
34
37
|
|
|
35
38
|
const HUBTYPE_API_URL = process.env.HUBTYPE_API_URL || 'https://api.hubtype.com'
|
|
36
39
|
|
|
40
|
+
const PUSHER_CONNECT_TIMEOUT_MS = 10000
|
|
37
41
|
const ACTIVITY_TIMEOUT = 20 * 1000 // https://pusher.com/docs/channels/using_channels/connection#activitytimeout-integer-
|
|
38
42
|
const PONG_TIMEOUT = 5 * 1000 // https://pusher.com/docs/channels/using_channels/connection#pongtimeout-integer-
|
|
39
43
|
|
|
@@ -41,16 +45,16 @@ const PONG_TIMEOUT = 5 * 1000 // https://pusher.com/docs/channels/using_channels
|
|
|
41
45
|
* Calls Hubtype APIs from Webchat
|
|
42
46
|
*/
|
|
43
47
|
export class HubtypeService {
|
|
44
|
-
appId: string
|
|
45
|
-
user: SessionUser
|
|
46
|
-
lastMessageId
|
|
47
|
-
lastMessageUpdateDate
|
|
48
|
-
onEvent: any
|
|
49
|
-
unsentInputs: () => UnsentInput[]
|
|
50
|
-
pusher: Pusher | null
|
|
51
|
-
channel: Channel
|
|
52
|
-
server
|
|
53
|
-
|
|
48
|
+
public appId: string
|
|
49
|
+
public user: SessionUser
|
|
50
|
+
public lastMessageId?: string
|
|
51
|
+
public lastMessageUpdateDate?: string
|
|
52
|
+
public onEvent: (event: any) => void
|
|
53
|
+
public unsentInputs: () => UnsentInput[]
|
|
54
|
+
public pusher: Pusher | null
|
|
55
|
+
public channel: Channel
|
|
56
|
+
public server?: ServerConfig
|
|
57
|
+
|
|
54
58
|
constructor({
|
|
55
59
|
appId,
|
|
56
60
|
user,
|
|
@@ -67,29 +71,35 @@ export class HubtypeService {
|
|
|
67
71
|
this.onEvent = onEvent
|
|
68
72
|
this.unsentInputs = unsentInputs
|
|
69
73
|
this.server = server
|
|
74
|
+
|
|
70
75
|
if (this.user.id && (lastMessageId || lastMessageUpdateDate)) {
|
|
71
76
|
// It's safe not awaiting Promise because:
|
|
72
77
|
// * it will never be called from AWS lambda
|
|
73
78
|
// * though init() is called again from postMesage, it does nothing if Pusher already created
|
|
74
|
-
// @ts-ignore
|
|
75
79
|
this.init()
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
83
|
+
init(
|
|
84
|
+
user?: SessionUser,
|
|
85
|
+
lastMessageId?: string,
|
|
86
|
+
lastMessageUpdateDate?: string
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
if (user) {
|
|
89
|
+
this.user = user
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (lastMessageId) {
|
|
93
|
+
this.lastMessageId = lastMessageId
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (lastMessageUpdateDate) {
|
|
86
97
|
this.lastMessageUpdateDate = lastMessageUpdateDate
|
|
98
|
+
}
|
|
99
|
+
|
|
87
100
|
return this._initPusher()
|
|
88
101
|
}
|
|
89
102
|
|
|
90
|
-
/**
|
|
91
|
-
* @returns {Promise<void>}
|
|
92
|
-
*/
|
|
93
103
|
_initPusher(): Promise<void> {
|
|
94
104
|
if (this.pusher) return Promise.resolve()
|
|
95
105
|
if (!this.user.id || !this.appId) {
|
|
@@ -115,7 +125,7 @@ export class HubtypeService {
|
|
|
115
125
|
}
|
|
116
126
|
const connectTimeout = setTimeout(
|
|
117
127
|
() => cleanAndReject('Connection Timeout'),
|
|
118
|
-
|
|
128
|
+
PUSHER_CONNECT_TIMEOUT_MS
|
|
119
129
|
)
|
|
120
130
|
this.channel.bind('pusher:subscription_succeeded', () => {
|
|
121
131
|
// Once subscribed, we know that authentication has been done: https://pusher.com/docs/channels/server_api/authenticating-users
|
|
@@ -157,11 +167,18 @@ export class HubtypeService {
|
|
|
157
167
|
|
|
158
168
|
constructHeaders(): BotonicHeaders {
|
|
159
169
|
const headers = {}
|
|
160
|
-
if (this.user && this.user.id)
|
|
161
|
-
|
|
170
|
+
if (this.user && this.user.id) {
|
|
171
|
+
headers['X-BOTONIC-USER-ID'] = this.user.id
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.lastMessageId) {
|
|
162
175
|
headers['X-BOTONIC-LAST-MESSAGE-ID'] = this.lastMessageId
|
|
163
|
-
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.lastMessageUpdateDate) {
|
|
164
179
|
headers['X-BOTONIC-LAST-MESSAGE-UPDATE-DATE'] = this.lastMessageUpdateDate
|
|
180
|
+
}
|
|
181
|
+
|
|
165
182
|
return headers as BotonicHeaders
|
|
166
183
|
}
|
|
167
184
|
|
|
@@ -210,12 +227,8 @@ export class HubtypeService {
|
|
|
210
227
|
})
|
|
211
228
|
}
|
|
212
229
|
|
|
213
|
-
|
|
214
|
-
* @return {Promise<void>}
|
|
215
|
-
*/
|
|
216
|
-
async postMessage(user: SessionUser, message: any): Promise<void> {
|
|
230
|
+
async postMessage(user: SessionUser, message: any) {
|
|
217
231
|
try {
|
|
218
|
-
// @ts-ignore
|
|
219
232
|
await this.init(user)
|
|
220
233
|
await axios.post(
|
|
221
234
|
`${HUBTYPE_API_URL}/v1/provider_accounts/webhooks/webchat/${this.appId}/`,
|
|
@@ -234,11 +247,9 @@ export class HubtypeService {
|
|
|
234
247
|
return Promise.resolve()
|
|
235
248
|
}
|
|
236
249
|
|
|
237
|
-
static async getWebchatVisibility(
|
|
238
|
-
appId,
|
|
239
|
-
}: {
|
|
250
|
+
static async getWebchatVisibility(
|
|
240
251
|
appId: string
|
|
241
|
-
|
|
252
|
+
): Promise<AxiosResponse<any>> {
|
|
242
253
|
return axios.get(
|
|
243
254
|
`${HUBTYPE_API_URL}/v1/provider_accounts/${appId}/visibility/`
|
|
244
255
|
)
|