|
| 1 | +<!doctype html> |
| 2 | +<html lang="en" class="h-full bg-gray-50"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 6 | + <title>AI Agent Messenger Quick Start</title> |
| 7 | + <link rel="stylesheet" href="style.css"> |
| 8 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.css"> |
| 9 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/theme/dracula.min.css"> |
| 10 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/codemirror.min.js"></script> |
| 11 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.min.js"></script> |
| 12 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/xml/xml.min.js"></script> |
| 13 | + <style> |
| 14 | + .CodeMirror { |
| 15 | + height: auto; |
| 16 | + border-radius: 0.375rem; |
| 17 | + font-size: 0.875rem; |
| 18 | + padding: 0.5rem; |
| 19 | + } |
| 20 | + </style> |
| 21 | + </head> |
| 22 | + <body class="h-full"> |
| 23 | + <div class="min-h-full"> |
| 24 | + <div class="bg-white shadow"> |
| 25 | + <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8"> |
| 26 | + <h1 class="text-3xl font-bold tracking-tight text-gray-900">AI Agent Messenger Quick Start</h1> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | + <main> |
| 30 | + <div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8"> |
| 31 | + <div class="px-4 py-6 sm:px-0"> |
| 32 | + <div class="flex gap-6"> |
| 33 | + <div class="w-2/3 overflow-hidden bg-white shadow sm:rounded-lg"> |
| 34 | + <div class="px-4 py-5 sm:p-6"> |
| 35 | + <div class="space-y-6"> |
| 36 | + <!-- Required Section --> |
| 37 | + <div class="border-b border-gray-200 pb-6"> |
| 38 | + <h2 class="text-2xl font-bold leading-6 text-gray-900">Getting Started</h2> |
| 39 | + <p class="mb-6 mt-3 text-base text-gray-500"> |
| 40 | + This guide demonstrates how to integrate the AI Agent Messenger using vanilla JavaScript, making it easy to implement in any browser environment without additional dependencies. |
| 41 | + </p> |
| 42 | + <div> |
| 43 | + <h3 class="text-lg font-medium leading-6 text-gray-900"> |
| 44 | + Step 1. Install AI Agent SDK |
| 45 | + </h3> |
| 46 | + <p class="mt-2 text-sm text-gray-500"> |
| 47 | + Add the AI Agent SDK to your web page by importing it as a module. |
| 48 | + </p> |
| 49 | + <div class="mt-4"> |
| 50 | + <textarea id="loadScript" class="code-editor-html"> |
| 51 | +<script type="module"> |
| 52 | + import { loadMessenger } from "https://aiagent.sendbird.com/orgs/default/index.js"; |
| 53 | +</script></textarea> |
| 54 | + </div> |
| 55 | + |
| 56 | + <h3 class="text-lg font-medium leading-6 text-gray-900 mt-6"> |
| 57 | + Step 2. Initialize Messenger |
| 58 | + </h3> |
| 59 | + <p class="mt-2 text-sm text-gray-500"> |
| 60 | + The messenger will be initialized and opened automatically after 2 seconds. |
| 61 | + </p> |
| 62 | + <div class="mt-4"> |
| 63 | + <textarea id="initializeCode" class="code-editor-js"> |
| 64 | +const messenger = await loadMessenger(); |
| 65 | +await messenger.initialize({ |
| 66 | + appId: 'APP_ID', |
| 67 | + aiAgentId: 'AI_AGENT_ID', |
| 68 | +}); |
| 69 | + |
| 70 | +// open the messenger automatically after 2 seconds |
| 71 | +setTimeout(() => { |
| 72 | + messenger.open(); |
| 73 | +}, 2000); |
| 74 | +</textarea> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + |
| 79 | + <!-- Advanced Section --> |
| 80 | + <div class="pt-6"> |
| 81 | + <h2 class="text-xl font-bold leading-6 text-gray-900 mb-6">Advanced Features</h2> |
| 82 | + |
| 83 | + <div class="space-y-8"> |
| 84 | + <div> |
| 85 | + <h3 class="text-lg font-medium leading-6 text-gray-900">Switch Application</h3> |
| 86 | + <p class="mt-2 text-sm text-gray-500">Change to a different application configuration.</p> |
| 87 | + <div class="mt-4"> |
| 88 | + <textarea id="updateConfigCode" class="code-editor-js"> |
| 89 | +await messenger.updateConfig({ |
| 90 | + appId: 'NEW_APP_ID', |
| 91 | + aiAgentId: 'NEW_AI_AGENT_ID', |
| 92 | + // ... other config options |
| 93 | +});</textarea> |
| 94 | + </div> |
| 95 | + <button id="updateConfigBtn" class="mt-3 inline-flex items-center rounded-md bg-[#742ddd] px-3 py-2 text-sm text-white shadow-sm hover:bg-[#6211c8]"> |
| 96 | + Update Config |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + |
| 100 | + <div> |
| 101 | + <h3 class="text-lg font-medium leading-6 text-gray-900">User Authentication</h3> |
| 102 | + <p class="mt-2 text-sm text-gray-500">Set up user authentication for personalized experience.</p> |
| 103 | + <div class="mt-4"> |
| 104 | + <textarea id="updateSessionCode" class="code-editor-js"> |
| 105 | +await messenger.updateUserSession({ |
| 106 | + userId: 'new_user_id', |
| 107 | + authToken: 'new_auth_token', |
| 108 | + // this callback should handle session token refresh: |
| 109 | + onSessionTokenRequest: async () => { |
| 110 | + const response = await fetch('new-token-endpoint'); |
| 111 | + return response.token; |
| 112 | + } |
| 113 | +});</textarea> |
| 114 | + </div> |
| 115 | + <button id="updateSessionBtn" class="mt-3 inline-flex items-center rounded-md bg-[#742ddd] px-3 py-2 text-sm text-white shadow-sm hover:bg-[#6211c8]"> |
| 116 | + Update Session |
| 117 | + </button> |
| 118 | + </div> |
| 119 | + |
| 120 | + <div> |
| 121 | + <h3 class="text-lg font-medium leading-6 text-gray-900">Manual Controls</h3> |
| 122 | + <p class="mt-2 text-sm text-gray-500">Manually control the messenger visibility.</p> |
| 123 | + <div class="mt-4"> |
| 124 | + <textarea id="controlCode" class="code-editor-js"> |
| 125 | +function openAgent() { |
| 126 | + if (!messenger) return; |
| 127 | + messenger.open(); |
| 128 | +} |
| 129 | + |
| 130 | +function closeAgent() { |
| 131 | + if (!messenger) return; |
| 132 | + messenger.close(); |
| 133 | +}</textarea> |
| 134 | + </div> |
| 135 | + <div class="mt-3 space-x-3"> |
| 136 | + <button id="openBtn" class="inline-flex items-center rounded-md bg-[#742ddd] px-3 py-2 text-sm text-white shadow-sm hover:bg-[#6211c8]"> |
| 137 | + Open Messenger |
| 138 | + </button> |
| 139 | + <button id="closeBtn" class="inline-flex items-center rounded-md bg-[#742ddd] px-3 py-2 text-sm text-white shadow-sm hover:bg-[#6211c8]"> |
| 140 | + Close Messenger |
| 141 | + </button> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div> |
| 146 | + <h3 class="text-lg font-medium leading-6 text-gray-900">Conversation List Display</h3> |
| 147 | + <p class="mt-2 text-sm text-gray-500">Display conversation list in a designated area without launcher.</p> |
| 148 | + <div class="mt-4"> |
| 149 | + <textarea id="customDisplayCode" class="code-editor-js"> |
| 150 | +await messenger.initialize({ |
| 151 | + appId: 'APP_ID', |
| 152 | + aiAgentId: 'AI_AGENT_ID', |
| 153 | + // Use Conversation component to display only the messenger without the launcher |
| 154 | + customMainComponent: ({ messenger }) => (props) => ( |
| 155 | + <messenger.AgentProviderContainer {...props}> |
| 156 | + <messenger.Conversation /> |
| 157 | + </messenger.AgentProviderContainer> |
| 158 | + ), |
| 159 | +}); |
| 160 | +</textarea> |
| 161 | + </div> |
| 162 | + <button id="renderConversationListBtn" class="mt-3 inline-flex items-center rounded-md bg-[#742ddd] px-3 py-2 text-sm text-white shadow-sm hover:bg-[#6211c8]"> |
| 163 | + Open Conversation List |
| 164 | + </button> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + </main> |
| 175 | + </div> |
| 176 | + |
| 177 | + <!-- Code Editor --> |
| 178 | + <script> |
| 179 | + const editors = {}; |
| 180 | + const configs = { |
| 181 | + mode: 'javascript', |
| 182 | + theme: 'dracula', |
| 183 | + readOnly: true, |
| 184 | + viewportMargin: Infinity, |
| 185 | + height: 'auto' |
| 186 | + } |
| 187 | + document.querySelectorAll('.code-editor-js').forEach((element) => { |
| 188 | + editors[element.id] = CodeMirror.fromTextArea(element, configs); |
| 189 | + }); |
| 190 | + document.querySelectorAll('.code-editor-html').forEach((element) => { |
| 191 | + editors[element.id] = CodeMirror.fromTextArea(element, { ...configs, mode: 'xml' }); |
| 192 | + }); |
| 193 | + </script> |
| 194 | + |
| 195 | + <!-- Live Demo --> |
| 196 | + <script type="module"> |
| 197 | + import { loadMessenger } from 'https://aiagent.stg.sendbirdtest.com/orgs/default/index.js'; |
| 198 | + |
| 199 | + const messenger = await loadMessenger(); |
| 200 | + await messenger.initialize({ |
| 201 | + appId: import.meta.env.VITE_APP_ID, |
| 202 | + aiAgentId: import.meta.env.VITE_AI_AGENT_ID, |
| 203 | + customApiHost: import.meta.env.VITE_CUSTOM_API_HOST, |
| 204 | + customWebSocketHost: import.meta.env.VITE_CUSTOM_WS_HOST, |
| 205 | + rootElement: document.getElementById('messenger-container'), |
| 206 | + }); |
| 207 | + |
| 208 | + // open the messenger automatically after 2 seconds |
| 209 | + setTimeout(() => { |
| 210 | + messenger.open(); |
| 211 | + }, 2000); |
| 212 | + |
| 213 | + document.getElementById('updateConfigBtn').addEventListener('click', () => { |
| 214 | + messenger.updateConfig({ |
| 215 | + // try to switch to another app |
| 216 | + appId: import.meta.env.VITE_NEW_APP_ID, |
| 217 | + aiAgentId: import.meta.env.VITE_NEW_AI_AGENT_ID, |
| 218 | + customApiHost: import.meta.env.VITE_NEW_CUSTOM_API_HOST, |
| 219 | + customWebSocketHost: import.meta.env.VITE_NEW_CUSTOM_WS_HOST, |
| 220 | + }); |
| 221 | + }); |
| 222 | + document.getElementById('updateSessionBtn').addEventListener('click', () => { |
| 223 | + messenger.updateUserSession({ |
| 224 | + // try to use an existing test user with its auth token |
| 225 | + userId: import.meta.env.VITE_USER_ID, |
| 226 | + authToken: import.meta.env.VITE_USER_AUTH_TOKEN, |
| 227 | + onSessionTokenRequest: async (resolve) => resolve(import.meta.env.VITE_USER_AUTH_TOKEN), |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
| 231 | + document.getElementById('openBtn').addEventListener('click', () => { |
| 232 | + if (!messenger) return; |
| 233 | + messenger.open(); |
| 234 | + }); |
| 235 | + |
| 236 | + document.getElementById('closeBtn').addEventListener('click', () => { |
| 237 | + if (!messenger) return; |
| 238 | + messenger.close(); |
| 239 | + }); |
| 240 | + |
| 241 | + document.getElementById('renderConversationListBtn').addEventListener('click', async () => { |
| 242 | + messenger.destroy(); |
| 243 | + const conversationListMessenger = await loadMessenger({ |
| 244 | + customMainComponent: ({ messenger, react }) => (props) => { |
| 245 | + return react.createElement(messenger.AgentProviderContainer, props, [ |
| 246 | + react.createElement(messenger.ConversationList), |
| 247 | + ]); |
| 248 | + } |
| 249 | + }); |
| 250 | + await conversationListMessenger.initialize({ |
| 251 | + appId: import.meta.env.VITE_APP_ID, |
| 252 | + aiAgentId: import.meta.env.VITE_AI_AGENT_ID, |
| 253 | + customApiHost: import.meta.env.VITE_CUSTOM_API_HOST, |
| 254 | + customWebSocketHost: import.meta.env.VITE_CUSTOM_WS_HOST, |
| 255 | + rootElement: document.getElementById('messenger-container'), |
| 256 | + }); |
| 257 | + }); |
| 258 | + </script> |
| 259 | + </body> |
| 260 | +</html> |
0 commit comments