Google Chat से मीटिंग शेड्यूल करना

कोडिंग का लेवल: बीच के लेवल वाला
कुल समय: 25 मिनट
प्रोजेक्ट टाइप: Google Chat ऐप्लिकेशन

मकसद

  • समझें कि समाधान क्या है.
  • जानें कि Apps Script सेवा के अंदर क्या काम करती है.
  • अपना एनवायरमेंट सेट अप करें.
  • स्क्रिप्ट सेट अप करें.
  • स्क्रिप्ट चलाएं.

इस सलूशन के बारे में जानकारी

Google Calendar में, डायरेक्ट मैसेज (DM) या Google Chat के स्पेस में से मीटिंग शेड्यूल करें. आपके पास मीटिंग के लिए खास जानकारी सेट करने का विकल्प है. जैसे, विषय, शुरू होने का समय या अवधि. इसके अलावा, तुरंत मीटिंग शेड्यूल करने के लिए डिफ़ॉल्ट सेटिंग का इस्तेमाल भी किया जा सकता है.

मीटिंग शेड्यूलर के चैट ऐप्लिकेशन का डायलॉग इंटरफ़ेस

यह कैसे काम करता है

Chat ऐप्लिकेशन स्क्रिप्ट, उपयोगकर्ताओं से मीटिंग की जानकारी पाने और Calendar इवेंट शेड्यूल करने के लिए, स्लैश कमांड और डायलॉग का इस्तेमाल करती है. स्क्रिप्ट में मीटिंग की डिफ़ॉल्ट सेटिंग शामिल होती हैं, जिन्हें आपकी ज़रूरत के हिसाब से बनाया जा सकता है.

Apps Script सेवाएं

यह समाधान इन सेवाओं का इस्तेमाल करता है:

  • कैलेंडर सेवा–इसमें दी गई मीटिंग की जानकारी से कैलेंडर इवेंट बनाया जाता है.
  • बुनियादी सेवा–यह स्क्रिप्ट के टाइम ज़ोन की जानकारी पाने के लिए, Session क्लास का इस्तेमाल करती है. इवेंट शेड्यूल करते समय Calendar, इस टाइम ज़ोन का इस्तेमाल करता है.
  • उपयोगिताएं–यह सेवा, कैलेंडर इवेंट की तारीख को फ़ॉर्मैट करती है. साथ ही, इवेंट का यूआरएल पाने के लिए इवेंट आईडी को कोड में बदल देती है.

ज़रूरी शर्तें

इस सैंपल का इस्तेमाल करने के लिए, आपको ये ज़रूरी शर्तें पूरी करनी होंगी:

  • Google खाता (Google Workspace खातों के लिए एडमिन की अनुमति ज़रूरी हो सकती है).
  • इंटरनेट ऐक्सेस करने वाला वेब ब्राउज़र.
  • Google Cloud प्रोजेक्ट.

अपना एनवायरमेंट सेट अप करें

Google Cloud Console में अपना Cloud प्रोजेक्ट खोलें

अगर यह पहले से नहीं खुला है, तो वह Cloud प्रोजेक्ट खोलें जिसे आपको इस सैंपल के लिए इस्तेमाल करना है:

  1. Google Cloud Console में, कोई प्रोजेक्ट चुनें पेज पर जाएं.

    Cloud प्रोजेक्ट चुनें

  2. वह Google Cloud प्रोजेक्ट चुनें जिसका आपको इस्तेमाल करना है. या प्रोजेक्ट बनाएं पर क्लिक करके, स्क्रीन पर दिए गए निर्देशों का पालन करें. अगर आपने Google Cloud प्रोजेक्ट बनाया है, तो आपको प्रोजेक्ट के लिए बिलिंग चालू करनी पड़ सकती है.

एपीआई चालू करें

Google API का इस्तेमाल करने से पहले, आपको उन्हें Google Cloud प्रोजेक्ट में चालू करना होगा. किसी एक Google Cloud प्रोजेक्ट में, एक या उससे ज़्यादा एपीआई चालू किए जा सकते हैं.

