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

চ্যাট স্পেস, সদস্য এবং বার্তা পরিচালনা করুন।

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

পূর্বশর্ত

এটি একটি উন্নত পরিষেবা যা ব্যবহারের আগে আপনাকে অবশ্যই চালু করতে হবে।

রেফারেন্স

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

নমুনা কোড

এই নমুনাগুলো আপনাকে দেখাবে কীভাবে অ্যাডভান্সড সার্ভিস ব্যবহার করে গুগল চ্যাট এপিআই-এর সাধারণ কাজগুলো সম্পাদন করতে হয়।

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

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

  1. অ্যাপস স্ক্রিপ্ট প্রজেক্টের 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 এ অথরাইজেশন স্কোপ নির্দিষ্ট করার প্রয়োজন হয় না। সার্ভিস অ্যাকাউন্ট দিয়ে অথেন্টিকেশন সম্পর্কে বিস্তারিত জানতে, “একটি গুগল চ্যাট অ্যাপ হিসাবে অথেন্টিকেট করুন” দেখুন।

অ্যাডভান্সড/চ্যাট.জিএস
/**
 * 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. অ্যাপস স্ক্রিপ্ট প্রজেক্টের 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. অ্যাপস স্ক্রিপ্ট প্রজেক্টের 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. অ্যাপস স্ক্রিপ্ট প্রজেক্টের 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);
      }
    }

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

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

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

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

সীমাবদ্ধতা এবং বিবেচ্য বিষয়

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

মেসেজ অ্যাটাচমেন্ট ডাউনলোড করতে বা ডেভেলপার প্রিভিউ মেথড কল করতে, এর পরিবর্তে UrlFetchApp ব্যবহার করুন।