title |
---|
Web Messaging SDK - Java |
- Documentation https://developer.genesys.cloud/devapps/sdk/docexplorer/webmessagingjava/
- Source https://github.com/MyPureCloud/web-messaging-sdk-java
Install the library from maven via the package cloud.genesys:web-messaging-sdk
Import the necessary packages:
import cloud.genesys.webmessaging.sdk.GenesysCloudRegionWebSocketHosts;
import cloud.genesys.webmessaging.sdk.WebMessagingException;
import cloud.genesys.webmessaging.sdk.WebMessagingClient;
import cloud.genesys.webmessaging.sdk.model.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
The web messaging APIs do not require standard Genesys Cloud authentication, but do require a token for all API calls other than creating a new chat. This is handled transparently if you don't require a custom ApiClient
// Configure session listener
WebMessagingClient.SessionListener sessionListener = new WebMessagingClient.SessionListener() {
@Override
public void sessionResponse(SessionResponse sessionResponse, String s) {
//Do stuff here
}
@Override
public void structuredMessage(StructuredMessage structuredMessage, String s) {
}
@Override
public void presignedUrlResponse(PresignedUrlResponse presignedUrlResponse, String s) {
}
@Override
public void uploadSuccessEvent(UploadSuccessEvent uploadSuccessEvent, String s) {
}
@Override
public void uploadFailureEvent(UploadFailureEvent uploadFailureEvent, String s) {
}
@Override
public void connectionClosedEvent(ConnectionClosedEvent connectionClosedEvent, String s) {
}
@Override
public void sessionExpiredEvent(SessionExpiredEvent sessionExpiredEvent, String s) {
}
@Override
public void jwtResponse(JwtResponse jwtResponse, String s) {
}
@Override
public void unexpectedMessage(BaseMessage baseMessage, String s) {
}
@Override
public void webSocketConnected() {
}
@Override
public void webSocketDisconnected(int i, String s) {
}
};
// Instantiate WebMessagingClient
GenesysCloudRegionWebSocketHosts webSocketHost = GenesysCloudRegionWebSocketHosts.us_east_1;
WebMessagingClient client = new WebMessagingClient(webSocketHost);
// Add the session listener
client.addSessionListener(sessionListener);
// Connect to the websocket
String deploymentId = "df2ad262-7fe2-4fb2-9a83-e34be463713a";
String origin = "app.mypurecloud.com";
client.connect(deploymentId, origin);
// Configure a new session
client.configureSession(deploymentId,origin);
// Send message
client.sendMessage("The quick brown fox jumps over the lazy dog");
// Send message with custom attributes
Map<String, String> customAttributes = Map.of(
"customerAccountId", "079fd5fe-03f8-434d-a2cb-0a9946849bd6",
"customerName", "John Doe"
);
client.sendMessage("The quick brown fox jumps over the lazy dog", customAttributes);
// Disconnect from conversation
client.disconnect();
Provide the full web socket url if not using one provided in GenesysCloudRegionWebSocketHosts
:
WebMessagingClient client = new WebMessagingClient("wss://webmessaging.mypurecloud.ie/v1");
If you want to use your own ApiClient you can build it with the access token and set it in the WebMessagingClient
ApiClient
implements a builder pattern to construct new instances:
// Create ApiClient instance
// Set Region
PureCloudRegionHosts region = PureCloudRegionHosts.us_east_1;
ApiClient apiClient = ApiClient.Builder.standard()
.withAccessToken(accessToken)
.withBasePath(region)
.build();
// Set it in the WebMessagingClient
client.setApiClient(apiClient);
Provide the full base url if not using one provided in GenesysCloudRegionHosts
:
.withBasePath("https://api.mypurecloud.ie")
The SDK supports the following HTTP connectors:
- Apache (default, synchronous), use
ApacheHttpClientConnectorProvider
- OkHTTP (synchronous), use
OkHttpClientConnectorProvider
Specify the connector in the builder:
.withProperty(ApiClientConnectorProperty.CONNECTOR_PROVIDER, new OkHttpClientConnectorProvider())
By default, the Java Web Messaging SDK does not automatically retry any failed requests.
To enable automatic retries, provide a RetryConfiguration
object with the maximum number of seconds to retry requests when building the ApiClient
instance.
Building a RetryConfiguration
instance:
ApiClient.RetryConfiguration retryConfiguration = new ApiClient.RetryConfiguration();
retryConfiguration.setMaxRetryTimeSec(30);
Setting RetryConfiguration
instance to ApiClient
:
.withRetryConfiguration(retryConfiguration)
Set the maxRetryTimeSec
to the number of seconds to process retries before returning an error.
When the retry time is a a positive integer, the SDK will follow the recommended backoff logic using the provided configuration.
The best practices are documented in the Rate Limiting Developer Center article.
withDefaultHeader(String header, String value)
Specifies additional headers to be sent with every requestwithUserAgent(String userAgent)
Overrides the default user agent headerwithObjectMapper(ObjectMapper objectMapper)
Overrides the defaultObjectMapper
used for deserializationwithDateFormat(DateFormat dateFormat)
Overrides the defaultDateFormat
withConnectionTimeout(int connectionTimeout)
Overrides the default connection timeoutwithShouldThrowErrors(boolean shouldThrowErrors)
Set tofalse
to suppress throwing of all errorswithProxy(Proxy proxy)
Sets a proxy to use for requestswithAuthenticatedProxy(Proxy proxy, String user, String pass)
Sets an authenticated proxy to use for requests
The SDK is automatically regenerated and published from the API's definition after each API release. For more information on the build process, see the platform-client-sdk-common project.
The SDK's version is incremented according to the Semantic Versioning Specification. The decision to increment version numbers is determined by diffing the Platform API's swagger for automated builds, and optionally forcing a version bump when a build is triggered manually (e.g. releasing a bugfix).
This package is intended to be forwards compatible with v2 of Genesys Cloud's Platform API. While the general policy for the API is not to introduce breaking changes, there are certain additions and changes to the API that cause breaking changes for the SDK, often due to the way the API is expressed in its swagger definition. Because of this, the SDK can have a major version bump while the API remains at major version 2. While the SDK is intended to be forward compatible, patches will only be released to the latest version. For these reasons, it is strongly recommended that all applications using this SDK are kept up to date and use the latest version of the SDK.
For any issues, questions, or suggestions for the SDK, visit the Genesys Cloud Developer Forum.