Google চ্যাট অ্যাপ হিসেবে স্ল্যাশ কমান্ডে সাড়া দিন

এই পৃষ্ঠাটি ব্যাখ্যা করে যে কীভাবে আপনার Google Chat অ্যাপের জন্য স্ল্যাশ কমান্ড সেট আপ করতে হয় এবং প্রতিক্রিয়া জানাতে হয়।

একটি স্ল্যাশ কমান্ড হল একটি সাধারণ উপায় যা ব্যবহারকারীরা একটি চ্যাট অ্যাপের সাথে যোগাযোগ করে। স্ল্যাশ কমান্ডগুলি ব্যবহারকারীদের একটি চ্যাট অ্যাপের মূল বৈশিষ্ট্যগুলি আবিষ্কার করতে এবং ব্যবহার করতে সহায়তা করে।

একটি স্ল্যাশ কমান্ড ব্যবহার করতে, ব্যবহারকারীরা একটি স্ল্যাশ ( / ) এবং তারপর একটি ছোট পাঠ্য কমান্ড টাইপ করে, যেমন /about সম্পর্কে চ্যাট অ্যাপ সম্পর্কে তথ্য পেতে। ব্যবহারকারীরা Google চ্যাটে একটি স্ল্যাশ টাইপ করে উপলব্ধ স্ল্যাশ কমান্ডগুলি আবিষ্কার করতে পারে, যা একটি উইন্ডো প্রদর্শন করে যা চ্যাট অ্যাপের জন্য উপলব্ধ কমান্ডগুলি তালিকাভুক্ত করে:

স্ল্যাশ কমান্ড উইন্ডো
চিত্র 1 : ব্যবহারকারীরা Google চ্যাটে স্ল্যাশ টাইপ করলে যে উইন্ডোটি প্রদর্শিত হয়।

যখন একজন ব্যবহারকারী একটি বার্তা পাঠায় যাতে একটি স্ল্যাশ কমান্ড থাকে, তখন বার্তাটি শুধুমাত্র ব্যবহারকারী এবং চ্যাট অ্যাপের কাছে দৃশ্যমান হয়।

আপনার স্ল্যাশ কমান্ড সেট আপ করা উচিত কিনা তা সিদ্ধান্ত নিতে এবং কীভাবে ব্যবহারকারীর ইন্টারঅ্যাকশন ডিজাইন করতে হয় তা বুঝতে, Google চ্যাট ডিজাইন নীতিগুলি দেখুন।

প্রাক-প্রয়োজনীয়

Node.js

অ্যাপস স্ক্রিপ্ট

পাইথন

একটি স্ল্যাশ কমান্ড সেট আপ করুন

এই বিভাগটি ব্যাখ্যা করে কিভাবে একটি স্ল্যাশ কমান্ড সেট আপ করতে নিম্নলিখিত পদক্ষেপগুলি সম্পূর্ণ করতে হয়:

  1. আপনার স্ল্যাশ কমান্ডের জন্য একটি নাম তৈরি করুন
  2. Google Chat API-এ স্ল্যাশ কমান্ড কনফিগার করুন

আপনার স্ল্যাশ কমান্ডের নাম দিন

একটি স্ল্যাশ কমান্ডের নাম হল ব্যবহারকারীরা চ্যাট অ্যাপটি চালু করতে একটি চ্যাট বার্তায় যা টাইপ করে। কমান্ডটি কীভাবে ব্যবহার করবেন সে সম্পর্কে ব্যবহারকারীদের আরও প্রম্পট করতে নামের নীচে একটি সংক্ষিপ্ত বিবরণও উপস্থিত হয়:

স্ল্যাশ কমান্ডের নাম এবং বিবরণ
চিত্র 2 : একটি স্ল্যাশ কমান্ডের নাম এবং বিবরণ।