सभी चैट ऐप्लिकेशन के लिए, सहमति वाली स्क्रीन को कॉन्फ़िगर करना ज़रूरी है. अपने ऐप्लिकेशन की OAuth सहमति स्क्रीन को कॉन्फ़िगर करने से यह तय होता है कि Google, उपयोगकर्ताओं को क्या दिखाता है और आपका ऐप्लिकेशन रजिस्टर करता है, ताकि आप उसे बाद में पब्लिश कर सकें.

  1. Google Cloud Console में, मेन्यू > एपीआई और सेवाएं > OAuth की सहमति वाली स्क्रीन पर जाएं.

    ऐसी स्क्रीन पर जाएं जहां OAuth के लिए सहमति दी जाती है

  2. उपयोगकर्ता टाइप के लिए, अंदरूनी चुनें. इसके बाद, बनाएं पर क्लिक करें.
  3. ऐप्लिकेशन रजिस्ट्रेशन फ़ॉर्म भरें. इसके बाद, सेव करें और जारी रखें पर क्लिक करें.
  4. फ़िलहाल, दायरों को छोड़कर आगे बढ़ा जा सकता है. साथ ही, सेव करें और जारी रखें पर क्लिक करें. आने वाले समय में, जब अपने Google Workspace संगठन से बाहर इस्तेमाल करने के लिए कोई ऐप्लिकेशन बनाया जाएगा, तो आपको उपयोगकर्ता टाइप को बाहरी उपयोगकर्ता में बदलना होगा. इसके बाद, आपको अपने ऐप्लिकेशन के लिए ज़रूरी अनुमति के दायरे जोड़ने होंगे.

  5. अपने ऐप्लिकेशन रजिस्ट्रेशन की खास जानकारी देखें. बदलाव करने के लिए, बदलाव करें पर क्लिक करें. अगर ऐप्लिकेशन रजिस्ट्रेशन ठीक लग रहा है, तो डैशबोर्ड पर वापस जाएं पर क्लिक करें.

स्क्रिप्ट सेट अप करें

Apps Script प्रोजेक्ट बनाना

  1. Google Chat से मीटिंग शेड्यूल करें Apps Script प्रोजेक्ट खोलने के लिए, नीचे दिए गए बटन पर क्लिक करें.
    प्रोजेक्ट खोलें
  2. खास जानकारी पर क्लिक करें.
  3. खास जानकारी देने वाले पेज पर, कॉपी बनाएं कॉपी बनाने के लिए आइकॉन पर क्लिक करें.

Cloud प्रोजेक्ट नंबर कॉपी करें

  1. Google Cloud Console में, मेन्यू > IAM और एडमिन > सेटिंग पर जाएं.

    IAM और एडमिन सेटिंग पर जाएं

  2. प्रोजेक्ट नंबर फ़ील्ड में, वैल्यू कॉपी करें.

Apps Script प्रोजेक्ट का क्लाउड प्रोजेक्ट सेट करें

  1. कॉपी किए गए Apps Script प्रोजेक्ट में, प्रोजेक्ट सेटिंग प्रोजेक्ट सेटिंग का आइकॉन पर क्लिक करें.
  2. Google Cloud Platform (GCP) प्रोजेक्ट में जाकर, प्रोजेक्ट बदलें पर क्लिक करें.
  3. GCP प्रोजेक्ट नंबर में, Google Cloud प्रोजेक्ट नंबर चिपकाएं.
  4. प्रोजेक्ट सेट करें पर क्लिक करें.

टेस्ट डिप्लॉयमेंट बनाना

  1. कॉपी किए गए Apps Script प्रोजेक्ट में, डिप्लॉय करें > डिप्लॉयमेंट की जांच करें पर क्लिक करें.
  2. बाद के चरण में इस्तेमाल करने के लिए, हेड डिप्लॉयमेंट आईडी को कॉपी करें और हो गया पर क्लिक करें.

