Skip to content

Commit 9459ae7

Browse files
feat: upgrade close dialog code snippets (#362)
* feat: upgrade close dialog code snippets * Update python sample * Update Java sample * Update Apps Script sample
1 parent 8fcce72 commit 9459ae7

File tree

4 files changed

+89
-89
lines changed
  • apps-script/contact-form-app
  • java/contact-form-app/src/main/java/com/google/chat/contact
  • node/contact-form-app
  • python/contact-form-app

4 files changed

+89
-89
lines changed

apps-script/contact-form-app/main.gs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,25 @@ function submitForm(event) {
177177
const contactName = event.common.parameters["contactName"];
178178
// Checks to make sure the user entered a contact name.
179179
// If no name value detected, returns an error message.
180-
if (!contactName) {
181-
const errorMessage = "Don't forget to name your new contact!";
182-
if (event.dialogEventType === "SUBMIT_DIALOG") {
183-
return { actionResponse: {
184-
type: "DIALOG",
185-
dialogAction: { actionStatus: {
186-
statusCode: "INVALID_ARGUMENT",
187-
userFacingMessage: errorMessage
188-
}}
189-
}};
190-
} else {
191-
return {
192-
privateMessageViewer: event.user,
193-
text: errorMessage
194-
};
195-
}
180+
const errorMessage = "Don't forget to name your new contact!";
181+
if (!contactName && event.dialogEventType === "SUBMIT_DIALOG") {
182+
return { actionResponse: {
183+
type: "DIALOG",
184+
dialogAction: { actionStatus: {
185+
statusCode: "INVALID_ARGUMENT",
186+
userFacingMessage: errorMessage
187+
}}
188+
}};
196189
}
197190
// [END status_notification]
191+
if (!contactName) {
192+
return {
193+
privateMessageViewer: event.user,
194+
text: errorMessage
195+
};
196+
}
198197

199-
// [START confirmation_message]
198+
// [START confirmation_success]
200199
// The Chat app indicates that it received form data from the dialog or card.
201200
// Sends private text message that confirms submission.
202201
const confirmationMessage = "✅ " + contactName + " has been added to your contacts.";
@@ -209,14 +208,15 @@ function submitForm(event) {
209208
userFacingMessage: "Success " + contactName
210209
}}
211210
}
212-
}
213-
} else {
214-
return {
215-
actionResponse: { type: "NEW_MESSAGE" },
216-
privateMessageViewer: event.user,
217-
text: confirmationMessage
218211
};
219212
}
213+
// [END confirmation_success]
214+
// [START confirmation_message]
215+
return {
216+
actionResponse: { type: "NEW_MESSAGE" },
217+
privateMessageViewer: event.user,
218+
text: confirmationMessage
219+
};
220220
// [END confirmation_message]
221221
}
222222

java/contact-form-app/src/main/java/com/google/chat/contact/App.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,22 @@ Message submitForm(JsonNode event) {
202202
String contactName = event.at("/common/parameters/contactName").asText();
203203
// Checks to make sure the user entered a contact name.
204204
// If no name value detected, returns an error message.
205-
if (contactName.isEmpty()) {
206-
String errorMessage = "Don't forget to name your new contact!";
207-
if (event.at("/dialogEventType") != null && "SUBMIT_DIALOG".equals(event.at("/dialogEventType").asText())) {
208-
return new Message().setActionResponse(new ActionResponse()
209-
.setType("DIALOG")
210-
.setDialogAction(new DialogAction().setActionStatus(new ActionStatus()
211-
.setStatusCode("INVALID_ARGUMENT")
212-
.setUserFacingMessage(errorMessage))));
213-
} else {
214-
return new Message()
215-
.setPrivateMessageViewer(new User().setName(event.at("/user/name").asText()))
216-
.setText(errorMessage);
217-
}
205+
String errorMessage = "Don't forget to name your new contact!";
206+
if (contactName.isEmpty() && event.at("/dialogEventType") != null && "SUBMIT_DIALOG".equals(event.at("/dialogEventType").asText())) {
207+
return new Message().setActionResponse(new ActionResponse()
208+
.setType("DIALOG")
209+
.setDialogAction(new DialogAction().setActionStatus(new ActionStatus()
210+
.setStatusCode("INVALID_ARGUMENT")
211+
.setUserFacingMessage(errorMessage))));
218212
}
219213
// [END status_notification]
214+
if (contactName.isEmpty()) {
215+
return new Message()
216+
.setPrivateMessageViewer(new User().setName(event.at("/user/name").asText()))
217+
.setText(errorMessage);
218+
}
220219

221-
// [START confirmation_message]
220+
// [START confirmation_success]
222221
// The Chat app indicates that it received form data from the dialog or card.
223222
// Sends private text message that confirms submission.
224223
String confirmationMessage = "✅ " + contactName + " has been added to your contacts.";
@@ -228,12 +227,13 @@ Message submitForm(JsonNode event) {
228227
.setDialogAction(new DialogAction().setActionStatus(new ActionStatus()
229228
.setStatusCode("OK")
230229
.setUserFacingMessage("Success " + contactName))));
231-
} else {
232-
return new Message()
233-
.setActionResponse(new ActionResponse().setType("NEW_MESSAGE"))
234-
.setPrivateMessageViewer(new User().setName(event.at("/user/name").asText()))
235-
.setText(confirmationMessage);
236230
}
231+
// [END confirmation_success]
232+
// [START confirmation_message]
233+
return new Message()
234+
.setActionResponse(new ActionResponse().setType("NEW_MESSAGE"))
235+
.setPrivateMessageViewer(new User().setName(event.at("/user/name").asText()))
236+
.setText(confirmationMessage);
237237
// [END confirmation_message]
238238
}
239239

