এই পৃষ্ঠাটি বর্ণনা করে যে কীভাবে আপনার চ্যাট অ্যাপ খুলতে পারে এবং ডায়ালগগুলিতে প্রতিক্রিয়া জানাতে পারে।
ডায়ালগগুলি উইন্ডোযুক্ত, কার্ড -ভিত্তিক ইন্টারফেস যা চ্যাট অ্যাপগুলি ব্যবহারকারীদের সাথে যোগাযোগ করার জন্য খোলে। ব্যবহারকারীদের মাল্টি-স্টেপ প্রসেস সম্পূর্ণ করতে সাহায্য করার জন্য, চ্যাট অ্যাপ ক্রমিক ডায়ালগ খুলতে পারে।
ডায়ালগগুলি নিম্নলিখিত ধরণের ব্যবহারকারীর মিথস্ক্রিয়াগুলির জন্য দরকারী:
- ব্যবহারকারীদের কাছ থেকে তথ্য সংগ্রহ
- ওয়েব পরিষেবার সাথে ব্যবহারকারীদের প্রমাণীকরণ
- চ্যাট অ্যাপ সেটিংস কনফিগার করা হচ্ছে
একটি ডায়ালগ খোলা, জমা দেওয়া বা বাতিল করার জন্য একটি DialogEventType
সহ একটি চ্যাট অ্যাপ থেকে একটি সিঙ্ক্রোনাস প্রতিক্রিয়া প্রয়োজন৷ ডায়ালগগুলি অ্যাসিঙ্ক্রোনাস আর্কিটেকচার দিয়ে তৈরি চ্যাট অ্যাপ দ্বারা সমর্থিত নয়, যেমন Pub/Sub বা create
বার্তা পদ্ধতি। আপনার চ্যাট অ্যাপ যদি অ্যাসিঙ্ক্রোনাস আর্কিটেকচার ব্যবহার করে, তাহলে ডায়ালগের পরিবর্তে একটি কার্ড মেসেজ ব্যবহার করুন।
পূর্বশর্ত
Node.js
- Google Chat- এ অ্যাক্সেস সহ একটি Google Workspace অ্যাকাউন্ট।
- একটি চ্যাট অ্যাপ। একটি চ্যাট অ্যাপ তৈরি করতে, এই কুইকস্টার্টটি অনুসরণ করুন।
- একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খোলা হলে, একটি ডায়ালগ খুলুন নির্বাচনের সাথে কনফিগার করা একটি স্ল্যাশ কমান্ড ।
Node.js কোডের নমুনা ক্লাউড ফাংশন হিসাবে চালানোর জন্য লেখা হয়।
অ্যাপস স্ক্রিপ্ট
- Google Chat- এ অ্যাক্সেস সহ একটি Google Workspace অ্যাকাউন্ট।
- একটি চ্যাট অ্যাপ। একটি চ্যাট অ্যাপ তৈরি করতে, এই কুইকস্টার্টটি অনুসরণ করুন।
- একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খোলা হলে, একটি ডায়ালগ খুলুন নির্বাচনের সাথে কনফিগার করা একটি স্ল্যাশ কমান্ড ।
পাইথন
- Google Chat- এ অ্যাক্সেস সহ একটি Google Workspace অ্যাকাউন্ট।
- একটি চ্যাট অ্যাপ। একটি চ্যাট অ্যাপ তৈরি করতে, এই কুইকস্টার্টটি অনুসরণ করুন।
- একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খোলা হলে, একটি ডায়ালগ খুলুন নির্বাচনের সাথে কনফিগার করা একটি স্ল্যাশ কমান্ড ।
পাইথন কোড নমুনাগুলি পাইথন 3.9 ব্যবহার করে ক্লাউড ফাংশন হিসাবে চালানোর জন্য লেখা হয়।
একটি ডায়ালগ খুলুন
আপনার চ্যাট অ্যাপ একটি কার্ডের বার্তার একটি বোতামে ক্লিক করলে ব্যবহারকারীর প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খুলতে পারে৷
স্ল্যাশ কমান্ড জারি করা ব্যবহারকারীর প্রতিক্রিয়ায় আপনার চ্যাট অ্যাপ একটি ডায়ালগ খুলতে পারে।
যখন একজন ব্যবহারকারী একটি ডায়ালগ খোলে, তখন আপনার চ্যাট অ্যাপ নিম্নলিখিত তথ্য সহ একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
-
isDialogEvent
true
। DialogEventType
নিম্নলিখিত ক্রিয়াগুলির মধ্যে একটি নির্দিষ্ট করে যা ব্যবহারকারী নিয়েছে:-
REQUEST_DIALOG
: একটি ডায়ালগ খোলা হয়েছে৷ -
SUBMIT_DIALOG
: একটি ডায়ালগে একটি বোতামে ক্লিক করেছে৷ -
CANCEL_DIALOG
: একটি ডায়ালগ বাতিল করা হয়েছে৷
-
উদাহরণস্বরূপ, যখন কোনও ব্যবহারকারী একটি ডায়ালগ খোলে, তখন আপনার চ্যাট অ্যাপ নিম্নলিখিতগুলির মতো একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
JSON
{
"type": enum (EventType),
"eventTime": string,
"threadKey": string,
"message": {
object (Message)
},
"user": {
object (User)
},
"space": {
object (Space)
},
"action": {
object (FormAction)
},
"configCompleteRedirectUrl": string,
"isDialogEvent": true,
"dialogEventType": "REQUEST_DIALOG",
"common": {
object (CommonEventObject)
}
}
আপনার চ্যাট অ্যাপ "type": "DIALOG"
এর একটি ActionResponse
ফেরত দিয়ে একটি ডায়ালগ খুলতে পারে একটি DialogAction
সহ যার মধ্যে ডায়ালগের একটি JSON বিবরণ রয়েছে:
JSON
{
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "contactName"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
}
একটি কার্ড বোতাম ক্লিকের প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খুলুন৷
একটি কার্ড বোতাম তৈরি করতে একটি ডায়ালগ খুলুন, নিম্নলিখিতগুলি নির্দিষ্ট করুন:
-
onClick.action.function
একটি ফাংশনের নাম হিসাবে যা একটি ডায়ালগ খোলে। -
onClick.action.interaction
OPEN_DIALOG
হিসাবে। এই সম্পত্তি চ্যাটকে বলে যে চ্যাট অ্যাপ একটি ডায়ালগ খুলতে চায়।
যখন একজন ব্যবহারকারী কার্ডের একটি বোতামে ক্লিক করেন, তখন আপনার চ্যাট অ্যাপ নিম্নলিখিত তথ্য সহ একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
-
EventType
হলCARD_CLICKED
। -
DialogEventType
হলREQUEST_DIALOG
। -
common.invokedFunction
হল ক্লিক করা কার্ড বোতামেরonClick
প্রপার্টি থেকে ফাংশনের নাম।
একটি ডায়ালগ খুলতে, নিম্নলিখিতগুলির সাথে সাড়া দিন:
- "টাইপ" এর একটি
ActionResponse
"type": "DIALOG"
। - একটি
DialogAction
যাতে ডায়ালগের একটি JSON বর্ণনা অন্তর্ভুক্ত থাকে।
এই উদাহরণে, একটি Chat অ্যাপ একটি কার্ডের সাথে একটি MESSAGE
ইন্টারঅ্যাকশন ইভেন্টে প্রতিক্রিয়া জানায় যার একটি বোতাম আছে যা একটি ডায়ালগ খোলে:
Node.js
/**
* Responds to messages that have links whose URLs
* match URL patterns configured for link previews.
*
* @param {Object} event The event object from Chat
* API.
*
* @return {Object} Response from the Chat app
* attached to the message with the previewed link.
*/
exports.onMessage = function onMessage(req, res) {
// Store the Google Chat event as a variable.
const event = req.body;
if (req.method === "GET" || !event.message) {
res.send("Hello! This function is meant to be used in a Google Chat " +
"Space.");
}
// Responds with a card that prompts the user to add a contact
else {
res.json({
"cardsV2": [{
"cardId": "addContact",
"card": {
"header": {
"title": "Rolodex",
"subtitle": "Manage your contacts!",
"imageUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
"imageType": "CIRCLE"
},
"sections": [
{
"widgets": [
{
"buttonList": {
"buttons": [
{
"text": "Add Contact",
"onClick": {
"action": {
"function": "openDialog",
"interaction": "OPEN_DIALOG"
}
}
}
]
}
}
]
}
]
}
}]
});
}
// Respond to button clicks on attached cards
if (event.type === "CARD_CLICKED") {
if (event.common.invokedFunction === "openDialog") {
openDialog(event);
};
/**
* Opens and starts a dialog that lets users add details about a contact.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "name"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
});
};
}
}
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
/**
* Responds to a MESSAGE event in Google Chat with a card with a button
* that opens a dialog.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in response to a card's button click.
*/
function onMessage(event) {
return {
"cardsV2": [{
"cardId": "addContact",
"card": {
"header": {
"title": "Rolodex",
"subtitle": "Manage your contacts!",
"imageUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
"imageType": "CIRCLE"
},
"sections": [
{
"widgets": [
{
"buttonList": {
"buttons": [
{
"text": "Add Contact",
"onClick": {
"action": {
"function": "openDialog",
"interaction": "OPEN_DIALOG"
}
}
}
]
},
"horizontalAlignment": "CENTER"
}
]
}
]
}
}]
};
}
/**
* Responds to a CARD_CLICKED event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onCardClick(event) {
if (event.common.invokedFunction === "openDialog") {
return openDialog(event);
}
}
/**
* Opens a dialog in Google Chat.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in Google Chat.
*/
function openDialog(event) {
return {
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "contactName"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
};
}
পাইথন
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat that includes the /createContact
slash command by opening a dialog.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
if req.method == 'GET':
return 'Sorry, this function must be called from a Google Chat.'
request = req.get_json(silent=True)
if request.get('type') == 'CARD_CLICKED':
if request.get('common', dict()).get('invokedFunction') == 'open_dialog':
return open_dialog(request)
else:
return {
'cardsV2': [{
'cardId': 'addContact',
'card': {
'header': {
'title': 'Rolodex',
'subtitle': 'Manage your contacts!',
'imageUrl': 'https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png',
'imageType': 'CIRCLE'
},
'sections': [
{
'widgets': [
{
'buttonList': {
'buttons': [
{
'text': 'Add Contact',
'onClick': {
'action': {
'function': 'open_dialog',
'interaction': 'OPEN_DIALOG'
}
}
}
]
}
}
]
}
]
}
}]
}
def open_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a dialog in Google Chat.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Name',
'type': 'SINGLE_LINE',
'name': 'name'
}
},
{
'textInput': {
'label': 'Address',
'type': 'MULTIPLE_LINE',
'name': 'address'
}
},
{
'decoratedText': {
'text': 'Add to favorites',
'switchControl': {
'controlType': 'SWITCH',
'name': 'saveFavorite'
}
}
},
{
'decoratedText': {
'text': 'Merge with existing contacts',
'switchControl': {
'controlType': 'SWITCH',
'name': 'mergeContact',
'selected': True
}
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Next',
'onClick': {
'action': {
'function': 'open_sequential_dialog'
}
}
}
]
}
}
]
}
]
}
}
}
}
}
একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খুলুন
যখন একজন ব্যবহারকারী একটি ডায়ালগ খোলার জন্য কনফিগার করা স্ল্যাশ কমান্ড দিয়ে একটি ডায়ালগ খোলে, তখন আপনার চ্যাট অ্যাপ নিম্নলিখিত তথ্য সহ একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
-
EventType
হলMESSAGE
। -
DialogEventType
হলREQUEST_DIALOG
।
একটি ডায়ালগ খুলতে, নিম্নলিখিতগুলির সাথে সাড়া দিন:
- "টাইপ" এর একটি
ActionResponse
"type": "DIALOG"
। - একটি
DialogAction
যাতে ডায়ালগের একটি JSON বর্ণনা অন্তর্ভুক্ত থাকে।
এই উদাহরণে, একটি চ্যাট অ্যাপ একটি ডায়ালগ খুলে /createContact
স্ল্যাশ কমান্ডে সাড়া দেয়:
Node.js
/**
* Responds to messages that have links whose URLs
* match URL patterns configured for link previews.
*
* @param {Object} event The event object from Chat
* API.
*
* @return {Object} Response from the Chat app
* attached to the message with the previewed link.
*/
exports.onMessage = function onMessage(req, res) {
// Store the Google Chat event as a variable.
const event = req.body;
if (req.method === "GET" || !event.message) {
res.send("Hello! This function is meant to be used in a Google Chat " +
"Space.");
}
// Checks for the presence of event.message.slashCommand.
// If the slash command is "/help", responds with a text message.
// If the slash command is "/createContact", opens a dialog.
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case 1: // /help
res.json({"text": "Contact bot helps you update your address book!"});
case 2: // /createContact
openDialog(event);
}
}
};
/**
* Opens and starts a dialog that lets users add details about a contact.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "name"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
});
};
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
/**
* Responds to a MESSAGE event in Google Chat that includes the /createContact
* slash command by opening a dialog.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in response to a slash command.
*/
function onMessage(event) {
// Checks for the presence of event.message.slashCommand.
// If the slash command is "/help", responds with a text message.
// If the slash command is "/createContact", opens a dialog.
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case 1: // /help
return {"text": "Contact bot helps you update your address book!"}
case 2: // /createContact
return openDialog(event);
}
}
}
/**
* Opens a dialog in Google Chat.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} open a Dialog in Google Chat.
*/
function openDialog(event) {
return {
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "contactName"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
};
}
পাইথন
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat that includes the /createContact
slash command by opening a dialog.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a slash command.
"""
if req.method == 'GET':
return 'Sorry, this function must be called from a Google Chat.'
request = req.get_json(silent=True)
if slash_command := request.get('message', dict()).get('slashCommand'):
command_id = slash_command['commandId']
if command_id == 1:
return {'text': 'Contact bot helps you update your address book!'}
elif command_id == 2:
return open_dialog(request)
def open_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a dialog in Google Chat.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a slash command.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Name',
'type': 'SINGLE_LINE',
'name': 'name'
}
},
{
'textInput': {
'label': 'Address',
'type': 'MULTIPLE_LINE',
'name': 'address'
}
},
{
'decoratedText': {
'text': 'Add to favorites',
'switchControl': {
'controlType': 'SWITCH',
'name': 'saveFavorite'
}
}
},
{
'decoratedText': {
'text': 'Merge with existing contacts',
'switchControl': {
'controlType': 'SWITCH',
'name': 'mergeContact',
'selected': True
}
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Next',
'onClick': {
'action': {
'function': 'open_sequential_dialog'
}
}
}
]
}
}
]
}
]
}
}
}
}
}
অনুক্রমিক ডায়ালগ খুলুন
যখন ব্যবহারকারীর ইন্টারঅ্যাকশনের জন্য একাধিক ডায়ালগের প্রয়োজন হয়, তখন আপনি একটি SUBMIT_DIALOG
DialogEventType
এর প্রতিক্রিয়া হিসাবে ক্রমানুসারে পরবর্তী ডায়ালগ ফিরিয়ে দিয়ে অন্য একটি ডায়ালগ খুলতে পারেন।
ডায়ালগ আপডেটকারী কার্ড বোতামে, একটি ফাংশনের নাম হিসাবে onClick.action.function
রিটার্ন করুন যা পরবর্তী ডায়ালগ খোলে এবং onClick.action.interaction
অনির্দিষ্ট রাখুন।
সমাপ্ত হলে, চ্যাট অ্যাপটি ব্যবহারকারীরা JSON হিসাবে ডায়ালগে প্রবেশ করা মানগুলি গ্রহণ করে। ব্যবহারকারীদের জানান যে তাদের মিথস্ক্রিয়া একটি টেক্সট বা কার্ড বার্তার মাধ্যমে সাড়া দিয়ে সফল হয়েছে।
যখন ব্যবহারকারীরা একটি ডায়ালগে একটি বোতামে ক্লিক করেন, তখন আপনার চ্যাট অ্যাপ নিম্নলিখিত তথ্য সহ একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
-
EventType
হলCARD_CLICKED
। -
DialogEventType
হলSUBMIT_DIALOG
।
এই উদাহরণে, একটি চ্যাট অ্যাপ অন্য ডায়ালগ খোলার মাধ্যমে একটি ডায়ালগ বোতাম ক্লিক থেকে একটি CARD_CLICKED
ইন্টারঅ্যাকশন ইভেন্টে প্রতিক্রিয়া জানায়:
Node.js
// Respond to button clicks on attached cards
if (event.type === "CARD_CLICKED") {
// Open the first dialog.
if (event.common.invokedFunction === "openDialog") {
openDialog(event);
}
// Open the second dialog.
if (event.common.invokedFunction === "openSequentialDialog") {
openSequentialDialog(event);
}
}
/**
* Opens and starts a dialog that lets users add details about a contact.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "name"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
});
};
/**
* Opens a second dialog that lets users add more contact details.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openSequentialDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Notes",
"type": "MULTIPLE_LINE",
"name": "notes"
}
},
{
"selectionInput": {
"type": "RADIO_BUTTON",
"label": "Contact type",
"name": "contactType",
"items": [
{
"text": "Work",
"value": "Work",
"selected": false
},
{
"text": "Personal",
"value": "Personal",
"selected": false
}
]
}
},
{
"buttonList": {
"buttons": [
{
"text": "Submit",
"onClick": {
"action": {
"function": "confirmDialogSuccess",
"parameters": [
{
"key": "confirmDialogSuccess",
"value": "confirmDialogSuccess"
}
]
}
}
}
]
},
"horizontalAlignment": "END"
}
]
}
]
}
}
}
}
});
}
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
/**
* Responds to a CARD_CLICKED event in Google Chat.
*
* @param {Object} event the event object from Google Chat
*/
function onCardClick(event) {
// When a user clicks a card, the Chat app checks to see which function to run.
if (event.common.invokedFunction === "openDialog") {
return openDialog(event);
}
if (event.common.invokedFunction === "openSequentialDialog") {
return openSequentialDialog(event);
}
}
/**
* Opens and starts a dialog that lets users add details about a contact.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openDialog(event) {
return {
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "contactName"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
// Specifies which function to run
// in response to the card click.
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
};
}
/**
* Opens a second dialog that lets users add more contact details.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openSequentialDialog(event) {
return {
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Notes",
"type": "MULTIPLE_LINE",
"name": "notes"
}
},
{
"selectionInput": {
"type": "RADIO_BUTTON",
"label": "Contact type",
"name": "contactType",
"items": [
{
"text": "Work",
"value": "Work",
"selected": false
},
{
"text": "Personal",
"value": "Personal",
"selected": false
}
]
}
},
{
"buttonList": {
"buttons": [
{
"text": "Submit",
"onClick": {
"action": {
// Specifies which function to run
// in response to the card click.
"function": "receiveDialog",
"parameters": [
{
"key": "receiveDialog",
"value": "receiveDialog"
}
]
}
}
}
]
},
"horizontalAlignment": "END"
}
]
}
]
}
}
}
}
};
}
পাইথন
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat that includes the /createContact
slash command by opening a dialog.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
if req.method == 'GET':
return 'Sorry, this function must be called from a Google Chat.'
request = req.get_json(silent=True)
if request.get('type') == 'CARD_CLICKED':
if invoked_function := request.get('common', dict()).get('invokedFunction'):
if invoked_function == 'open_dialog':
return open_dialog(request)
elif invoked_function == 'open_sequential_dialog':
return open_dialog(request)
def open_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a dialog in Google Chat.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Name',
'type': 'SINGLE_LINE',
'name': 'name'
}
},
{
'textInput': {
'label': 'Address',
'type': 'MULTIPLE_LINE',
'name': 'address'
}
},
{
'decoratedText': {
'text': 'Add to favorites',
'switchControl': {
'controlType': 'SWITCH',
'name': 'saveFavorite'
}
}
},
{
'decoratedText': {
'text': 'Merge with existing contacts',
'switchControl': {
'controlType': 'SWITCH',
'name': 'mergeContact',
'selected': True
}
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Next',
'onClick': {
'action': {
'function': 'open_sequential_dialog'
}
}
}
]
}
}
]
}
]
}
}
}
}
}
def open_sequential_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a second dialog that lets users add more contact details.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Notes',
'type': 'MULTIPLE_LINE',
'name': 'notes'
}
},
{
'selectionInput': {
'type': 'RADIO_BUTTON',
'label': 'Contact type',
'name': 'contactType',
'items': [
{
'text': 'Work',
'value': 'Work',
'selected': False
},
{
'text': 'Personal',
'value': 'Personal',
'selected': False
}
]
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Submit',
'onClick': {
'action': {
'function': 'receiveDialog',
'parameters': [
{
'key': 'receiveDialog',
'value': 'receiveDialog'
}
]
}
}
}
]
},
'horizontalAlignment': 'END'
}
]
}
]
}
}
}
}
}
একটি অ্যাপ হোম কার্ড বার্তার প্রতিক্রিয়া হিসাবে একটি ডায়ালগ খুলুন৷
শুধুমাত্র অ্যাপ হোম কার্ড বার্তাগুলির জন্য, একটি ডায়ালগ খুলতে action_response
এর পরিবর্তে render_actions
ব্যবহার করুন:
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
function openDialog() {
return {
render_actions: {
action: {
navigations: [{
update_card: {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "contactName"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}]
}]
}
}]
}
}
}
}
ডায়ালগ থেকে ফর্ম ডেটা গ্রহণ করুন
যখন ব্যবহারকারীরা একটি ডায়ালগের একটি বোতামে ক্লিক করেন, তখন তাদের প্রবেশ করা ডেটা চ্যাট অ্যাপে জমা দেওয়া হয় এবং আপনার চ্যাট অ্যাপ নিম্নলিখিত তথ্য সহ একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
-
EventType
হলCARD_CLICKED
। -
DialogEventType
হলSUBMIT_DIALOG
।
ডায়ালগে ব্যবহারকারীরা যে ডেটা প্রবেশ করে তা ইন্টারঅ্যাকশন ইভেন্টে Event.common.formInputs
হিসাবে উপলব্ধ থাকে, একটি মানচিত্র যেখানে কীগুলি প্রতিটি ডায়ালগ উইজেটে বরাদ্দ করা স্ট্রিং আইডি এবং মানগুলি প্রতিটি উইজেটের জন্য ব্যবহারকারীর ইনপুট উপস্থাপন করে। বিভিন্ন অবজেক্ট বিভিন্ন ইনপুট ডেটা টাইপের প্রতিনিধিত্ব করে। উদাহরণস্বরূপ, Event.common.formInputs.stringInputs
স্ট্রিং ইনপুট উপস্থাপন করে।
যখন একজন ব্যবহারকারী একটি ডায়ালগ জমা দেন, তখন আপনার চ্যাট অ্যাপ চ্যাট থেকে এইরকম একটি ইন্টারঅ্যাকশন ইভেন্ট পায়:
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
}
}
আপনার চ্যাট অ্যাপ event.common.formInputs. NAME .stringInputs.value[0]
, যেখানে NAME হল একটি TextInput
উইজেটের name
ক্ষেত্র।
ডায়ালগ ফর্ম ডেটা পাওয়ার পরে, চ্যাট অ্যাপের একটি ActionResponse
সাথে প্রতিক্রিয়া জানানো উচিত:
- সফল প্রাপ্তি স্বীকার করতে, একটি
ActionResponse
দিয়ে সাড়া দিন যাতে আছে"actionStatus": "OK"
। এটি একটি প্রতিক্রিয়া পোস্ট না করেই ডায়ালগটি বন্ধ করে দেয়৷ - একটি টেক্সট বা একটি কার্ড বার্তার সাথে উত্তর দিতে, একটি
ActionResponse
দিয়ে উত্তর দিন যার একটিResponseType
NEW_MESSAGE
,UPDATE_MESSAGE
, বাUPDATE_USER_MESSAGE_CARDS
। আরও তথ্যের জন্য, একটি ডায়ালগের প্রতিক্রিয়া দেখুন। - একটি ত্রুটি ফেরত দিতে, একটি
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. Any "actionStatus" value other than "OK"
// gets returned as an error.
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 Chat app indicates that it received
// form data from the dialog. An "actionStatus" of "OK" is
// interpreted as code 200, and the dialog closes.
} else {
res.json({
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "OK"
}
}
});
}
}
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
/**
* 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. Any "actionStatus" value other than "OK"
// gets returned as an error.
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 Chat app indicates that it received
// form data from the dialog. An "actionStatus" of "OK" is
// interpreted as code 200, and the dialog closes.
} else {
return {
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "OK"
}
}
};
}
}
পাইথন
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!'
}
}
}
একটি ডায়ালগে সাড়া দিন
আপনি একটি নতুন বার্তা বা একটি বিদ্যমান বার্তার একটি আপডেট সহ একটি ডায়ালগের প্রতিক্রিয়া জানাতে পারেন৷
একটি নতুন বার্তা সহ একটি ডায়ালগের উত্তর দিন৷
একটি নতুন বার্তা সহ একটি ডায়ালগ ফর্ম জমা দেওয়ার প্রতিক্রিয়া জানাতে, চ্যাট অ্যাপটি নতুন বার্তার বিষয়বস্তু নির্দেশ করে মার্কআপ সহ NEW_MESSAGE
টাইপের একটি ActionResponse
প্রদান করে৷ এই প্রতিক্রিয়া পাওয়ার পর, ডায়ালগ বন্ধ হয়ে যায় এবং নতুন বার্তা পোস্ট করা হয়।
নিম্নলিখিত কোডটি একটি ডায়ালগে JSON প্রতিক্রিয়ার একটি উদাহরণ যা একটি নতুন উত্তর বার্তা তৈরি করার জন্য একটি চ্যাট অ্যাপ দ্বারা পাঠানো হয়েছে:
JSON
{
"actionResponse": {
"type": "NEW_MESSAGE",
},
"text": "This message is a reply to a dialog form submission.",
"cardsV2": [
{
"cardId": "reply-card-id",
"card": {
"header": {
"title": "Reply card title"
},
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": "Reply card message"
}
}
]
}
]
}
}
]
}
চ্যাট অ্যাপটি একটি টেক্সট বা কার্ড বার্তার সাথে অ্যাসিঙ্ক্রোনাসভাবে উত্তর দিতে পারে।
একটি আপডেট করা বার্তা সহ একটি ডায়ালগের উত্তর দিন৷
আপনি যখন একটি আপডেট করা বার্তা সহ একটি ডায়ালগের উত্তর দেন, তখন আপনি একটি বিদ্যমান চ্যাট অ্যাপ বার্তা আপডেট করতে পারেন বা একটি লিঙ্ক প্রিভিউ আপডেট করতে পারেন।
চ্যাট অ্যাপ বার্তা
চ্যাট অ্যাপ দ্বারা প্রেরিত একটি বিদ্যমান বার্তার আপডেট সহ একটি ডায়ালগ ফর্ম জমা দেওয়ার প্রতিক্রিয়া জানাতে, এটি UPDATE_MESSAGE
টাইপের একটি ActionResponse
প্রদান করে। প্রতিক্রিয়াটিতে মার্কআপ অন্তর্ভুক্ত থাকে যা আপডেট করা বার্তার বিষয়বস্তু নির্দেশ করে। এই প্রতিক্রিয়া পাওয়ার পরে, ডায়ালগটি বন্ধ হয়ে যায় এবং বার্তাটি নতুন সামগ্রীর সাথে আপডেট করা হয়।
নিম্নলিখিত কোডটি একটি ডায়ালগে JSON প্রতিক্রিয়ার একটি উদাহরণ যা একটি চ্যাট অ্যাপের মাধ্যমে একটি বিদ্যমান চ্যাট অ্যাপ বার্তা আপডেট করার জন্য পাঠানো হয়:
JSON
{
"actionResponse": {
"type": "UPDATE_MESSAGE",
},
"text": "This message has been updated with new content in response to a dialog form submission.",
"cardsV2": [
{
"cardId": "updated-card-id",
"card": {
"header": {
"title": "Updated card title"
},
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": "Updated card message"
}
}
]
}
]
}
}
]
}
Chat অ্যাপটি Google Chat API ব্যবহার করে অ্যাসিঙ্ক্রোনাসভাবে একটি চ্যাট অ্যাপ বার্তা আপডেট করতে পারে।
লিঙ্ক প্রিভিউ
ডায়ালগ ফর্ম জমা দেওয়ার প্রতিক্রিয়া হিসাবে নতুন বিষয়বস্তুর সাথে লিঙ্ক প্রিভিউ আপডেট করতে, চ্যাট অ্যাপ UPDATE_USER_MESSAGE_CARDS
ধরনের একটি ActionResponse
প্রদান করে। প্রতিক্রিয়াটিতে নতুন কার্ড বার্তাগুলির জন্য মার্কআপ অন্তর্ভুক্ত রয়েছে যেগুলির সাথে লিঙ্কের পূর্বরূপগুলি আপডেট করা হয়েছে৷ এই প্রতিক্রিয়া পাওয়ার পরে, ডায়ালগ বন্ধ হয়ে যায় এবং লিঙ্কের পূর্বরূপগুলি নতুন কার্ড বার্তাগুলির সাথে আপডেট করা হয়৷
নিম্নলিখিত উদাহরণ JSON প্রতিক্রিয়া একটি নতুন কার্ড বার্তা সহ একটি লিঙ্ক পূর্বরূপ আপডেট করে:
JSON
{
"actionResponse": "UPDATE_USER_MESSAGE_CARDS",
"cardsV2": [
{
"cardId" : "updated-card-id",
"card" : {
"header": {
"title": "Updated card title"
},
"sections": [
{
"widgets" : [
{
"textParagraph": {
"text": "Updated card message"
}
}
]
}
]
}
}
]
}
অ্যাপ হোম কার্ড বার্তাগুলির জন্য একটি ডায়ালগের উত্তর দিন
শুধুমাত্র অ্যাপ হোম কার্ড বার্তাগুলির জন্য, একটি ডায়ালগ বন্ধ করার দুটি ভিন্ন উপায় রয়েছে:
-
CLOSE_DIALOG
: ডায়ালগ বন্ধ করে এবং অ্যাপ হোম কার্ড বার্তায় ফিরে আসে। CLOSE_DIALOG_AND_EXECUTE
: ডায়ালগ বন্ধ করে এবং অ্যাপ হোম কার্ড বার্তা রিফ্রেশ করে।
পাইথন
নিম্নলিখিত কোড নমুনাটি একটি ডায়ালগ বন্ধ করতে এবং অ্যাপ হোম কার্ড বার্তায় ফিরে যেতে CLOSE_DIALOG
ব্যবহার করে:
def close_dialog():
"""Handles dismiss dialog request from Chat."""
return {
'render_actions': {
'action': {
'navigations': [{
'end_navigation': {'action': 'CLOSE_DIALOG'}
}]
}
}
}
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
নিম্নলিখিত কোড নমুনাটি একটি ডায়ালগ বন্ধ করতে এবং অ্যাপ হোম কার্ড বার্তায় ফিরে যেতে CLOSE_DIALOG
ব্যবহার করে:
function closeDialog(event) {
return {
render_actions: {
action:{
navigations:[{
end_navigation:{
action: "CLOSE_DIALOG"
}
}]
}
}
};
}
সম্পূর্ণ উদাহরণ: রোলোডেক্স যোগাযোগ পরিচালনা করে চ্যাট অ্যাপ
এই উদাহরণে, একটি চ্যাট অ্যাপ একটি ডায়ালগ খোলে যাতে ব্যবহারকারী একটি পরিচিতি সম্পর্কে বিশদ বিবরণ যোগ করতে পারেন, যেমন নাম, ইমেল এবং ঠিকানা:
Node.js
/**
* Responds to messages that have links whose URLs
* match URL patterns configured for link previews.
*
* @param {Object} event The event object from Chat
* API.
*
* @return {Object} Response from the Chat app
* attached to the message with the previewed link.
*/
exports.onMessage = function onMessage(req, res) {
// Store the Google Chat event as a variable.
const event = req.body;
if (req.method === "GET" || !event.message) {
res.send("Hello! This function is meant to be used in a Google Chat " +
"Space.");
}
// Checks for the presence of event.message.slashCommand.
// If the slash command is "/help", responds with a text message.
// If the slash command is "/createContact", opens a dialog.
if (event.message.slashCommand) {
switch (event.message.slashCommand.commandId) {
case 1: // /help
res.json({"text": "Contact bot helps you update your address book!"});
case 2: // /createContact
openDialog(event);
}
}
// If the Chat app doesn"t detect a slash command, it responds
// with a card that prompts the user to add a contact
else {
res.json({
"cardsV2": [{
"cardId": "addContact",
"card": {
"header": {
"title": "Rolodex",
"subtitle": "Manage your contacts!",
"imageUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
"imageType": "CIRCLE"
},
"sections": [
{
"widgets": [
{
"buttonList": {
"buttons": [
{
"text": "Add Contact",
"onClick": {
"action": {
"function": "openDialog",
"interaction": "OPEN_DIALOG"
}
}
}
]
}
}
]
}
]
}
}]
});
}
// Respond to button clicks on attached cards
if (event.type === "CARD_CLICKED") {
if (event.common.invokedFunction === "openDialog") {
openDialog(event);
}
if (event.common.invokedFunction === "openSequentialDialog") {
openSequentialDialog(event);
}
if (event.common.invokedFunction === "confirmDialogSuccess") {
confirmDialogSuccess(event);
}
}
};
/**
* Opens and starts a dialog that lets users add details about a contact.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Name",
"type": "SINGLE_LINE",
"name": "name"
}
},
{
"textInput": {
"label": "Address",
"type": "MULTIPLE_LINE",
"name": "address"
}
},
{
"decoratedText": {
"text": "Add to favorites",
"switchControl": {
"controlType": "SWITCH",
"name": "saveFavorite"
}
}
},
{
"decoratedText": {
"text": "Merge with existing contacts",
"switchControl": {
"controlType": "SWITCH",
"name": "mergeContact",
"selected": true
}
}
},
{
"buttonList": {
"buttons": [
{
"text": "Next",
"onClick": {
"action": {
"function": "openSequentialDialog"
}
}
}
]
}
}
]
}
]
}
}
}
}
});
};
/**
* Opens a second dialog that lets users add more contact details.
*
* @param {object} event the event object from Google Chat.
*
* @return {object} open a dialog.
*/
function openSequentialDialog(event) {
res.json({
"action_response": {
"type": "DIALOG",
"dialog_action": {
"dialog": {
"body": {
"sections": [
{
"header": "Add new contact",
"widgets": [
{
"textInput": {
"label": "Notes",
"type": "MULTIPLE_LINE",
"name": "notes"
}
},
{
"selectionInput": {
"type": "RADIO_BUTTON",
"label": "Contact type",
"name": "contactType",
"items": [
{
"text": "Work",
"value": "Work",
"selected": false
},
{
"text": "Personal",
"value": "Personal",
"selected": false
}
]
}
},
{
"buttonList": {
"buttons": [
{
"text": "Submit",
"onClick": {
"action": {
"function": "confirmDialogSuccess",
"parameters": [
{
"key": "confirmDialogSuccess",
"value": "confirmDialogSuccess"
}
]
}
}
}
]
},
"horizontalAlignment": "END"
}
]
}
]
}
}
}
}
});
}
/**
* 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. Any "actionStatus" value other than "OK"
// gets returned as an error.
if (event.common.formInputs.contactName.stringInputs.value[0] === "") {
res.json({
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "Don't forget to name your new contact!"
}
}
});
// Otherwise the Chat app indicates that it received
// form data from the dialog. An "actionStatus" of "OK" is
// interpreted as code 200, and the dialog closes.
} else {
res.json({
"actionResponse": {
"type": "DIALOG",
"dialogAction": {
"actionStatus": "OK"
}
}
});
}
}
অ্যাপস স্ক্রিপ্ট
এই উদাহরণ কার্ড JSON ফেরত দিয়ে একটি কার্ড বার্তা পাঠায়। আপনি Apps স্ক্রিপ্ট কার্ড পরিষেবাও ব্যবহার করতে পারেন৷
পাইথন
from typing import Any, Mapping
import flask
import functions_framework
@functions_framework.http
def main(req: flask.Request) -> Mapping[str, Any]:
"""Responds to a MESSAGE event in Google Chat that includes the /createContact
slash command by opening a dialog.
Args:
req (flask.Request): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
if req.method == 'GET':
return 'Sorry, this function must be called from a Google Chat.'
request = req.get_json(silent=True)
if request.get('type') == 'CARD_CLICKED':
invoked_function = request.get('common', dict()).get('invokedFunction')
if invoked_function == 'open_dialog':
return open_dialog(request)
elif invoked_function == 'open_sequential_dialog':
return open_dialog(request)
elif invoked_function == "receive_dialog":
return receive_dialog(request)
else:
return {
'cardsV2': [{
'cardId': 'addContact',
'card': {
'header': {
'title': 'Rolodex',
'subtitle': 'Manage your contacts!',
'imageUrl': 'https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png',
'imageType': 'CIRCLE'
},
'sections': [
{
'widgets': [
{
'buttonList': {
'buttons': [
{
'text': 'Add Contact',
'onClick': {
'action': {
'function': 'open_dialog',
'interaction': 'OPEN_DIALOG'
}
}
}
]
}
}
]
}
]
}
}]
}
def open_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a dialog in Google Chat.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Name',
'type': 'SINGLE_LINE',
'name': 'name'
}
},
{
'textInput': {
'label': 'Address',
'type': 'MULTIPLE_LINE',
'name': 'address'
}
},
{
'decoratedText': {
'text': 'Add to favorites',
'switchControl': {
'controlType': 'SWITCH',
'name': 'saveFavorite'
}
}
},
{
'decoratedText': {
'text': 'Merge with existing contacts',
'switchControl': {
'controlType': 'SWITCH',
'name': 'mergeContact',
'selected': True
}
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Next',
'onClick': {
'action': {
'function': 'open_sequential_dialog'
}
}
}
]
}
}
]
}
]
}
}
}
}
}
def open_sequential_dialog(request: Mapping[str, Any]) -> Mapping[str, Any]:
"""Opens a second dialog that lets users add more contact details.
Args:
request (Mapping[str, Any]): the event object from Chat API.
Returns:
Mapping[str, Any]: open a Dialog in response to a card's button click.
"""
return {
'action_response': {
'type': 'DIALOG',
'dialog_action': {
'dialog': {
'body': {
'sections': [
{
'header': 'Add new contact',
'widgets': [
{
'textInput': {
'label': 'Notes',
'type': 'MULTIPLE_LINE',
'name': 'notes'
}
},
{
'selectionInput': {
'type': 'RADIO_BUTTON',
'label': 'Contact type',
'name': 'contactType',
'items': [
{
'text': 'Work',
'value': 'Work',
'selected': False
},
{
'text': 'Personal',
'value': 'Personal',
'selected': False
}
]
}
},
{
'buttonList': {
'buttons': [
{
'text': 'Submit',
'onClick': {
'action': {
'function': 'receive_dialog',
'parameters': [
{
'key': 'receiveDialog',
'value': 'receiveDialog'
}
]
}
}
}
]
},
'horizontalAlignment': 'END'
}
]
}
]
}
}
}
}
}
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 event.get('common', dict()) \
.get('formInputs', dict()).get('contactName', dict()) \
.get('stringInputs').get('value', list()):
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'actionStatus': 'OK'
}
}
}
else:
return {
'actionResponse': {
'type': 'DIALOG',
'dialogAction': {
'actionStatus': "Don't forget to name your new contact!"
}
}
}
সমস্যা সমাধান
যখন একটি Google চ্যাট অ্যাপ বা কার্ড একটি ত্রুটি ফেরত দেয়, তখন চ্যাট ইন্টারফেস "কিছু ভুল হয়েছে" বলে একটি বার্তা দেখায়। অথবা "আপনার অনুরোধ প্রক্রিয়া করতে অক্ষম।" কখনও কখনও চ্যাট UI কোনও ত্রুটি বার্তা প্রদর্শন করে না, তবে চ্যাট অ্যাপ বা কার্ড একটি অপ্রত্যাশিত ফলাফল তৈরি করে; উদাহরণস্বরূপ, একটি কার্ড বার্তা প্রদর্শিত নাও হতে পারে৷
যদিও একটি ত্রুটি বার্তা চ্যাট UI-তে প্রদর্শিত নাও হতে পারে, বর্ণনামূলক ত্রুটি বার্তা এবং লগ ডেটা উপলব্ধ রয়েছে যাতে আপনি যখন চ্যাট অ্যাপগুলির জন্য ত্রুটি লগিং চালু থাকে তখন ত্রুটিগুলি ঠিক করতে সহায়তা করে৷ দেখা, ডিবাগিং এবং ত্রুটিগুলি সংশোধন করতে সহায়তার জন্য, Google Chat ত্রুটিগুলি সমস্যা সমাধান এবং ঠিক করুন দেখুন৷
সম্পর্কিত বিষয়
- পূর্বরূপ লিঙ্ক
- স্ল্যাশ কমান্ড সেট আপ করুন এবং প্রতিক্রিয়া জানান
- ব্যবহারকারীদের দ্বারা ইনপুট তথ্য প্রক্রিয়া