Chat API को कॉन्फ़िगर करना

  1. Google Cloud Console में, Chat API पेज पर जाएं.
    Chat API पर जाएं
  2. कॉन्फ़िगरेशन पर क्लिक करें.
  3. इस जानकारी के साथ Chat API को कॉन्फ़िगर करें:
    • नाम: Meeting Scheduler
    • अवतार का यूआरएल: ऐसा यूआरएल जोड़ें जो कम से कम 256x256 पिक्सल वाली इमेज पर ले जाता हो.
    • जानकारी: Quickly create meetings.
    • फ़ंक्शनलिटी: दोनों बॉक्स पर सही का निशान लगाएं, ताकि उपयोगकर्ता सीधे ऐप्लिकेशन को मैसेज कर सकें और उसे स्पेस में जोड़ सकें.
    • कनेक्शन सेटिंग: Apps Script पर क्लिक करें और हेड डिप्लॉयमेंट आईडी डालें.
    • स्लैश कमांड: /help और /schedule_Meeting के लिए, स्लैश कमांड जोड़ें. इसके लिए, यह तरीका अपनाएं:
      1. स्लैश कमांड जोड़ें पर क्लिक करें और उसे यहां दी गई जानकारी के साथ कॉन्फ़िगर करें:
        • नाम: /help
        • कमांड आईडी: 1
        • जानकारी: Learn what this app does.
      2. स्लैश कमांड जोड़ें पर फिर से क्लिक करें और उसे यहां दी गई जानकारी के साथ कॉन्फ़िगर करें:
        • नाम: /schedule_Meeting
        • कमांड आईडी: 2
        • जानकारी: Schedule a meeting.
        • डायलॉग खुलता है बॉक्स को चुनें.
    • अनुमतियां: आपके डोमेन के खास लोग और ग्रुप चुनें और अपना ईमेल पता डालें.
  4. सेव करें पर क्लिक करें और पेज को रीफ़्रेश करें.
  5. कॉन्फ़िगरेशन पेज पर, ऐप्लिकेशन के स्टेटस में जाकर, स्टेटस को लाइव - उपयोगकर्ताओं के लिए उपलब्ध पर सेट करें.
  6. सेव करें पर क्लिक करें.

स्क्रिप्ट चलाएं

  1. Google Chat खोलें.
  2. चैट शुरू करें पर क्लिक करें.
  3. ऐप्लिकेशन का नाम Meeting Scheduler खोजें.
  4. अनुमति देने के लिए, शुरुआती मैसेज भेजें, जैसे कि hello.
  5. जब ऐप्लिकेशन जवाब दे, तब कॉन्फ़िगर करें पर क्लिक करें और ऐप्लिकेशन को अनुमति दें. अगर OAuth के लिए सहमति वाली स्क्रीन पर चेतावनी दिखती है, इस ऐप्लिकेशन की पुष्टि नहीं हुई है, तो बेहतर > को चुनकर जारी रखें {Project Name} पर जाएं (असुरक्षित).

  6. ऐप्लिकेशन को /schedule_Meeting भेजें.

  7. डायलॉग में, न्योता पाने वाला कम से कम एक ईमेल पता जोड़ें. अन्य फ़ील्ड अपडेट किए जा सकते हैं या डिफ़ॉल्ट एंट्री का इस्तेमाल किया जा सकता है.

  8. सबमिट करें पर क्लिक करें.

  9. मीटिंग देखने के लिए, Calendar इवेंट खोलें पर क्लिक करें.

कोड की समीक्षा करें

इस समाधान के Apps Script कोड की समीक्षा करने के लिए, नीचे सोर्स कोड देखें पर क्लिक करें:

सोर्स कोड देखें

Code.gs

solutions/chat-bots/schedule-meetings/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/chat-bots/schedule-meetings

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Application constants
const BOTNAME = 'Chat Meeting Scheduler';
const SLASHCOMMAND = {
  HELP: 1, // /help
  DIALOG: 2, // /schedule_Meeting
};