আপনার স্ল্যাশ কমান্ডের জন্য একটি নাম এবং বিবরণ নির্বাচন করার সময়, নিম্নলিখিত সুপারিশগুলি বিবেচনা করুন:

  • আপনার স্ল্যাশ কমান্ডের নাম দিতে:

    • ব্যবহারকারীর কাছে কমান্ডগুলিকে পরিষ্কার এবং সহজ করতে সংক্ষিপ্ত, বর্ণনামূলক এবং কার্যকরী শব্দ বা বাক্যাংশ ব্যবহার করুন। উদাহরণস্বরূপ, /createAReminder বলার পরিবর্তে, /remindMe ব্যবহার করুন।
    • যদি আপনার কমান্ডে একাধিক শব্দ থাকে, প্রথম শব্দের জন্য সমস্ত ছোট হাতের অক্ষর ব্যবহার করে এবং তারপর অতিরিক্ত শব্দের প্রথম অক্ষর বড় করে ব্যবহারকারীদের কমান্ড পড়তে সাহায্য করুন। উদাহরণস্বরূপ, /updatecontact এর পরিবর্তে, /updateContact ব্যবহার করুন।
    • আপনার কমান্ডের জন্য একটি অনন্য বা সাধারণ নাম ব্যবহার করবেন কিনা তা বিবেচনা করুন। যদি আপনার কমান্ড একটি সাধারণ মিথস্ক্রিয়া বা বৈশিষ্ট্য বর্ণনা করে, আপনি একটি সাধারণ নাম ব্যবহার করতে পারেন যা ব্যবহারকারীরা চিনতে পারে এবং আশা করে, যেমন /settings বা /feedback । অন্যথায়, অনন্য কমান্ডের নাম ব্যবহার করার চেষ্টা করুন, কারণ আপনার কমান্ডের নাম যদি অন্যান্য চ্যাট অ্যাপের জন্য একই হয়, তাহলে ব্যবহারকারীকে অবশ্যই আপনার খুঁজে পেতে এবং ব্যবহার করতে অনুরূপ কমান্ডের মাধ্যমে ফিল্টার করতে হবে।
  • আপনার স্ল্যাশ কমান্ড বর্ণনা করতে:

    • বর্ণনা সংক্ষিপ্ত এবং পরিষ্কার রাখুন যাতে ব্যবহারকারীরা জানতে পারে যে তারা যখন কমান্ডটি ব্যবহার করবে তখন কী আশা করা উচিত।
    • কমান্ডের জন্য কোন ফর্ম্যাটিং প্রয়োজনীয়তা আছে কিনা তা ব্যবহারকারীদের জানান। উদাহরণ স্বরূপ, আপনি যদি একটি /remindMe কমান্ড তৈরি করেন যার জন্য আর্গুমেন্ট টেক্সট প্রয়োজন হয়, তাহলে বর্ণনাটিকে এমন কিছুতে সেট করুন যেমন Remind me to do [something] at [time] .
    • চ্যাট অ্যাপটি স্পেসের প্রত্যেকের কাছে বা গোপনে যে ব্যবহারকারীর কমান্ড আহ্বান করে তাদের উত্তর দেয় কিনা তা ব্যবহারকারীদের জানান। উদাহরণস্বরূপ, স্ল্যাশ কমান্ডের জন্য /about , আপনি এটিকে Learn about this app (Only visible to you) হিসাবে বর্ণনা করতে পারেন। একটি স্ল্যাশ কমান্ডে ব্যক্তিগতভাবে প্রতিক্রিয়া জানাতে, একটি ব্যক্তিগত বার্তার সাথে প্রতিক্রিয়া বিভাগটি দেখুন।

Google Chat API-এ আপনার স্ল্যাশ কমান্ড কনফিগার করুন

একটি স্ল্যাশ কমান্ড তৈরি করতে, আপনাকে Google Chat API-এর জন্য আপনার চ্যাট অ্যাপের কনফিগারেশনে কমান্ড সম্পর্কে তথ্য উল্লেখ করতে হবে।

