উন্নত চ্যাট পরিষেবা

অ্যাডভান্সড চ্যাট পরিষেবা আপনাকে অ্যাপস স্ক্রিপ্টে গুগল চ্যাট এপিআই ব্যবহার করতে দেয়। এই এপিআই স্ক্রিপ্টগুলিকে চ্যাট স্পেস খুঁজে পেতে, তৈরি করতে এবং পরিবর্তন করতে, স্পেসে সদস্যদের যোগ করতে বা সরাতে এবং টেক্সট, কার্ড, সংযুক্তি এবং প্রতিক্রিয়া সহ বার্তা পড়তে বা পোস্ট করতে দেয়।

পূর্বশর্ত

তথ্যসূত্র

এই পরিষেবা সম্পর্কে আরও তথ্যের জন্য, চ্যাট API রেফারেন্স ডকুমেন্টেশন দেখুন। অ্যাপস স্ক্রিপ্টের সমস্ত উন্নত পরিষেবার মতো, চ্যাট পরিষেবাটি পাবলিক API-এর মতো একই বস্তু, পদ্ধতি এবং পরামিতি ব্যবহার করে।

নমুনা কোড

এই নমুনাগুলি আপনাকে দেখায় যে উন্নত পরিষেবা ব্যবহার করে কীভাবে সাধারণ Google Chat API অ্যাকশনগুলি সম্পাদন করতে হয়।

ব্যবহারকারীর শংসাপত্র সহ একটি বার্তা পোস্ট করুন

নিম্নলিখিত উদাহরণটি ব্যবহারকারীর পক্ষ থেকে চ্যাট স্পেসে কীভাবে একটি বার্তা পোস্ট করতে হয় তা দেখায়।

  1. Apps Script প্রজেক্টের appsscript.json ফাইলে chat.messages.create অনুমোদনের সুযোগ যোগ করুন:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.messages.create"
    ]
    
  2. অ্যাপস স্ক্রিপ্ট প্রজেক্টের কোডে এই ধরণের একটি ফাংশন যোগ করুন:

    উন্নত/চ্যাট.জিএস
    /**
     * Posts a new message to the specified space on behalf of the user.
     * @param {string} spaceName The resource name of the space.
     */
    function postMessageWithUserCredentials(spaceName) {
      try {
        const message = { text: "Hello world!" };
        Chat.Spaces.Messages.create(message, spaceName);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log("Failed to create message with error %s", err.message);
      }
    }

অ্যাপ শংসাপত্র সহ একটি বার্তা পোস্ট করুন

নিম্নলিখিত উদাহরণটি অ্যাপের পক্ষ থেকে চ্যাট স্পেসে কীভাবে একটি বার্তা পোস্ট করতে হয় তা দেখায়। একটি পরিষেবা অ্যাকাউন্টের সাথে উন্নত চ্যাট পরিষেবা ব্যবহার করার জন্য আপনাকে appsscript.json এ অনুমোদনের স্কোপ নির্দিষ্ট করতে হবে না। পরিষেবা অ্যাকাউন্টগুলির সাথে প্রমাণীকরণ সম্পর্কে বিশদ জানতে, Authenticate as a Google Chat অ্যাপ দেখুন।

উন্নত/চ্যাট.জিএস
/**
 * Posts a new message to the specified space on behalf of the app.
 * @param {string} spaceName The resource name of the space.
 */
function postMessageWithAppCredentials(spaceName) {
  try {
    // See https://developers.google.com/chat/api/guides/auth/service-accounts
    // for details on how to obtain a service account OAuth token.
    const appToken = getToken_();
    const message = { text: "Hello world!" };
    Chat.Spaces.Messages.create(
      message,
      spaceName,
      {},
      // Authenticate with the service account token.
      { Authorization: `Bearer ${appToken}` },
    );
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed to create message with error %s", err.message);
  }
}

জায়গা পান

