logo

IM Customer Service SDK

With this SDK, you can implement an IM customer service workstation with simple configuration.

SDK Address

https://{company_customer_service_domain}/lib/udesk-im/agent-client.js

How to Use

Start by including this JavaScript

<script src='https://{company_customer_service_domain}/lib/udesk-im/agent-client.js'></script>

Create an Instance

<script>
// Fetch token
function fetchToken(){
    if(fetchToken.cache){ // If token is cached, return it directly
        return Promise.resolve(fetchToken.cache);
    }
    return fetch('/token').then((t)=>{
        setTimeout(()=>fetchToken.cache = null,60*60*1000); // Refresh token every hour to avoid expiration
        fetchToken.cache=t;   // Cache token
        return t;
    });
}
const udeskAgentClient = new UdeskAgentClient({
    host: 'xxxx.udesk.cn', // Udesk customer service domain
    getToken: ()=> fetchToken(), // Request backend to get token
    lang?: 'zh-cn' // SDK language, supports Chinese, English, Japanese
})
</script>

Create a Container and Render into it

<!-- The container needs to have a width and height, otherwise the rendering result style will be messy -->
<style>
#udesk-agent-client {
    width: 100%;
    height: 100%;
}
</style>
<div id='udesk-agent-client'></div>
<script>
udeskAgentClient.renderTo(document.getElementById('udesk-agent-client'));
</script>

Parameter Explanation

ParameterRequiredDescription
getTokenYes()=>Promise<string>, used for authentication
hostYesUdesk customer service system's host
onNewChatNoTriggered when a new chat occurs
langNoSDK language, defaults to Chinese if not passed, valid values are 'zh-cn'|'en-us'|'ja'

Token Generation Method

The token is in JWT format.
Obtain the JWT secret key: In the Udesk system, go to Management Center - Single Sign-On - Shared Secret
JWT data must include

{
  "email": "agent@udesk.cn", // Agent email
  "iat": 1605768993, // Integer of Greenwich time, token generation time
}

Note:

JWT_TOKEN expires after 10 hours. Developers need to provide functionality to refresh JWT_TOKEN.