/**
 * Responds to an ADDED_TO_SPACE event in Google Chat.
 * Called when the bot is added to a space. The bot can either be directly added to the space
 * or added by a @mention. If the bot is added by a @mention, the event object includes a message property. 
 * Returns a Message object, which is usually a welcome message informing users about the bot.
 *
 * @param {Object} event The event object from Google Chat
 */
function onAddToSpace(event) {
  let message = '';

  // Personalizes the message depending on how the bot is called.
  if (event.space.singleUserBotDm) {
    message = `Hi ${event.user.displayName}!`;
  } else {
    const spaceName = event.space.displayName ? event.space.displayName : "this chat";
    message = `Hi! Thank you for adding me to ${spaceName}`;
  }

  // Lets users know what they can do and how they can get help.
  message = message + '/nI can quickly schedule a meeting for you with just a few clicks.' +
    'Try me out by typing */schedule_Meeting*. ' +
    '/nTo learn what else I can do, type */help*.'

  return { "text": message };
}

/**
 * Responds to a MESSAGE event triggered in Chat.
 * Called when the bot is already in the space and the user invokes it via @mention or / command.
 * Returns a message object containing the bot's response. For this bot, the response is either the
 * help text or the dialog to schedule a meeting.
 * 
 * @param {object} event The event object from Google Chat
 * @return {object} JSON-formatted response as text or Card message
 */
function onMessage(event) {

  // Handles regular onMessage logic.
  // Evaluates if and handles for all slash commands.
  if (event.message.slashCommand) {
    switch (event.message.slashCommand.commandId) {

      case SLASHCOMMAND.DIALOG: // Displays meeting dialog for /schedule_Meeting.

        // TODO update this with your own logic to set meeting recipients, subjects, etc (e.g. a group email).
        return getInputFormAsDialog_({
          invitee: '',
          startTime: getTopOfHourDateString_(),
          duration: 30,
          subject: 'Status Stand-up',
          body: 'Scheduling a quick status stand-up meeting.'
        });

      case SLASHCOMMAND.HELP: // Responds with help text for /help.
        return getHelpTextResponse_();

      /* TODO Add other use cases here. E.g:
      case SLASHCOMMAND.NEW_FEATURE:  // Your Feature Here
        getDialogForAddContact(message);
      */

    }
  }
  else {
    // Returns text if users didn't invoke a slash command.
    return { text: 'No action taken - use Slash Commands.' }
  }
}

/**
 * Responds to a CARD_CLICKED event triggered in Chat.
 * @param {object} event the event object from Chat
 * @return {object} JSON-formatted response
 * @see https://developers.google.com/chat/api/guides/message-formats/events
 */
function onCardClick(event) {
  if (event.action.actionMethodName === 'handleFormSubmit') {
    const recipients = getFieldValue_(event.common.formInputs, 'email');
    const subject = getFieldValue_(event.common.formInputs, 'subject');
    const body = getFieldValue_(event.common.formInputs, 'body');

    // Assumes dialog card inputs for date and times are in the correct format. mm/dd/yyy HH:MM
    const dateTimeInput = getFieldValue_(event.common.formInputs, 'date');
    const startTime = getStartTimeAsDateObject_(dateTimeInput);
    const duration = Number(getFieldValue_(event.common.formInputs, 'duration'));

    // Handles instances of missing or invalid input parameters.
    const errors = [];

    if (!recipients) {
      errors.push('Missing or invalid recipient email address.');
    }
    if (!subject) {
      errors.push('Missing subject line.');
    }
    if (!body) {
      errors.push('Missing event description.');
    }
    if (!startTime) {
      errors.push('Missing or invalid start time.');
    }
    if (!duration || isNaN(duration)) {
      errors.push('Missing or invalid duration');
    }
    if (errors.length) {
      // Redisplays the form if missing or invalid inputs exist.
      return getInputFormAsDialog_({
        errors,
        invitee: recipients,
        startTime: dateTimeInput,
        duration,
        subject,
        body
      });
    }

    //  Calculates the end time via duration.
    const endTime = new Date(startTime.valueOf());
    endTime.setMinutes(endTime.getMinutes() + duration);

    // Creates calendar event with notification.
    const calendar = CalendarApp.getDefaultCalendar()
    const scheduledEvent = calendar.createEvent(subject,
      startTime,
      endTime,
      {
        guests: recipients,
        sendInvites: true,
        description: body + '\nThis meeting scheduled by a Google Chat App!'
      });

    // Gets a link to the Calendar event.
    const url = getCalendarEventURL_(scheduledEvent, calendar)

    return getConfirmationDialog_(url);

  } else if (event.action.actionMethodName === 'closeDialog') {

    // Returns this dialog as success.
    return {
      actionResponse: {
        type: 'DIALOG',
        dialog_action: {
          actionStatus: 'OK'
        }
      }
    }
  }
}

