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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added snippets folder and excluded them from the build
- Loading branch information
erlapso
committed
Sep 1, 2022
1 parent
327a28c
commit abd2883
Showing
15 changed files
with
1,301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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.BusinessMessagesReceipt; | ||
|
||
import com.google.api.services.businessmessages.v1.model.*; | ||
import java.io.FileInputStream; | ||
import java.util.Arrays; | ||
import java.util.UUID; | ||
|
||
public class ReadReceiptSnippet { | ||
/** | ||
* 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"; | ||
String messageId = "MESSAGE_ID"; | ||
|
||
// Create client library reference | ||
Businessmessages.Builder builder = getBusinessMessagesBuilder(); | ||
|
||
// Create a new read receipt | ||
Businessmessages.Conversations.Messages.UpdateReceipt request | ||
= builder.build().conversations().messages() | ||
.updateReceipt("conversations/" + conversationId + "/messages/" + messageId + "/receipt", | ||
new BusinessMessagesReceipt().setReceiptType("READ")); | ||
|
||
// Set up retries with exponential backoff | ||
HttpRequest httpRequest = | ||
((AbstractGoogleClientRequest) request).buildHttpRequest(); | ||
|
||
httpRequest.setUnsuccessfulResponseHandler(new | ||
HttpBackOffUnsuccessfulResponseHandler( | ||
new ExponentialBackOff())); | ||
|
||
// Execute request | ||
httpRequest.execute(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
snippets/com/google/apis/SendAuthenticationRequestSuggestionSnippet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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 SendAuthenticationRequestSuggestionSnippet { | ||
/** | ||
* 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 text message with an authentication request | ||
BusinessMessagesMessage message = new BusinessMessagesMessage() | ||
.setMessageId(UUID.randomUUID().toString()) | ||
.setText("Would you like to chat with a live agent?") | ||
.setFallback("Would you like to chat with a live agent?") | ||
.setSuggestions(Arrays.asList(new BusinessMessagesSuggestion() | ||
.setAuthenticationRequest(new BusinessMessagesAuthenticationRequest() | ||
.setOauth(new BusinessMessagesAuthenticationRequestOauth() | ||
.setClientId("CLIENT_ID") | ||
.setCodeChallenge("CODE_CHALLENGE") | ||
.setScopes(Arrays.asList("SCOPE")) | ||
))) | ||
) | ||
.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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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 SendDialActionSnippet { | ||
/** | ||
* 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 text message with a dial action | ||
BusinessMessagesMessage message = new BusinessMessagesMessage() | ||
.setMessageId(UUID.randomUUID().toString()) | ||
.setText("Contact support for help with this issue.") | ||
.setFallback("Give us a call at +12223334444.") | ||
.setSuggestions(Arrays.asList(new BusinessMessagesSuggestion() | ||
.setAction(new BusinessMessagesSuggestedAction() | ||
.setText("Call support").setPostbackData("call-support") | ||
.setDialAction( | ||
new BusinessMessagesDialAction().setPhoneNumber("+12223334444")) | ||
))) | ||
.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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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 SendImageMessageSnippet { | ||
/** | ||
* 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 an Image | ||
BusinessMessagesMessage message = new BusinessMessagesMessage() | ||
.setMessageId(UUID.randomUUID().toString()) | ||
.setRepresentative(representative) | ||
.setImage(new BusinessMessagesImage() | ||
.setContentInfo( | ||
new BusinessMessagesContentInfo() | ||
.setFileUrl("FILE_URL") | ||
.setAltText("ALT_TEXT") | ||
.setForceRefresh("FORCE_REFRESH") | ||
)); | ||
|
||
// 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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.