নিম্নলিখিত উদাহরণটি দেখায় যে কীভাবে চ্যাট স্পেস সম্পর্কে তথ্য পেতে হয়।

  1. Apps Script প্রজেক্টের appsscript.json ফাইলে chat.spaces.readonly অনুমোদনের সুযোগ যোগ করুন:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.readonly"
    ]
    
  2. অ্যাপস স্ক্রিপ্ট প্রজেক্টের কোডে এই ধরণের একটি ফাংশন যোগ করুন:

    উন্নত/চ্যাট.জিএস
    /**
     * Gets information about a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function getSpace(spaceName) {
      try {
        const space = Chat.Spaces.get(spaceName);
        console.log("Space display name: %s", space.displayName);
        console.log("Space type: %s", space.spaceType);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log("Failed to get space with error %s", err.message);
      }
    }

একটি স্থান তৈরি করুন

নিচের উদাহরণটি কীভাবে একটি চ্যাট স্পেস তৈরি করতে হয় তা দেখায়।

  1. Apps Script প্রজেক্টের appsscript.json ফাইলে chat.spaces.create অনুমোদনের সুযোগ যোগ করুন:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.spaces.create"
    ]
    
  2. অ্যাপস স্ক্রিপ্ট প্রজেক্টের কোডে এই ধরণের একটি ফাংশন যোগ করুন:

    উন্নত/চ্যাট.জিএস
    /**
     * Creates a new Chat space.
     */
    function createSpace() {
      try {
        const space = { displayName: "New Space", spaceType: "SPACE" };
        Chat.Spaces.create(space);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log("Failed to create space with error %s", err.message);
      }
    }

সদস্যপদ তালিকাভুক্ত করুন

নিম্নলিখিত উদাহরণটি দেখায় যে কীভাবে একটি চ্যাট স্পেসের সকল সদস্যের তালিকা তৈরি করতে হয়।

  1. Apps Script প্রজেক্টের appsscript.json ফাইলে chat.memberships.readonly অনুমোদনের সুযোগ যোগ করুন:

    "oauthScopes": [
      "https://www.googleapis.com/auth/chat.memberships.readonly"
    ]
    
  2. অ্যাপস স্ক্রিপ্ট প্রজেক্টের কোডে এই ধরণের একটি ফাংশন যোগ করুন:

    উন্নত/চ্যাট.জিএস
    /**
     * Lists all the members of a Chat space.
     * @param {string} spaceName The resource name of the space.
     */
    function listMemberships(spaceName) {
      let response;
      let pageToken = null;
      try {
        do {
          response = Chat.Spaces.Members.list(spaceName, {
            pageSize: 10,
            pageToken: pageToken,
          });
          if (!response.memberships || response.memberships.length === 0) {
            pageToken = response.nextPageToken;
            continue;
          }
          for (const membership of response.memberships) {
            console.log(
              "Member: %s, Role: %s",
              membership.member.displayName,
              membership.role,
            );
          }
          pageToken = response.nextPageToken;
        } while (pageToken);
      } catch (err) {
        // TODO (developer) - Handle exception
        console.log("Failed with error %s", err.message);
      }
    }

সমস্যা সমাধান

যদি আপনি Error 400: invalid_scope এর সম্মুখীন হন যেখানে Some requested scopes cannot be shown " ত্রুটি বার্তাটি থাকে, তাহলে এর অর্থ হল আপনি Apps Script প্রকল্পের appsscript.json ফাইলে কোনও অনুমোদন স্কোপ নির্দিষ্ট করেননি। বেশিরভাগ ক্ষেত্রে, Apps Script স্বয়ংক্রিয়ভাবে নির্ধারণ করে যে কোনও স্ক্রিপ্টের কোন স্কোপ প্রয়োজন, কিন্তু যখন আপনি Chat উন্নত পরিষেবা ব্যবহার করেন, তখন আপনাকে অবশ্যই আপনার স্ক্রিপ্ট যে অনুমোদন স্কোপ ব্যবহার করে তা আপনার Apps Script প্রকল্পের ম্যানিফেস্ট ফাইলে ম্যানুয়ালি যোগ করতে হবে। স্পষ্ট স্কোপ সেট করা দেখুন।

ত্রুটিটি সমাধান করতে, oauthScopes অ্যারের অংশ হিসেবে Apps Script প্রকল্পের appsscript.json ফাইলে উপযুক্ত অনুমোদনের স্কোপ যোগ করুন। উদাহরণস্বরূপ, spaces.messages.create পদ্ধতিটি কল করতে, নিম্নলিখিতগুলি যোগ করুন:

"oauthScopes": [
  "https://www.googleapis.com/auth/chat.messages.create"
]

সীমাবদ্ধতা এবং বিবেচনা

অ্যাডভান্সড চ্যাট পরিষেবাটি সমর্থন করে না:

একটি বার্তা সংযুক্তি ডাউনলোড করতে বা একটি বিকাশকারী প্রিভিউ পদ্ধতিতে কল করতে, পরিবর্তে UrlFetchApp ব্যবহার করুন।