You haven't include header for ChasitorInit request:
X-LIVEAGENT-AFFINITY
Where X-LIVEAGENT-AFFINITY is the system-generated ID used to identify the Live Agent session on the Live Agent servers. This affinity token is included in the response body of the SessionId request.
That's why you have http 403
When you are using Rest tools e.g. PostMan, token could be omitted, but when you are trying to use SF, ChasitorInit denies access.
Modify Get:
req.setHeader('X-LIVEAGENT-AFFINITY', 'null');
//..
else if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'affinityToken')){
parser.nextToken();
affinityToken = parser.getText();
}
Post:
req.setHeader('X-LIVEAGENT-AFFINITY', affinityToken);
Minimum request message looks like:
{
"organizationId": "00Dxxxxxxxxxxxx",
"deploymentId": "572xxxxxxxxxxxx",
"buttonId": "573xxxxxxxxxxxx",
"sessionId": "0b333f76-90c7-4cc0-8653-de2885857236",
"userAgent": "",
"language": "en-US",
"screenResolution": "1900x1080",
"visitorName": "Ilya",
"prechatDetails": [],
"prechatEntities": [],
"receiveQueueUpdates": true,
"isPost": true
}
It's very close to your original message, but prechatDetails,prechatEntities
are required.
Part1
If you look at SessionId
response, you will see parameter clientPollTimeout
:
The number of seconds before you must make a Messages request before your Messages long polling loop times out and is terminated.
And then you refer to Your Message Long Polling Loop:
If you don’t make another Messages request to continue the messaging loop, your session will end after a system timeout on the Live Agent server.
This means that LiveChat was designed in way to listen to client messages. If client isn't active in 30(clientPollTimeout value) seconds, livechat will terminate session.
For example during your session, agent could perform some actions, and livechat will send back everything to the client.
- If you look at any response, you will see parameter sequence, this is serial number of message. When you are going to use messages service you should add parameter ack=sequence.
req.SetEndPoint('https:example/chat/rest/System/Messages?ack=' + sequence);
You can parse your responses or use increment operation for this.
Finally we're going to check code:
private static integer sequence = 0;
//..
webservice static void getMessages() {
HttpRequest req = new HttpRequest();
Http h = new Http();
String response;
req.SetEndPoint('https:example.com/System/Messages?ack=' + sequence);
req.setMethod('GET');
req.setHeader('X-LIVEAGENT-API-VERSION','36');
req.setHeader('X-LIVEAGENT-AFFINITY', affinityToken);
req.setHeader('X-Liveagent-Session-Key', key);
try {
HttpResponse resp = h.send(req);
response = resp.getBody();
} catch (System.CalloutException e) {
System.debug(loggingLevel.ERROR, 'Callout error: ' + e);
}
system.debug('messages: ' + response);
sequence += 1;
}
- If you interact with messages service couple times, (after you've got sessionid and created chatroom and accept request in SF). And livechat agent starts typing and then finishes session, you'll have:
- In your service, you just have to use messages service every ~25 seconds and process its response.
- If you've received 503, you could restart chat by : ResyncSession
updated:
Make sure, that yo have following structure:
WeChat LiveAgent
|----SessionId------------->|
|<----key-------------------|
|---ChasitorInit(key)------>|
|<--chatestablished---------|
|-GET Msg(every 30sec)----->|
|<- messages( diff types )--|
|-POST ChatMessage(body)--->|
| GET Messages------------->|
|<-- http 503---------------|
|---ResyncSession---------->|
| GET Messages------------->|
| ----ChatEnd-------------->|
Best Answer
You can not set time out.
In Live agent Rest API document (page 4) it is mentioned as:
You can only get the timeout property (
clientPollTimeout
) from session Id request.