[SalesForce] How to keep Session Id and key after implement ChasitorInit request

I want to integrate an social chat App (WeChat) into Salesforce, I wish this integration can implement an complete chat communication between Live agent and customer such as following:

  1. customer send a message to an Org (Salesforce plateform)
  2. this message become a chat request for Live Chat agent, if any Live Chat agent accept that request, customer also become a chat visitor.
  3. customer can communicate with Live agent in WeChat App layout by seamless
  4. Live agent can start to chat with customer or chat visitor by using Live Chat app layout

I already can make step 1 and 2 above happen based on Salesforce oficial guide as following:

a.) Create a Live Agent Session

https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_SessionId.htm

b.) Create a Chat Visitor Session

https://developer.salesforce.com/docs/atlas.en-us.live_agent_rest.meta/live_agent_rest/live_agent_rest_ChasitorInit.htm

but now I cannot start step 3 because every time I follow official guide to start a chat, I always receive error message as following:
enter image description here

Can someone help me to fix this problem?

ps: everyone who try to help me fix this problem, you no need to concern about WeChat side technical issues because I already handle all of it and I'm sure problem what I'm proposed not coming from WeChat APP. Now, What I need is technical support from Salesforce side.
@Ilya Lepesh

=================================================================

New update 1:
I already can achieve my step 3 by using "ChatMessage" method as following:

    HttpRequest req = new HttpRequest();   
    Http h = new Http();  
    String bodyRes = '';        

    req.SetEndPoint('https://d.la10.salesforceliveagent.com/chat/rest/Chasitor/ChatMessage'); 
    req.setMethod('POST');
    req.setHeader('X-LIVEAGENT-API-VERSION','36');
    req.setHeader('X-LIVEAGENT-AFFINITY', affinityToken);
    req.setHeader('X-LIVEAGENT-SESSION-KEY', key);

    JSONGenerator gen = JSON.createGenerator(true);

    gen.writeStartObject();
        gen.writeStringField('text', '1');
    gen.writeEndObject();

    String sendMsg = gen.getAsString();
    req.setBody(sendMsg);
    try {
        HttpResponse res = h.send(req);
        bodyRes = res.getBody();     
    } catch (System.CalloutException e) {
        System.debug('Callout error: '+ e);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage()));
    } 

The reason I'm using "ChatMessage" method because new incoming message come from WeChat(social APP), then I have to handle this message first by extracting from WeChat API, and then I using code above to push this message to the Salesforce Live Agent as following:

enter image description here

WeChat Layout "send"

enter image description here
Live Agent Layout "receive"

As you can see, every message either I send from Live Agent or receive from WeChat, I have to pre-process first to make sure all incoming or outcoming message are fix to Live Agent and WeChat.

Now, I got new problem is I don't know how to achieve my step 4 because I don't know how to obtain Live Agent message and send it to WeChat client. Because if I just directly input my message into Live Agent panel and click "send" button, then WeChat client will receive nothing.

If I can can control the Live Agent send message behavior, then I can send it to specific URL and WeChat client can see it appear in the WeChat layout.

Can you help me to fix this problem?

Best Answer

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.

  • Now let's move on to Messages service:

    Returns all messages that were sent between agents and chat visitors during a chat 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:

LiveChat messages

  • 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-------------->|