@botpress/webchat 0.2.4 → 0.2.5

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.
@@ -5,10 +5,9 @@ import { BotInfo, uuid } from '../typings';
5
5
  import BpSocket from './socket';
6
6
  export default class WebchatApi {
7
7
  private socket;
8
- private axios;
9
- private axiosConfig;
10
8
  constructor(socket: BpSocket);
11
9
  fetchBotInfo(mediaFileServiceUrl: string): Promise<BotInfo | undefined>;
10
+ listCurrentConversationMessages(limit?: number): Promise<Message[]>;
12
11
  fetchConversations(): Promise<RecentConversation[]>;
13
12
  fetchConversation(conversationId: uuid): Promise<{
14
13
  messages: Message[];
package/dist/core/api.js CHANGED
@@ -29,6 +29,11 @@ class WebchatApi {
29
29
  }
30
30
  });
31
31
  }
32
+ listCurrentConversationMessages(limit) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return this.socket.socket.listMessages(limit);
35
+ });
36
+ }
32
37
  fetchConversations() {
33
38
  return __awaiter(this, void 0, void 0, function* () {
34
39
  try {
@@ -37,7 +42,7 @@ class WebchatApi {
37
42
  for (const conversation of conversations) {
38
43
  const limit = 1;
39
44
  yield this.socket.socket.switchConversation(conversation.id);
40
- const lastMessages = yield this.socket.socket.listMessages(limit);
45
+ const lastMessages = yield this.listCurrentConversationMessages(limit);
41
46
  if (lastMessages.length >= limit) {
42
47
  conversation.lastMessage = lastMessages[0];
43
48
  }
@@ -125,7 +130,6 @@ class WebchatApi {
125
130
  data.append('file', file);
126
131
  data.append('conversationId', conversationId);
127
132
  data.append('payload', payload);
128
- return this.axios.post('/messages/files', data, this.axiosConfig);
129
133
  });
130
134
  }
131
135
  // TODO: Fix this
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ interface State {
7
7
  store: RootStore;
8
8
  }
9
9
  interface Props {
10
- config: Config;
10
+ config?: Config;
11
11
  fullscreen?: boolean;
12
12
  }
13
13
  export declare class ExposedWebChat extends React.Component<Props, State> {
package/dist/index.js CHANGED
@@ -30,12 +30,7 @@ exports.Fullscreen = Fullscreen;
30
30
  class ExposedWebChat extends react_1.default.Component {
31
31
  constructor(props) {
32
32
  super(props);
33
- const { clientId, encryptionKey, useSessionStorage } = props.config;
34
- window.BP_STORAGE = new storage_1.default({
35
- clientId,
36
- encryptionKey,
37
- useSessionStorage
38
- });
33
+ window.BP_STORAGE = new storage_1.default(props.config);
39
34
  this.state = {
40
35
  store: new store_1.RootStore({ fullscreen: props.fullscreen }, props.config)
41
36
  };
@@ -44,9 +39,8 @@ class ExposedWebChat extends react_1.default.Component {
44
39
  const store = this.state.store;
45
40
  const { botUILanguage: locale } = store;
46
41
  return (react_1.default.createElement(mobx_react_1.Provider, { store: store },
47
- react_1.default.createElement(react_intl_1.IntlProvider, { locale: locale, messages: translations_1.translations[locale], defaultLocale: translations_1.defaultLocale },
48
- react_1.default.createElement(react_1.default.Fragment, null,
49
- react_1.default.createElement(main_1.default, Object.assign({ store: store }, this.props))))));
42
+ react_1.default.createElement(react_intl_1.IntlProvider, { locale: locale, messages: translations_1.translations[locale || translations_1.defaultLocale], defaultLocale: translations_1.defaultLocale },
43
+ react_1.default.createElement(main_1.default, Object.assign({ store: store }, this.props)))));
50
44
  }
51
45
  }
52
46
  exports.ExposedWebChat = ExposedWebChat;
@@ -24,7 +24,7 @@ declare class RootStore {
24
24
  delayedMessages: QueuedMessage[];
25
25
  constructor(options: {
26
26
  fullscreen: boolean;
27
- }, config: Config);
27
+ }, config?: Config);
28
28
  setIntlProvider(provider: IntlShape): void;
29
29
  setSocket(socket: BpSocket): void;
30
30
  get isConversationStarted(): boolean;
@@ -37,7 +37,9 @@ class RootStore {
37
37
  this.delayedMessages = [];
38
38
  this.composer = new composer_1.default(this);
39
39
  this.view = new view_1.default(this, options.fullscreen);
40
- this.updateConfig(config);
40
+ if (config) {
41
+ this.updateConfig(config);
42
+ }
41
43
  this.botUILanguage = (0, translations_1.getUserLocale)();
42
44
  }
43
45
  setIntlProvider(provider) {
@@ -51,8 +53,8 @@ class RootStore {
51
53
  return !!((_a = this.currentConversation) === null || _a === void 0 ? void 0 : _a.messages.length);
52
54
  }
53
55
  get botName() {
54
- var _a;
55
- return this.config.botName || ((_a = this.botInfo) === null || _a === void 0 ? void 0 : _a.name) || 'Bot';
56
+ var _a, _b;
57
+ return ((_a = this.config) === null || _a === void 0 ? void 0 : _a.botName) || ((_b = this.botInfo) === null || _b === void 0 ? void 0 : _b.name) || 'Bot';
56
58
  }
57
59
  get hasBotInfoDescription() {
58
60
  var _a;
@@ -297,8 +299,20 @@ class RootStore {
297
299
  return;
298
300
  }
299
301
  let info = `Conversation Id: ${conversation.id}\nCreated on: ${formatDate(conversation.createdOn)}\nUser: ${conversation.userId}\n-----------------`;
300
- for (const message of (0, orderBy_1.default)(conversation.messages, 'sentOn', 'desc')) {
301
- info += `\n[${formatDate(message.sentOn)}] ${message.authorId ? 'User' : this.config.botId || 'Bot'}: ${message.payload.text}`;
302
+ const messages = yield this.api.listCurrentConversationMessages(500);
303
+ for (const message of (0, orderBy_1.default)(messages, 'sentOn', 'desc')) {
304
+ const payload = message.payload;
305
+ if (payload.type === 'session_reset') {
306
+ continue;
307
+ }
308
+ info += `\n[${formatDate(message.sentOn)}] ${message.authorId ? 'User' : this.config.botId || 'Bot'}: Event (${payload.type}): ${payload.text ||
309
+ payload.audio ||
310
+ payload.image ||
311
+ payload.video ||
312
+ payload.file ||
313
+ payload.message ||
314
+ payload.title ||
315
+ ''}`;
302
316
  }
303
317
  const blobFile = new Blob([info]);
304
318
  (0, utils_1.downloadFile)(`conversation-${conversation.id}`, blobFile);
@@ -1,11 +1,10 @@
1
1
  import { Config } from '../typings';
2
- export declare type StorageConfig = Pick<Config, 'clientId' | 'encryptionKey' | 'useSessionStorage'>;
3
2
  export declare class BPStorage {
4
- private _config;
3
+ private _config?;
5
4
  private _storage;
6
- constructor(config: StorageConfig);
7
- get config(): Partial<StorageConfig>;
8
- set config(config: Partial<StorageConfig>);
5
+ constructor(config?: Config);
6
+ get config(): Config | undefined;
7
+ set config(config: Config | undefined);
9
8
  private serialize;
10
9
  private deserialize;
11
10
  private getStorageKey;
@@ -11,7 +11,7 @@ const js_cookie_1 = __importDefault(require("js-cookie"));
11
11
  class BPStorage {
12
12
  constructor(config) {
13
13
  this.serialize = (value) => {
14
- var _a;
14
+ var _a, _b;
15
15
  if (value === null || value === undefined) {
16
16
  throw new Error('[Storage] Cannot store null or undefined values');
17
17
  }
@@ -23,47 +23,48 @@ class BPStorage {
23
23
  else {
24
24
  str = JSON.stringify(value);
25
25
  }
26
- if ((_a = this.config.encryptionKey) === null || _a === void 0 ? void 0 : _a.length) {
26
+ if ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.encryptionKey) === null || _b === void 0 ? void 0 : _b.length) {
27
27
  str = aes_1.default.encrypt(str, this.config.encryptionKey).toString();
28
28
  }
29
29
  return str;
30
30
  }
31
- catch (_b) {
31
+ catch (_c) {
32
32
  console.error('[Storage] Error parsing value', value);
33
33
  return '';
34
34
  }
35
35
  };
36
36
  this.deserialize = (strValue) => {
37
- var _a;
37
+ var _a, _b;
38
38
  if (strValue === null || strValue === undefined) {
39
39
  return undefined;
40
40
  }
41
41
  try {
42
- if ((_a = this.config.encryptionKey) === null || _a === void 0 ? void 0 : _a.length) {
42
+ if ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.encryptionKey) === null || _b === void 0 ? void 0 : _b.length) {
43
43
  strValue = aes_1.default.decrypt(strValue, this.config.encryptionKey).toString(enc_utf8_1.default);
44
44
  }
45
45
  return JSON.parse(strValue);
46
46
  }
47
- catch (_b) {
47
+ catch (_c) {
48
48
  return undefined;
49
49
  }
50
50
  };
51
51
  this.getStorageKey = (key) => {
52
- var _a, _b;
52
+ var _a, _b, _c;
53
53
  const rawKey = `bp-chat-${key}`;
54
54
  if ((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.encryptionKey) === null || _b === void 0 ? void 0 : _b.length) {
55
55
  return `${rawKey}-${(0, sha256_1.default)(`${this.config.clientId}-${this.config.encryptionKey}`).toString()}`;
56
56
  }
57
57
  else {
58
- return `${rawKey}-${this.config.clientId}`;
58
+ return `${rawKey}-${(_c = this.config) === null || _c === void 0 ? void 0 : _c.clientId}`;
59
59
  }
60
60
  };
61
61
  this.getDriver = () => {
62
+ var _a;
62
63
  if (this._storage) {
63
64
  return this._storage;
64
65
  }
65
66
  try {
66
- const storage = this.config.useSessionStorage === true && typeof sessionStorage !== 'undefined' ? sessionStorage : localStorage;
67
+ const storage = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.useSessionStorage) === true && typeof sessionStorage !== 'undefined' ? sessionStorage : localStorage;
67
68
  const tempKey = '__storage_test__';
68
69
  storage.setItem(tempKey, tempKey);
69
70
  storage.removeItem(tempKey);
@@ -73,15 +74,20 @@ class BPStorage {
73
74
  return (this._storage = 'cookie');
74
75
  }
75
76
  };
76
- this._config = config;
77
+ if (config) {
78
+ this._config = config;
79
+ }
77
80
  }
78
81
  get config() {
79
82
  return this._config;
80
83
  }
81
84
  set config(config) {
82
- this._config = Object.assign(Object.assign({}, this._config), config);
85
+ this._config = config;
83
86
  }
84
87
  set(key, value) {
88
+ if (!this.config) {
89
+ return;
90
+ }
85
91
  try {
86
92
  const driver = this.getDriver();
87
93
  driver !== 'cookie'
@@ -93,6 +99,9 @@ class BPStorage {
93
99
  }
94
100
  }
95
101
  get(key) {
102
+ if (!this.config) {
103
+ return;
104
+ }
96
105
  try {
97
106
  const driver = this.getDriver();
98
107
  return driver !== 'cookie'
@@ -104,6 +113,9 @@ class BPStorage {
104
113
  }
105
114
  }
106
115
  del(key) {
116
+ if (!this.config) {
117
+ return;
118
+ }
107
119
  try {
108
120
  const driver = this.getDriver();
109
121
  driver !== 'cookie' ? driver.removeItem(this.getStorageKey(key)) : js_cookie_1.default.remove(this.getStorageKey(key));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botpress/webchat",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "source": "src/index.tsx",