node/contact-form-app/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,25 @@ function submitForm(event) {
197197
const contactName = event.common.parameters["contactName"];
198198
// Checks to make sure the user entered a contact name.
199199
// If no name value detected, returns an error message.
200-
if (!contactName) {
201-
const errorMessage = "Don't forget to name your new contact!";
202-
if (event.dialogEventType === "SUBMIT_DIALOG") {
203-
return { actionResponse: {
204-
type: "DIALOG",
205-
dialogAction: { actionStatus: {
206-
statusCode: "INVALID_ARGUMENT",
207-
userFacingMessage: errorMessage
208-
}}
209-
}};
210-
} else {
211-
return {
212-
privateMessageViewer: event.user,
213-
text: errorMessage
214-
};
215-
}
200+
const errorMessage = "Don't forget to name your new contact!";
201+
if (!contactName && event.dialogEventType === "SUBMIT_DIALOG") {
202+
return { actionResponse: {
203+
type: "DIALOG",
204+
dialogAction: { actionStatus: {
205+
statusCode: "INVALID_ARGUMENT",
206+
userFacingMessage: errorMessage
207+
}}
208+
}};
216209
}
217210
// [END status_notification]
211+
if (!contactName) {
212+
return {
213+
privateMessageViewer: event.user,
214+
text: errorMessage
215+
};
216+
}
218217

219-
// [START confirmation_message]
218+
// [START confirmation_success]
220219
// The Chat app indicates that it received form data from the dialog or card.
221220
// Sends private text message that confirms submission.
222221
const confirmationMessage = "✅ " + contactName + " has been added to your contacts.";
@@ -229,14 +228,15 @@ function submitForm(event) {
229228
userFacingMessage: "Success " + contactName
230229
}}
231230
}
232-
}
233-
} else {
234-
return {
235-
actionResponse: { type: "NEW_MESSAGE" },
236-
privateMessageViewer: event.user,
237-
text: confirmationMessage
238231
};
239232
}
233+
// [END confirmation_success]
234+
// [START confirmation_message]
235+
return {
236+
actionResponse: { type: "NEW_MESSAGE" },
237+
privateMessageViewer: event.user,
238+
text: confirmationMessage
239+
};
240240
// [END confirmation_message]
241241
}
242242

python/contact-form-app/main.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,23 @@ def submit_form(event: dict) -> dict:
169169
contact_name = event.get('common').get('parameters')["contactName"]
170170
# Checks to make sure the user entered a contact name.
171171
# If no name value detected, returns an error message.
172-
if contact_name == "":
173-
error_message = "Don't forget to name your new contact!"
174-
if "SUBMIT_DIALOG" == event.get('dialogEventType'):
175-
return { 'actionResponse': {
176-
'type': "DIALOG",
177-
'dialogAction': { 'actionStatus': {
178-
'statusCode': "INVALID_ARGUMENT",
179-
'userFacingMessage': error_message
180-
}}
172+
error_message = "Don't forget to name your new contact!"
173+
if contact_name == "" and "SUBMIT_DIALOG" == event.get('dialogEventType'):
174+
return { 'actionResponse': {
175+
'type': "DIALOG",
176+
'dialogAction': { 'actionStatus': {
177+
'statusCode': "INVALID_ARGUMENT",
178+
'userFacingMessage': error_message
181179
}}
182-
else:
183-
return {
184-
'privateMessageViewer': event.get('user'),
185-
'text': error_message
186-
}
187-
# [END status_notification]
188-
189-
# [START confirmation_message]
180+
}}
181+
# [END status_notification]
182+
if contact_name == "":
183+
return {
184+
'privateMessageViewer': event.get('user'),
185+
'text': error_message
186+
}
187+
188+
# [START confirmation_success]
190189
# The Chat app indicates that it received form data from the dialog or card.
191190
# Sends private text message that confirms submission.
192191
confirmation_message = "✅ " + contact_name + " has been added to your contacts.";
@@ -200,13 +199,14 @@ def submit_form(event: dict) -> dict:
200199
}}
201200
}
202201
}
203-
else:
204-
return {
205-
'actionResponse': { 'type': "NEW_MESSAGE" },
206-
'privateMessageViewer': event.get('user'),
207-
'text': confirmation_message
208-
}
209-
# [END confirmation_message]
202+
# [END confirmation_success]
203+
# [START confirmation_message]
204+
return {
205+
'actionResponse': { 'type': "NEW_MESSAGE" },
206+
'privateMessageViewer': event.get('user'),
207+
'text': confirmation_message
208+
}
209+
# [END confirmation_message]
210210

211211

212212
def fetch_form_value(event: dict, widgetName: str) -> str:

0 commit comments

Comments
 (0)