Skip to content

Commit 11e7c60

Browse files
committed
docs: add context object guide to js guide
1 parent 452e1ec commit 11e7c60

File tree

2 files changed

+104
-56
lines changed

2 files changed

+104
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
**/dist
33
**/.vite
44
**/deps
5+
.idea

js/README.md

Lines changed: 103 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The **Sendbird AI Agent Messenger** allows seamless integration of chatbot featu
1515
- [Advanced Features](#advanced-features)
1616
- [Display messenger without launcher button](#display-messenger-without-launcher-button)
1717
- [Deauthenticate and clear session](#deauthenticate-and-clear-session)
18+
- [Passing context object to Agent](#passing-context-object-to-agent)
1819

1920
## Prerequisites
2021

@@ -60,18 +61,18 @@ Now that you have installed and initialized the AI Agent SDK, follow the steps b
6061

6162
For proper user session management, you can update the session information using the following methods.
6263

63-
```javascript
64-
// Update entire session configuration
65-
messenger.updateUserSession({
66-
userId: 'new_user_id',
67-
authToken: 'new_auth_token',
68-
// this callback should handle session token refresh:
69-
onSessionTokenRequest: async () => {
70-
const response = await fetch('new-token-endpoint');
71-
return response.token;
72-
}
73-
});
74-
```
64+
```javascript
65+
// Update entire session configuration
66+
messenger.updateUserSession({
67+
userId: 'new_user_id',
68+
authToken: 'new_auth_token',
69+
// this callback should handle session token refresh:
70+
onSessionTokenRequest: async () => {
71+
const response = await fetch('new-token-endpoint');
72+
return response.token;
73+
}
74+
});
75+
```
7576

7677
### Launch the messenger
7778

@@ -83,37 +84,37 @@ To launch and display the messenger, implement the code below:
8384

8485
>__Note__: Replace `YOUR_APP_ID` AND `YOUR_AI_AGENT_ID` with your Application ID and AI agent ID which you can obtain from the Sendbird Dashboard. To learn how do to so, refer to the [prerequisites](#prerequisites) section.
8586
86-
```javascript
87-
const messenger = await loadMessenger();
88-
messenger.initialize({
89-
appId: 'YOUR_APP_ID',
90-
aiAgentId: 'YOUR_AI_AGENT_ID',
91-
});
92-
```
87+
```javascript
88+
const messenger = await loadMessenger();
89+
messenger.initialize({
90+
appId: 'YOUR_APP_ID',
91+
aiAgentId: 'YOUR_AI_AGENT_ID',
92+
});
93+
```
9394

9495
The messenger view can be programmatically controlled using the `open()` and `close()` methods:
9596

96-
```javascript
97-
// Open the messenger view automatically after initialized
98-
messenger.initialize({ appId, aiAgentId });
99-
messenger.onLoad(() => {
100-
messenger.open();
101-
});
97+
```javascript
98+
// Open the messenger view automatically after initialized
99+
messenger.initialize({ appId, aiAgentId });
100+
messenger.onLoad(() => {
101+
messenger.open();
102+
});
102103

103-
// Close the messenger view by clicking a button
104-
<button onClick={() => messenger.close()}>Close</button>
105-
```
104+
// Close the messenger view by clicking a button
105+
<button onClick={() => messenger.close()}>Close</button>
106+
```
106107

107108
To update the configurations:
108109

109-
```javascript
110-
// Update configuration
111-
messenger.updateConfig({
112-
appId: 'NEW_APP_ID',
113-
aiAgentId: 'NEW_BOT_ID',
114-
// ... other config options
115-
});
116-
```
110+
```javascript
111+
// Update configuration
112+
messenger.updateConfig({
113+
appId: 'NEW_APP_ID',
114+
aiAgentId: 'NEW_BOT_ID',
115+
// ... other config options
116+
});
117+
```
117118

118119
---
119120

@@ -125,27 +126,73 @@ The following are available advanced features.
125126

126127
![Image](https://github.com/user-attachments/assets/348ccad1-ec9a-4851-9324-084eaf569e34)
127128

128-
```javascript
129-
const messenger = await loadMessenger({
130-
// Use Conversation component to display only the messenger without the launcher
131-
customMainComponent: ({ messenger, react }) => {
132-
return (props) => {
133-
return react.createElement(messenger.AgentProviderContainer, props, [
134-
react.createElement(messenger.Conversation),
135-
]);
136-
};
137-
}
138-
});
139-
messenger.initialize({
140-
appId: 'APP_ID',
141-
aiAgentId: 'AI_AGENT_ID',
142-
});
143-
```
144-
129+
```javascript
130+
const messenger = await loadMessenger({
131+
// Use Conversation component to display only the messenger without the launcher
132+
customMainComponent: ({ messenger, react }) => {
133+
return (props) => {
134+
return react.createElement(messenger.AgentProviderContainer, props, [
135+
react.createElement(messenger.Conversation),
136+
]);
137+
};
138+
}
139+
});
140+
messenger.initialize({
141+
appId: 'APP_ID',
142+
aiAgentId: 'AI_AGENT_ID',
143+
});
144+
```
145145
### Deauthenticate and clear session
146146

147147
While it is not required, you can de-authenticate the SDK to clear session data and disconnect when a user logs out.
148148

149-
```javascript
150-
messenger.destroy();
151-
```
149+
```javascript
150+
messenger.destroy();
151+
```
152+
153+
### Passing context object to Agent
154+
155+
You can predefine customer-specific information such as country, language, or other custom context data to guide the AI Agent in providing faster and more accurate responses.
156+
157+
This allows for a more personalized and context-aware interaction experience.
158+
159+
> Once the contexts are set, they will be used throughout the conversation to provide personalized and context-aware responses.
160+
161+
162+
### Passing context object to Agent
163+
164+
You can predefine customer-specific information such as country, language, or other custom context data to guide the AI Agent in providing faster and more accurate responses.
165+
166+
This allows for a more personalized and context-aware interaction experience.
167+
168+
> Once the contexts are set, they will be used throughout the conversation to provide personalized and context-aware responses.
169+
170+
```javascript
171+
// Metadata can be updated incrementally using updateMetadata().
172+
// Top-level key-value pairs are merged across multiple calls.
173+
174+
messenger.updateMetadata({
175+
language: 'en-US', // default: navigator.language
176+
});
177+
178+
messenger.updateMetadata({
179+
countryCode: 'US',
180+
});
181+
182+
const newContextObject = { key1: 'value1', key2: 'value2' };
183+
messenger.updateMetadata({
184+
message: { contextObject: newContextObject },
185+
});
186+
187+
// Updating context with a new object replaces the previous one.
188+
const removedContextObject = { key1: 'value1' };
189+
messenger.updateMetadata({
190+
message: { contextObject: removedContextObject },
191+
});
192+
193+
// Sending an empty object clears the context.
194+
const emptyContextObject = {};
195+
messenger.updateMetadata({
196+
message: { contextObject: emptyContextObject },
197+
});
198+
```

0 commit comments

Comments
 (0)