במדריך הזה נסביר איך לקבל ולקרוא את המידע שמשתמשים מזינים הודעות בכרטיס וגם תיבות דו-שיח. המשתמשים יכולים להזין נתונים שאפליקציות Chat מקבלות, קוראים ומגיבות ל. ווידג'טים שמאפשרים למשתמשים להזין מידע כוללים את אלה:
TextInput
להזנה של טקסט חופשי שתומך גם בהצעות.SelectionInput
עבור פריטים ברשימה ותפריטים, כמו תיבות סימון, לחצני בחירה ותפריטים נפתחים.DateTimePicker
לרשומות של תאריך ושעה.
אתם יכולים להשתמש בכלי ליצירת כרטיסים כדי לעצב הודעות של כרטיסי JSON ולהציג אותן בתצוגה מקדימה בשביל אפליקציות ל-Chat:
לפתיחת הכלי ליצירת כרטיסיםקבלת קלט נתונים מהמשתמשים מאפשרת לאפליקציות Chat לבצע פעולות כמו הבאים:
- עדכון בקשות תמיכה של שירות לקוחות.
- ליצור הזמנות עבודה.
- ביצוע אימות באמצעות שירותי אינטרנט.
דרישות מוקדמות
Node.js
אפליקציית Google Chat שמופעלת בה תכונות אינטראקטיביות. כדי ליצור להשתמש בשירות HTTP באפליקציית Chat האינטראקטיבית. במדריך למתחילים מוסבר איך עושים את זה.
Apps Script
אפליקציית Google Chat שמופעלת בה תכונות אינטראקטיביות. כדי ליצור כדי להשתמש באפליקציית Chat האינטראקטיבית ב-Apps Script, צריך להשלים את המדריך למתחילים.
Python
אפליקציית Google Chat שמופעלת בה תכונות אינטראקטיביות. כדי ליצור להשתמש בשירות HTTP באפליקציית Chat האינטראקטיבית. במדריך למתחילים מוסבר איך עושים את זה.
הסבר על קבלת נתונים
אפליקציית Chat מציגה למשתמש מידע
או הודעה בכרטיס. בדוגמה הזו, תיבת דו-שיח מבקשת מהמשתמש להזין
פרטים על איש קשר באמצעות
TextInput
וגם
SelectionInput
ווידג'טים:
בסיום, אפליקציית Chat תקבל את הנתונים שמשתמשים הזינו בתיבת הדו-שיח בפורמט JSON אירוע אינטראקציה שבו:
EventType
היאCARD_CLICKED
.DialogEventType
הואSUBMIT_DIALOG
(לתיבות דו-שיח בלבד).
כדי לקבל נתונים על הפרטים שהמשתמשים הזינו, צריך להשתמש
Event.common.formInputs
בשדה המטען הייעודי (payload) של האירוע. השדה formInputs
הוא מפה שבה נמצאים המפתחות
מזהי המחרוזות שהוקצו לכל ווידג'ט והערכים מייצגים את הקלט של המשתמשים עבור
לכל ווידג'ט. אובייקטים שונים מייצגים סוגים שונים של נתוני קלט. עבור
דוגמה,
Event.common.formInputs.stringInputs
שמייצג את ערכי הקלט של מחרוזות.
האפליקציה שלך יכולה לגשת לערך הראשון שהוזן על ידי המשתמש בכתובת
event.common.formInputs.NAME.stringInputs.value[0]
,
כאשר NAME
הוא השדה name
של
הווידג'ט TextInput
.
קבלת נתונים מכרטיסים
כשמשתמש מזין נתונים בהודעה בכרטיס, אפליקציית Chat מקבלת אפליקציית Chat. אירוע אינטראקציה, כמו הדוגמה הבאה:
JSON
{
"type": enum (EventType),
"eventTime": string,
"threadKey": string,
"message": {
object (Message)
},
"user": {
object (User)
},
"space": {
object (Space)
},
"action": {
object (FormAction)
},
"configCompleteRedirectUrl": string,
"common": {
// Represents user data entered in a card.
"formInputs": {
// Represents user data entered for a specific field in a card.
"NAME": {
// Represents string data entered in a card, like text input fields
// and check boxes.
"stringInputs": {
// An array of strings entered by the user in a card.
"value": [
string
]
}
}
},
"parameters": {
string: string,
...
},
"invokedFunction": string
}
}
קבלת נתונים מתיבות דו-שיח
כשמשתמש שולח נתונים בתיבת דו-שיח, אפליקציית Chat שלכם. מקבל אירוע אחר של אינטראקציה עם אפליקציית Chat, כמו לדוגמה:
JSON
{
"type": enum (EventType),
"eventTime": string,
"threadKey": string,
"message": {
object (Message)
},
"user": {
object (User)
},
"space": {
object (Space)
},
"action": {
object (FormAction)
},
"configCompleteRedirectUrl": string,
// Indicates that this event is dialog-related.
"isDialogEvent": true,
// Indicates that a user clicked a button, and all data
// they entered in the dialog is included in Event.common.formInputs.
"dialogEventType": "SUBMIT_DIALOG",
"common": {
"userLocale": string,
"hostApp": enum (HostApp),
"platform": enum (Platform),
"timeZone": {
object (TimeZone)
},
// Represents user data entered in a dialog.
"formInputs": {
// Represents user data entered for a specific field in a dialog.
"NAME": {
// Represents string data entered in a dialog, like text input fields
// and check boxes.
"stringInputs": {
// An array of strings entered by the user in a dialog.
"value": [
string
]
}
}
},
"parameters": {
string: string,
...
},
"invokedFunction": string
}
}
תשובות לנתונים שנאספו מהודעה או בתיבת דו-שיח בכרטיס
אחרי קבלת הנתונים מהודעה או מתיבת דו-שיח לגבי הכרטיס,
אפליקציית Chat מגיבה באמצעות אישור קבלה או באמצעות
החזרת שגיאה, ושניהם מתבצעים על ידי החזרת
ActionResponse
:
- כדי לאשר שהקבלה בוצעה בהצלחה, יש להשיב עם
ActionResponse
של הפרמטר"actionStatus": "OK"
. - כדי להחזיר שגיאה, צריך להשיב עם
ActionResponse
של הפרמטר"actionStatus": "ERROR MESSAGE"
.
דוגמה
הדוגמה הבאה בודקת אם יש ערך מסוג name
. אם השדה חסר,
מחזירה שגיאה. אם קיים, האפליקציה מאשרת את קבלת נתוני הטופס
וסוגר את תיבת הדו-שיח.
Node.js
/**
* Checks for a form input error, the absence of
* a "name" value, and returns an error if absent.
* Otherwise, confirms successful receipt of a dialog.
*
* Confirms successful receipt of a dialog.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in Google Chat.
*/
function receiveDialog(event) {
// Checks to make sure the user entered a name
// in a dialog. If no name value detected, returns
// an error message.
if (event.common.formInputs.WIDGET_NAME.stringInputs.value[0] == "") {
res.json({
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "Don't forget to name your new contact!"
}
}
});
// Otherwise the app indicates that it received
// form data from the dialog. Any value other than "OK"
// gets returned as an error. "OK" is interpreted as
// code 200, and the dialog closes.
} else {
res.json({
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "OK"
}
}
});
}
}
Apps Script
בדוגמה הזו, נשלחת הודעה בכרטיס על ידי חזרה כרטיס JSON. אפשר גם להשתמש שירות הכרטיסים של Apps Script.
/**
* Checks for a form input error, the absence of
* a "name" value, and returns an error if absent.
* Otherwise, confirms successful receipt of a dialog.
*
* Confirms successful receipt of a dialog.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in Google Chat.
*/
function receiveDialog(event) {
// Checks to make sure the user entered a name
// in a dialog. If no name value detected, returns
// an error message.
if (event.common.formInputs.WIDGET_NAME[""].stringInputs.value[0] == "") {
return {
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "Don't forget to name your new contact!"
}
}
};
// Otherwise the app indicates that it received
// form data from the dialog. Any value other than "OK"
// gets returned as an error. "OK" is interpreted as
// code 200, and the dialog closes.
} else {
return {
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "OK"
}
}
};
}
}
Python
def receive_dialog(event: Mapping[str, Any]) -> Mapping[str, Any]:
"""Checks for a form input error, the absence of a "name" value, and returns
an error if absent. Otherwise, confirms successful receipt of a dialog.
Args:
event (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: the response.
"""
if common := event.get('common'):
if form_inputs := common.get('formInputs'):
if contact_name := form_inputs.get('WIDGET_NAME'):
if string_inputs := contact_name.get('stringInputs'):
if name := string_inputs.get('value')[0]:
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'actionStatus': 'OK'
}
}
}
else:
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'actionStatus': 'Don\'t forget to name your new contact!'
}
}
}
פתרון בעיות
כשאפליקציית Google Chat או אפליקציית Google Chat הפונקציה card מחזירה שגיאה, בממשק של Chat מוצגת ההודעה "משהו השתבש". או "לא ניתן לעבד את הבקשה שלך". לפעמים בממשק המשתמש של Chat לא מציגה הודעות שגיאה, אבל אפליקציית Chat או והכרטיס מפיק תוצאה לא צפויה: לדוגמה, הודעה בכרטיס יופיעו.
יכול להיות שלא תופיע הודעת שגיאה בממשק המשתמש של Chat, אבל יש הודעות שגיאה תיאוריות ונתוני יומן זמינים כדי לעזור לכם לתקן שגיאות כשמופעלת רישום שגיאות ביומן של אפליקציות ל-Chat. לקבלת עזרה בצפייה, לניפוי באגים ולתיקון שגיאות: פתרון בעיות ותיקון שגיאות ב-Google Chat
נושאים קשורים
- הצגת דוגמאות של אפליקציות ל-Chat שמעבדות מידע של משתמשים.
- פתיחת תיבות דו-שיח אינטראקטיביות