This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SendRichTextMessageSnippet.java
82 lines (68 loc) · 3.09 KB
/
SendRichTextMessageSnippet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.services.businessmessages.v1.Businessmessages;
import com.google.api.services.businessmessages.v1.model.*;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class SendRichTextMessageSnippet {
/**
* Initializes credentials used by the Business Messages API.
*/
private static Businessmessages.Builder getBusinessMessagesBuilder() {
Businessmessages.Builder builder = null;
try {
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream("PATH_TO_SERVICE_ACCOUNT_KEY"));
credential = credential.createScoped(Arrays.asList(
"https://www.googleapis.com/auth/businessmessages"));
credential.refreshToken();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// Create instance of the Business Messages API
builder = new Businessmessages
.Builder(httpTransport, jsonFactory, null)
.setApplicationName("Sample Application");
// Set the API credentials and endpoint
builder.setHttpRequestInitializer(credential);
} catch (Exception e) {
e.printStackTrace();
}
return builder;
}
public static void main(String args[]) {
try {
String conversationId = "CONVERSATION_ID";
// Create client library reference
Businessmessages.Builder builder = getBusinessMessagesBuilder();
// Create a rich text message
BusinessMessagesMessage message = new BusinessMessagesMessage()
.setMessageId(UUID.randomUUID().toString())
.setContainsRichText(true) // Force this message to be processed as rich text
.setText("Hello, here is some **bold text**, *italicized text*, and a [link](https://www.google.com).")
.setFallback("Hello, check out this link https://www.google.com.")
.setRepresentative(new BusinessMessagesRepresentative()
.setRepresentativeType("TYPE"));
// Create message request
Businessmessages.Conversations.Messages.Create messageRequest
= builder.build().conversations().messages()
.create("conversations/" + conversationId, message);
// Setup retries with exponential backoff
HttpRequest httpRequest =
((AbstractGoogleClientRequest) messageRequest).buildHttpRequest();
httpRequest.setUnsuccessfulResponseHandler(new
HttpBackOffUnsuccessfulResponseHandler(
new ExponentialBackOff()));
// Execute request
httpRequest.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}