Configurations in Salesforce for Messaging for In-App and Web REST API:
1. Create a Messaging Channel of type “Messaging for In-App and Web”.
2. Activate the Messaging Channel.
3. Create a Embedded Service Deployment of Conversation Type “Messaging for In-App and Web”.
4. Use Custom Client for the Embedded Service Deployment.
5. Publish the Embedded Service Deployment.
6. Click Code Snippet and note down the Organization Id and the URL.
Steps to invoke the Salesforce for Messaging for In-App and Web REST API:
1. Do a POST Request to get the Access Token.
Sample POST Request Body for Access Token:
{
"orgId": "00DSB00000FiY6z",
"esDeveloperName": "MIAW_REST_API",
"capabilitiesVersion": "1",
"platform": "Web"
}
2. Do a POST Request to create a Conversation.
In order to create a conversation, we have to generate the conversationId value. I have used UUID.randomUUID() in Salesforce apex to generate it. Please check the bottom of the blog post for the full Apex code used to generate.
In the Header, use Authorization as Key and for the value use Bearer followed by the accessToken value from the Get Token POST Request.
Sample POST Request Body to create Conversation:
{
"conversationId": "cebf41a4-f8a5-48ab-a628-7283c794c71c",
"esDeveloperName": "MIAW_REST_API"
}
3. Go a GET Request to subscribe to the Server-Sent Events.
4. Do a POST Request to send a message.
In order to send a message, we have to generate the message id value. I have used UUID.randomUUID() in Salesforce apex to generate it. Please check the bottom of the blog post for the full Apex code used to generate.
In the Header, use Authorization as Key and for the value use Bearer followed by the accessToken value from the Get Token POST Request.
Sample Body for Send Message POST Request:
{
"message": {
"inReplyToMessageId": "",
"id": "5321d8cb-6505-49e3-9d29-5edd1efb5915",
"messageType": "StaticContentMessage",
"staticContent": {
"formatType": "Text",
"text": "Testing REST API for MIAW"
}
},
"esDeveloperName": "MIAW_REST_API",
"isNewMessagingSession": true,
"routingAttributes": {},
"language": "en"
}
5. Do a DELETE Request to close the Conversation.
In the Header, use Authorization as Key and for the value use Bearer followed by the accessToken value from the Get Token POST Request.
Output:
Sample Apex Code to generate UUID:
UUID randomUUID = UUID.randomUUID();
System.debug(
randomUUID
);