Google Chat API-এ একটি স্ল্যাশ কমান্ড কনফিগার করতে, নিম্নলিখিত পদক্ষেপগুলি সম্পূর্ণ করুন:

  1. Google ক্লাউড কনসোলে, মেনু > APIs & Services > Enabled APIs & Services > Google Chat API-এ ক্লিক করুন

    Google Chat API পৃষ্ঠায় যান

  2. কনফিগারেশন ক্লিক করুন।

  3. স্ল্যাশ কমান্ডের অধীনে, একটি স্ল্যাশ কমান্ড যোগ করুন ক্লিক করুন।

  4. কমান্ডের জন্য একটি নাম, কমান্ড আইডি এবং বিবরণ লিখুন:

    1. নাম: কমান্ডের জন্য প্রদর্শনের নাম, এবং ব্যবহারকারীরা আপনার অ্যাপ চালু করতে কী টাইপ করে। একটি স্ল্যাশ দিয়ে শুরু করতে হবে, শুধুমাত্র পাঠ্য থাকতে হবে এবং 50টি অক্ষর পর্যন্ত হতে পারে৷
    2. বর্ণনা: টেক্সট যেটি বর্ণনা করে কিভাবে কমান্ডটি ব্যবহার এবং ফরম্যাট করতে হয়। বর্ণনা 50টি অক্ষর পর্যন্ত হতে পারে।
    3. কমান্ড আইডি: 1 থেকে 1000 পর্যন্ত একটি সংখ্যা যা আপনার চ্যাট অ্যাপ স্ল্যাশ কমান্ড চিনতে এবং একটি প্রতিক্রিয়া ফেরাতে ব্যবহার করে।
  5. ঐচ্ছিক: আপনি যদি চান যে আপনার চ্যাট অ্যাপ একটি ডায়ালগের সাথে কমান্ডের প্রতিক্রিয়া জানাতে, একটি ডায়ালগ খুলুন চেকবক্স নির্বাচন করুন।

  6. Save এ ক্লিক করুন।

স্ল্যাশ কমান্ডটি এখন চ্যাট অ্যাপের জন্য কনফিগার করা হয়েছে।

একটি স্ল্যাশ কমান্ডে সাড়া দিন

যখন ব্যবহারকারীরা একটি চ্যাট বার্তা তৈরি করে যাতে একটি স্ল্যাশ কমান্ড থাকে, তখন আপনার চ্যাট অ্যাপ একটি MESSAGE ইন্টারঅ্যাকশন ইভেন্ট পায়। ইভেন্ট পেলোডে স্ল্যাশ কমান্ড এবং slashCommand slashCommandMetadata ক্ষেত্র সহ তথ্য রয়েছে। আপনি কমান্ড আইডি সনাক্ত করতে এবং একটি কাস্টম প্রতিক্রিয়া ফেরত দিতে এই ক্ষেত্রগুলি ব্যবহার করুন৷

নিম্নলিখিত উদাহরণটি একটি MESSAGE ইন্টারঅ্যাকশন ইভেন্টের জন্য JSON পেলোড দেখায় যাতে স্ল্যাশ কমান্ড /vote অন্তর্ভুক্ত থাকে:

    {
      ...
      "message": {
        ...
        "text": "/vote yes",
        "argumentText": " yes",
        "slashCommand": {
          "commandId": 2
        },
        "annotations": [
          {
            "length": 5,
            "startIndex": 0,
            "type": "SLASH_COMMAND",
            "slashCommand": {
              "commandName":"/vote",
              "commandId":1,
              "type": "INVOKE",
              "bot": {
                "avatarUrl": "https://www.example.com/images/vote-app-icon.png",
                "displayName": "Voter Chat App",
                "name": "users/1234567890987654321",
                "type": "BOT"
              }
            }
          }
        ]
      }
    }

একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া জানাতে, আপনি শনাক্ত করতে পারেন যে slashCommand ক্ষেত্রটি ইভেন্ট পেলোডে উপস্থিত আছে কিনা এবং যদি তাই হয়, কমান্ডে একটি প্রতিক্রিয়া ফেরত দিন। নিম্নলিখিত কোড নমুনা দেখায় কিভাবে একটি MESSAGE ইন্টারঅ্যাকশন ইভেন্টে প্রতিক্রিয়া জানাতে হয় যাতে একটি স্ল্যাশ কমান্ড রয়েছে:

Node.js

/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} function in response to a slash command.
*/

exports.onMessage = function onMessage(req, res) {

  // Stores the Google Chat event as a variable.
  var event = req.body;

  // Checks for the presence of event.message.slashCommand.
  if (event.message.slashCommand) {
    switch (event.message.slashCommand.commandId) {
      case ID: // The ID for your slash command
        res.runFunction; // The response to the slash command.
    }
  }

অ্যাপস স্ক্রিপ্ট

/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Chat API.
*
* @return {object} function in response to a slash command.
*/

function onMessage(event) {

  // Checks for the presence of event.message.slashCommand
  if (event.message.slashCommand) {
    switch (event.message.slashCommand.commandId) {
      case ID: // The ID for your slash command
        return runFunction; // The response to the slash command.
    }
  }
}

পাইথন

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 a slash command.

  Args:
      req (flask.Request): the event object from Chat API.

  Returns:
      Mapping[str, Any]: function 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 == ID:
      return runFunction

কোড ব্যবহার করতে, নিম্নলিখিত প্রতিস্থাপন করুন:

ঐচ্ছিক: একটি ব্যক্তিগত বার্তা দিয়ে সাড়া দিন

যে বার্তাগুলিতে স্ল্যাশ কমান্ড রয়েছে তা কেবলমাত্র সেই ব্যবহারকারীর কাছে দৃশ্যমান হয় যে বার্তাটি পাঠিয়েছে এবং যে চ্যাট অ্যাপটি কমান্ড গ্রহণ করে। আপনি যদি আপনার চ্যাট অ্যাপটিকে একাধিক ব্যক্তির সাথে স্পেসগুলিতে যোগ করার জন্য কনফিগার করে থাকেন, তাহলে আপনি ব্যবহারকারী এবং চ্যাট অ্যাপের মধ্যে ইন্টারঅ্যাকশন ব্যক্তিগত রাখতে ব্যক্তিগতভাবে স্ল্যাশ কমান্ডের প্রতিক্রিয়া বিবেচনা করতে পারেন।

উদাহরণস্বরূপ, যদি একটি দল এমন একটি চ্যাট অ্যাপ ব্যবহার করে যা একটি গ্রাহক সহায়তা পরিষেবা পরিচালনা করে, ব্যবহারকারীরা তাদের জন্য বরাদ্দ করা সমর্থন কেসগুলি দেখতে /myCases এর মতো একটি স্ল্যাশ কমান্ড ব্যবহার করতে পারে। যদি দলটি একটি স্পেসে চ্যাট অ্যাপটি যোগ করে, তবে যে ব্যবহারকারী স্ল্যাশ কমান্ডটি স্পেসে ব্যবহার করেন তিনি চান যে চ্যাট অ্যাপ শুধুমাত্র তাদের প্রতিক্রিয়া জানাতে পারে। স্পেসের প্রত্যেকের কাছে ব্যবহারকারীর সহায়তার কেস পোস্ট করা এড়াতে, চ্যাট অ্যাপ ব্যক্তিগতভাবে প্রতিক্রিয়া জানাতে পারে।

ব্যক্তিগতভাবে একটি স্ল্যাশ কমান্ডের প্রতিক্রিয়া জানাতে, Google চ্যাট ব্যবহারকারীদের ব্যক্তিগত বার্তা পাঠান দেখুন।

সম্পূর্ণ উদাহরণ: Rolodex Chat অ্যাপ ব্যবহার করে পরিচিতি সেট আপ করুন

নিম্নলিখিত উদাহরণটি একটি চ্যাট অ্যাপ দেখায় যা নিম্নলিখিত স্ল্যাশ কমান্ডগুলিতে সাড়া দেয়:

  • /help কমান্ডটি একটি পাঠ্য বার্তা ফেরত দেয় যা ব্যাখ্যা করে যে কীভাবে চ্যাট অ্যাপের সাথে সমর্থন পেতে হয়। কমান্ড আইডি 1 এ সেট করা হয়েছে।
  • /createContact কমান্ড একটি ডায়ালগ খোলে যেখানে ব্যবহারকারীরা একটি পরিচিতি সম্পর্কে বিশদ লিখতে পারেন। কমান্ড আইডি 2 এ সেট করা হয়েছে।

আপনি এই নমুনাটি চালানোর আগে, Google Chat API-এ স্ল্যাশ কমান্ডগুলি কনফিগার করার পদক্ষেপগুলি অনুসরণ করুন৷

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
        res.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") {
      res.openDialog(event);
    }

    if (event.common.invokedFunction === "openSequentialDialog") {
      res.openSequentialDialog(event);
    }

    if (event.common.invokedFunction === "confirmDialogSuccess") {
      res.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.
  if (event.common.formInputs.contactName.stringInputs.value[0] === "") {
    return {
      "actionResponse": {
        "type": "DIALOG",
        "dialogAction": {
          "actionStatus": {
            "statusCode": "OK",
            "userFacingMessage": "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/dialogs/rolodex.gs
/**
* Responds to a MESSAGE event in Google Chat.
*
* @param {Object} event the event object from Chat API.
*
* @return {Object} open a Dialog in response to a slash command
* or a card"s button click.
*/
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);
    }
  }

  // If the Chat app doesn"t detect a slash command, it responds
  // with a card that prompts the user to add a contact
  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": "openDialog",
                            "interaction": "OPEN_DIALOG"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          ]
        }
      }]

    };
  }
}