/**
 * Responds with help text about this chat bot.
 * @return {string} The help text as seen below
 */
function getHelpTextResponse_() {
  const help = `*${BOTNAME}* lets you quickly create meetings from Google Chat. Here\'s a list of all its commands:
  \`/schedule_Meeting\`  Opens a dialog with editable, preset parameters to create a meeting event
  \`/help\`  Displays this help message

  Learn more about creating Google Chat bots at https://developers.google.com/chat.`

  return { 'text': help }
}

Dialog.gs

solutions/chat-bots/schedule-meetings/Dialog.js
/**
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
* Form input dialog as JSON.
* @return {object} JSON-formatted cards for the dialog.
*/
function getInputFormAsDialog_(options) {
  const form = getForm_(options);
  return {
    'actionResponse': {
      'type': 'DIALOG',
      'dialogAction': {
        'dialog': {
          'body': form
        }
      }
    }
  };
}

/**
* Form JSON to collect inputs regarding the meeting.
* @return {object} JSON-formatted cards.
*/
function getForm_(options) {
  const sections = [];

  // If errors present, display additional section with validation messages.
  if (options.errors && options.errors.length) {
    let errors = options.errors.reduce((str, err) => `${str}• ${err}<br>`, '');
    errors = `<b>Errors:</b><br><font color="#ba0000">${errors}</font>`;
    const errorSection = {
      'widgets': [
        {
          textParagraph: {
            text: errors
          }
        }
      ]
    }
    sections.push(errorSection);
  }
  let formSection = {
    'header': 'Schedule meeting and send email to invited participants',
    'widgets': [
      {
        'textInput': {
          'label': 'Event Title',
          'type': 'SINGLE_LINE',
          'name': 'subject',
          'value': options.subject
        }
      },
      {
        'textInput': {
          'label': 'Invitee Email Address',
          'type': 'SINGLE_LINE',
          'name': 'email',
          'value': options.invitee,
          'hintText': 'Add team group email'
        }
      },
      {
        'textInput': {
          'label': 'Description',
          'type': 'MULTIPLE_LINE',
          'name': 'body',
          'value': options.body
        }
      },
      {
        'textInput': {
          'label': 'Meeting start date & time',
          'type': 'SINGLE_LINE',
          'name': 'date',
          'value': options.startTime,
          'hintText': 'mm/dd/yyyy H:MM'
        }
      },
      {
        'selectionInput': {
          'type': 'DROPDOWN',
          'label': 'Meeting Duration',
          'name': 'duration',
          'items': [
            {
              'text': '15 minutes',
              'value': '15',
              'selected': options.duration === 15
            },
            {
              'text': '30 minutes',
              'value': '30',
              'selected': options.duration === 30
            },
            {
              'text': '45 minutes',
              'value': '45',
              'selected': options.duration === 45
            },
            {
              'text': '1 Hour',
              'value': '60',
              'selected': options.duration === 60
            },
            {
              'text': '1.5 Hours',
              'value': '90',
              'selected': options.duration === 90
            },
            {
              'text': '2 Hours',
              'value': '120',
              'selected': options.duration === 120
            }
          ]
        }
      }
    ],
    'collapsible': false
  };
  sections.push(formSection);
  const card =  {
    'sections': sections,
    'name': 'Google Chat Scheduled Meeting',
    'fixedFooter': {
      'primaryButton': {
        'text': 'Submit',
        'onClick': {
          'action': {
            'function': 'handleFormSubmit'
          }
        },
        'altText': 'Submit'
      }
    }
  };
  return card;
}

