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
/
SendRichCardMessageSnippet.java
106 lines (92 loc) · 4.49 KB
/
SendRichCardMessageSnippet.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 com.google.communications.businessmessages.v1.MediaHeight;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.UUID;
class SendRichCardMessageSnippet {
/**
* 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 card with two suggested replies
BusinessMessagesMessage message = new BusinessMessagesMessage()
.setMessageId(UUID.randomUUID().toString())
.setFallback("Hello, world!\nSent with Business Messages\n\nReply with \"Suggestion #1\" or \"Suggestion #2\"")
.setRichCard(new BusinessMessagesRichCard()
.setStandaloneCard(new BusinessMessagesStandaloneCard()
.setCardContent(
new BusinessMessagesCardContent()
.setTitle("Hello, world!")
.setDescription("Sent with Business Messages.")
.setSuggestions(Arrays.asList(
new BusinessMessagesSuggestion()
.setReply(new BusinessMessagesSuggestedReply()
.setText("Suggestion #1").setPostbackData("suggestion_1")
),
new BusinessMessagesSuggestion()
.setReply(new BusinessMessagesSuggestedReply()
.setText("Suggestion #2").setPostbackData("suggestion_2")
))
)
.setMedia(new BusinessMessagesMedia()
.setHeight(MediaHeight.MEDIUM.toString())
.setContentInfo(
new BusinessMessagesContentInfo()
.setAltText("Google logo")
.setFileUrl("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
.setForceRefresh(false)
))
)))
.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();
}
}
}