/**
* 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);
  }

  if (event.common.invokedFunction === "openSequentialDialog") {
    const contactName = fetchFormValue(event, "contactName");
    const address = fetchFormValue(event, "address");
    return openSequentialDialog(contactName, address);
  }

  if (event.common.invokedFunction === "receiveDialog") {
    const parameters = event.common.parameters;
    parameters["contactType"] = fetchFormValue(event, "contactType");
    parameters["notes"] = fetchFormValue(event, "notes");
    return receiveDialog(parameters);
  }
}

/**
 * Extracts form input value for a given widget
 * 
 * @param {Object} event the event object from Google Chat
 * @param {String} widgetName the widget name
 * @returns the form input value for the widget
 */
function fetchFormValue(event, widgetName) {
  const widget = event.common.formInputs[widgetName];
  if (widget) {
    return widget[""]["stringInputs"]["value"][0];
  }
}

/**
* Opens and starts a dialog that lets users add details about a contact.
*
*
* @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": {
                              "function": "openSequentialDialog"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }
      }
    }
  };
}

/**
* Opens a second dialog that lets users add more contact details.
*
* @param {String} contactName the contact name from the previous dialog.
* @param {String} address the address from the previous dialog.
*
* @return {Object} open a dialog.
*/
function openSequentialDialog(contactName, address) {
  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": "contactName",
                                  "value": contactName
                                },
                                {
                                  "key": "address",
                                  "value": address
                                }
                              ]
                            }
                          }
                        }
                      ]
                    },
                    "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} parameters the form input values.
*
* @return {Object} open a Dialog in Google Chat.
*/
function receiveDialog(parameters) {

  // Checks to make sure the user entered a name
  // in a dialog. If no name value detected, returns
  // an error message.
  if (!parameters.contactName) {
    return {
      "actionResponse": {
        "type": "DIALOG",
        "dialogAction": {
          "actionStatus": {
            "statusCode": "INVALID_ARGUMENT",
            "userFacingMessage": "Don't forget to name your new contact!"
          }
        }
      }
    };

    // Otherwise the Chat 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": {
            "statusCode": "OK",
            "userFacingMessage": "Success " + JSON.stringify(parameters)
          }
        }
      }
    };
  }
}

পাইথন

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!"
        }
      }
    }