/**
* Confirmation dialog after a calendar event is created successfully.
* @param {string} url The Google Calendar Event url for link button
* @return {object} JSON-formatted cards for the dialog
*/
function getConfirmationDialog_(url) {
  return {
    'actionResponse': {
      'type': 'DIALOG',
      'dialogAction': {
        'dialog': {
          'body': {
            'sections': [
              {
                'widgets': [
                  {
                    'textParagraph': {
                      'text': 'Meeting created successfully!'
                    },
                    'horizontalAlignment': 'CENTER'
                  },
                  {
                    'buttonList': {
                      'buttons': [
                        {
                          'text': 'Open Calendar Event',
                          'onClick': {
                            'openLink': {
                              'url': url
                            }
                          }
                        }

                      ]
                    },
                    'horizontalAlignment': 'CENTER'
                  }
                ]
              }
            ],
            'fixedFooter': {
              'primaryButton': {
                'text': 'OK',
                'onClick': {
                  'action': {
                    'function': 'closeDialog'
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Utilities.gs

solutions/chat-bots/schedule-meetings/Utilities.js
/**
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
* Helper function that gets the field value from the given form input.
* @return {string} 
*/
function getFieldValue_(formInputs, fieldName) {
  return formInputs[fieldName][''].stringInputs.value[0];
}

// Regular expression to validate the date/time input.
const DATE_TIME_PATTERN = /\d{1,2}\/\d{1,2}\/\d{4}\s+\d{1,2}:\d\d/;

/**
* Casts date and time from string to Date object.
* @return {date} 
*/
function getStartTimeAsDateObject_(dateTimeStr) {
  if (!dateTimeStr || !dateTimeStr.match(DATE_TIME_PATTERN)) {
    return null;
  }

  const parts = dateTimeStr.split(' ');
  const [month, day, year] = parts[0].split('/').map(Number);
  const [hour, minute] = parts[1].split(':').map(Number);


  Session.getScriptTimeZone()

  return new Date(year, month - 1, day, hour, minute)
}

/** 
* Gets the current date and time for the upcoming top of the hour (e.g. 01/25/2022 18:00).
* @return {string} date/time in mm/dd/yyy HH:MM format needed for use by Calendar
*/
function getTopOfHourDateString_() {
  const date = new Date();
  date.setHours(date.getHours() + 1);
  date.setMinutes(0, 0, 0);
  // Adding the date as string might lead to an incorrect response due to time zone adjustments.
  return Utilities.formatDate(date, Session.getScriptTimeZone(), 'MM/dd/yyyy H:mm');
}


/** 
* Creates the URL for the Google Calendar event.
*
* @param {object} event The Google Calendar Event instance
* @param {object} cal The associated Google Calendar 
* @return {string} URL in the form of 'https://www.google.com/calendar/event?eid={event-id}'
*/
function getCalendarEventURL_(event, cal) {
  const baseCalUrl = 'https://www.google.com/calendar';
  // Joins Calendar Event Id with Calendar Id, then base64 encode to derive the event URL.
  let encodedId = Utilities.base64Encode(event.getId().split('@')[0] + " " + cal.getId()).replace(/\=/g, '');
  encodedId = `/event?eid=${encodedId}`;
  return (baseCalUrl + encodedId);

}

योगदानकर्ता

Google, इस सैंपल को मैनेज करने के लिए Google Developer के विशेषज्ञों की मदद लेता है.

अगले चरण