public class ChatConversationExample {
// Find your authentication token in UIC-ADMIN
private static final String AUTH_TOKEN = "1bba5583-d068-496d-a9d8-a5bb2139c97c";
private static final String REFERER = "doc";
private static final String URL = null;
public static void main(String[] args) {
// Create a rest client (using the framework of your choice)
WebBoxApi webBoxAPIClient = new WebBoxApi();
ChatApi chatAPIClient = new ChatApi();
try {
Configuration webBoxConfiguration = webBoxAPIClient.getConfiguration(AUTH_TOKEN, REFERER, URL);
String webBoxRuleUUID = webBoxConfiguration.getWebboxRule().getUuid();
Medium chatMediaStatus = chatAPIClient.getChatAvailability(AUTH_TOKEN, REFERER, URL, webBoxRuleUUID);
System.out.println("Chat media status = " + chatMediaStatus);
if (chatMediaStatus.getStatus() == MediumStatus.OPEN_AVAILABLE) {
Data data = new Data();
data.setTargetingConditionUuid(webBoxRuleUUID);
data.setProfileParameters(new ArrayList<>());
String locationHeader = chatAPIClient.createChat(AUTH_TOKEN, data, REFERER, URL);
System.out.println("Location header = " + locationHeader);
String[] parts = locationHeader.split("/");
String chatSessionUUID = parts[7];
System.out.println("Extracted chat session UUID = " + chatSessionUUID);
// Pool for the events parallelly
new Thread(new EventsPollingRunner(chatAPIClient, chatSessionUUID, AUTH_TOKEN, REFERER, URL)).start();
while ( !EventsPollingRunner.operatorJoinedEventReceived() || EventsPollingRunner.haveBeenDissuaded()) {
// Wait for an agent to join the chat conversation
}
System.out.println("Web user is now in conversation with an agent");
// Notify that the web user started to type a message
chatAPIClient.typingStarted(AUTH_TOKEN, chatSessionUUID, REFERER, URL);
String message = "I need help with an item you are selling";
// Notify that the web user stopped to type a message
chatAPIClient.typingStopped(AUTH_TOKEN, chatSessionUUID, REFERER, URL);
// Send the message
int messageId = 1; // It's the first message
chatAPIClient.sendMessage(AUTH_TOKEN, chatSessionUUID, messageId, message, REFERER, URL);
messageId++;
// Terminate the chat
chatAPIClient.closeChat(AUTH_TOKEN, chatSessionUUID, REFERER, URL);
}
} catch (ApiException e) { // Every error code during processing the requests
e.printStackTrace();
}
}
}
public class EventsPollingRunner implements Runnable {
private static List<> receivedEvents = new ArrayList<>();
private String chatSessionUUID;
private String authenticationToken;
private String referer;
private String url;
private ChatApi chatAPIClient;
public EventsPollingRunner(ChatApi chatAPIClient, String chatSessionUUID, String authenticationToken, String referer, String url) {
this.authenticationToken = authenticationToken;
this.chatSessionUUID = chatSessionUUID;
this.referer = referer;
this.url = url;
this.chatAPIClient = chatAPIClient;
}
@Override
public void run() {
boolean pooling = true;
int lastEventReceivedId = 0;
while (pooling) {
try {
System.out.println("Requesting events with last ID = " + lastEventReceivedId);
MediumAndEvents mediumAndEvents = chatAPIClient.getMediumAndEvents(authenticationToken, chatSessionUUID, lastEventReceivedId, referer, url);
if (mediumAndEvents.getEvents().size() > 0) {
int eventsCount = mediumAndEvents.getEvents().size();
lastEventReceivedId = mediumAndEvents.getEvents().get(eventsCount - 1).getEventId();
System.out.println(mediumAndEvents);
// Check if we should continue polling or not
for (Event event : mediumAndEvents.getEvents()) {
receivedEvents.add(event);
if (event instanceof MediumClosedEvent) {
pooling = false;
}
}
}
Thread.sleep(2000); // Leave time between each request for interactions
} catch (ApiException | InterruptedException e) { // Every error code during processing the requests
e.printStackTrace();
}
}
}
public static boolean operatorJoinedEventReceived() {
for (Event event : receivedEvents) {
if (event instanceof OperatorJoinedEvent) {
return true;
}
}
return false;
}
public static boolean haveBeenDissuaded() {
for (Event event : receivedEvents) {
if (event instanceof OpenMediumResultEvent) {
OpenMediumResultEvent openMediumResultEvent = (OpenMediumResultEvent) event;
if (openMediumResultEvent.getStatus().equals("DISSUADED")) {
return true;
}
}
}
return false;
}
}