Chat REST API Samples
Overview
The following are sample code snippets for chat interactions using the REST API of the Web API Server.
A complete list of Chat interaction operations can be found in the Web API Server API Reference.
Request Chat
[+] Sample
CreateSessionDetails sessionDetails = new CreateSessionDetails();
sessionDetails.setTenantName("Environment");
sessionDetails.setFirstName("John");
sessionDetails.setLastName("Doe");
sessionDetails.setNickname("Nickname");
sessionDetails.setEmailAddress("email@address");
sessionDetails.setSubject("Subject");
ObjectMapper mapper = new ObjectMapper();
String sessionDetailsString = mapper.writeValueAsString(sessionDetails);
Client client = new Client();
WebResource webResource = client.resource("http://localhost:8080/webapi/api/v2/chats");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, sessionDetailsString);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String json = response.getEntity(String.class);
Send Message
[+] Sample
ChatOperationDetails operationDetails = new ChatOperationDetails();
operationDetails.setOperationName("SendMessage");
operationDetails.setAlias("117");
operationDetails.setSecureKey("very_secure");
operationDetails.setUserId("user1");
operationDetails.setTenantName("Environment");
ObjectMapper mapper = new ObjectMapper();
String operationDetailsString = mapper.writeValueAsString(operationDetails);
Client client = new Client();
WebResource webResource = client.resource("http://localhost:8080/webapi/api/v2/chats/0000KaA0C8XH03D1");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, operationDetailsString);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String json = response.getEntity(String.class);
Update User Data
[+] Sample
ChatOperationDetails operationDetails = new ChatOperationDetails();
operationDetails.setOperationName("UpdateUserData");
operationDetails.setAlias("117");
operationDetails.setSecureKey("very_secure");
operationDetails.setUserId("user1");
operationDetails.setTenantName("Environment");
Map<String, String> userData = new HashMap<>();
userData.put("key1", "value1");
operationDetails.setUserData(userData);
ObjectMapper mapper = new ObjectMapper();
String operationDetailsString = mapper.writeValueAsString(operationDetails);
Client client = new Client();
WebResource webResource = client.resource("http://localhost:8080/webapi/api/v2/chats/0000KaA0C8XH03D1");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, operationDetailsString);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String json = response.getEntity(String.class);
Chat Complete
[+] Sample
ChatOperationDetails operationDetails = new ChatOperationDetails();
operationDetails.setOperationName("Complete");
operationDetails.setAlias("117");
operationDetails.setSecureKey("very_secure");
operationDetails.setUserId("user1");
operationDetails.setTenantName("Environment");
ObjectMapper mapper = new ObjectMapper();
String operationDetailsString = mapper.writeValueAsString(operationDetails);
Client client = new Client();
WebResource webResource = client.resource("http://localhost:8080/webapi/api/v2/chats/0000KaA0C8XH03D1");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, operationDetailsString);
if (response.getStatus() != 200)
{
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}
String json = response.getEntity(String.class);