কিউরেটেড কন্টেন্ট পাঠান

কোডিং স্তর : শিক্ষানবিস
সময়কাল : ২০ মিনিট
প্রকল্পের ধরণ : ইভেন্ট-চালিত ট্রিগার সহ অটোমেশন

উদ্দেশ্য

  • সমাধানটি কী কাজ করে তা বুঝুন।
  • সলিউশনের মধ্যে অ্যাপস স্ক্রিপ্ট সার্ভিসগুলো কী কাজ করে তা বুঝুন।
  • স্ক্রিপ্টটি সেট আপ করুন।
  • স্ক্রিপ্টটি চালান।

এই সমাধান সম্পর্কে

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

গুগল ফর্মস এবং জিমেইলের মাধ্যমে কন্টেন্ট পাঠানোর ডেমো

এটি কীভাবে কাজ করে

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

অ্যাপস স্ক্রিপ্ট পরিষেবা

এই সমাধানটি নিম্নলিখিত পরিষেবাগুলি ব্যবহার করে:

  • স্ক্রিপ্ট সার্ভিস : এটি ইভেন্ট-চালিত ট্রিগার ইনস্টল করে, যা কেউ ফর্ম জমা দিলেই চালু হয়।
  • ডকুমেন্ট সার্ভিস : স্ক্রিপ্টটি ইমেল তৈরি করতে যে ডক্স টেমপ্লেটটি ব্যবহার করে, সেটি খোলে।
  • মেইল পরিষেবা : ব্যবহারকারীর নাম এবং নির্বাচিত বিষয়বস্তু সহ ইমেইল তৈরি করে এবং পাঠায়।
  • স্প্রেডশিট পরিষেবা : স্ক্রিপ্টটি ইমেল পাঠানোর পর, এটি ফর্মের উত্তর সংক্রান্ত শীটে একটি নিশ্চিতকরণ বার্তা যোগ করে।

পূর্বশর্ত

এই নমুনাটি ব্যবহার করার জন্য আপনার নিম্নলিখিত পূর্বশর্তগুলো প্রয়োজন:

  • একটি গুগল অ্যাকাউন্ট (গুগল ওয়ার্কস্পেস অ্যাকাউন্টের জন্য প্রশাসকের অনুমোদনের প্রয়োজন হতে পারে)।
  • ইন্টারনেট সংযোগসহ একটি ওয়েব ব্রাউজার।

স্ক্রিপ্টটি সেট আপ করুন

  1. 'Send curated content Sheets' স্প্রেডশিটটির একটি অনুলিপি তৈরি করতে নিম্নলিখিত বোতামটি ক্লিক করুন। এই সলিউশনের অ্যাপস স্ক্রিপ্ট প্রজেক্টটি স্প্রেডশিটটির সাথে সংযুক্ত আছে:

    একটি অনুলিপি তৈরি করুন

  2. আপনার কপি করা স্প্রেডশীটে, এক্সটেনশন > অ্যাপস স্ক্রিপ্ট- এ ক্লিক করুন।

  3. ফাংশন ড্রপ-ডাউন থেকে installTrigger নির্বাচন করুন।

  4. রান-এ ক্লিক করুন।

  5. অনুরোধ করা হলে, স্ক্রিপ্টটিকে অনুমোদন দিন। <<../_snippets/oauth.md>>

গুরুত্বপূর্ণ : আপনি যদি `installTrigger` একাধিকবার চালান, তাহলে স্ক্রিপ্টটি একাধিক ট্রিগার তৈরি করে, যেগুলোর প্রতিটি ব্যবহারকারী ফর্ম জমা দিলে একটি করে ইমেল পাঠায়। অতিরিক্ত ট্রিগারগুলো মুছে ফেলতে এবং ডুপ্লিকেট ইমেল এড়াতে, `Triggers ক্লিক করুন। প্রতিটি অতিরিক্ত ট্রিগারের উপর রাইট-ক্লিক করে `Delete trigger`-এ ক্লিক করুন।

স্ক্রিপ্টটি চালান

  1. স্প্রেডশিটে ফিরে যান এবং টুলস > ফর্ম পরিচালনা > লাইভ ফর্মে যান-এ ক্লিক করুন।
  2. ফর্মটি পূরণ করুন এবং সাবমিট বাটনে ক্লিক করুন।
  3. আপনার নির্বাচিত বিষয়বস্তুর লিঙ্কসহ একটি ইমেলের জন্য আপনার ইমেল চেক করুন।

কোডটি পর্যালোচনা করুন

এই সলিউশনের অ্যাপস স্ক্রিপ্ট কোড পর্যালোচনা করতে, 'ভিউ সোর্স কোড'- এ ক্লিক করুন:

উৎস কোড দেখুন

কোড.জিএস

সমাধান/স্বয়ংক্রিয়করণ/কন্টেন্ট-সাইনআপ/কোড.জেএস
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/content-signup

/*
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.
*/

// To use your own template doc, update the below variable with the URL of your own Google Doc template.
// Make sure you update the sharing settings so that 'anyone'  or 'anyone in your organization' can view.
const EMAIL_TEMPLATE_DOC_URL =
  "https://docs.google.com/document/d/1enes74gWsMG3dkK3SFO08apXkr0rcYBd3JHKOb2Nksk/edit?usp=sharing";
// Update this variable to customize the email subject.
const EMAIL_SUBJECT = "Hello, here is the content you requested";

// Update this variable to the content titles and URLs you want to offer. Make sure you update the form so that the content titles listed here match the content titles you list in the form.
const topicUrls = {
  "Google Calendar how-to videos":
    "https://www.youtube.com/playlist?list=PLU8ezI8GYqs7IPb_UdmUNKyUCqjzGO9PJ",
  "Google Drive how-to videos":
    "https://www.youtube.com/playlist?list=PLU8ezI8GYqs7Y5d1cgZm2Obq7leVtLkT4",
  "Google Docs how-to videos":
    "https://www.youtube.com/playlist?list=PLU8ezI8GYqs4JKwZ-fpBP-zSoWPL8Sit7",
  "Google Sheets how-to videos":
    "https://www.youtube.com/playlist?list=PLU8ezI8GYqs61ciKpXf_KkV7ZRbRHVG38",
};

/**
 * Installs a trigger on the spreadsheet for when someone submits a form.
 */
function installTrigger() {
  ScriptApp.newTrigger("onFormSubmit")
    .forSpreadsheet(SpreadsheetApp.getActive())
    .onFormSubmit()
    .create();
}

/**
 * Sends a customized email for every form response.
 *
 * @param {Object} event - Form submit event
 */
function onFormSubmit(e) {
  const responses = e.namedValues;

  // If the question title is a label, it can be accessed as an object field.
  // If it has spaces or other characters, it can be accessed as a dictionary.
  const timestamp = responses.Timestamp[0];
  const email = responses["Email address"][0].trim();
  const name = responses.Name[0].trim();
  const topicsString = responses.Topics[0].toLowerCase();

  // Parse topics of interest into a list (since there are multiple items
  // that are saved in the row as blob of text).
  const topics = Object.keys(topicUrls).filter((topic) => {
    // indexOf searches for the topic in topicsString and returns a non-negative
    // index if the topic is found, or it will return -1 if it's not found.
    return topicsString.indexOf(topic.toLowerCase()) !== -1;
  });

  // If there is at least one topic selected, send an email to the recipient.
  let status = "";
  if (topics.length > 0) {
    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name, topics),
    });
    status = "Sent";
  } else {
    status = "No topics selected";
  }

  // Append the status on the spreadsheet to the responses' row.
  const sheet = SpreadsheetApp.getActiveSheet();
  const row = sheet.getActiveRange().getRow();
  const column = e.values.length + 1;
  sheet.getRange(row, column).setValue(status);

  console.log(`status=${status}; responses=${JSON.stringify(responses)}`);
}

/**
 * Creates email body and includes the links based on topic.
 *
 * @param {string} recipient - The recipient's email address.
 * @param {string[]} topics - List of topics to include in the email body.
 * @return {string} - The email body as an HTML string.
 */
function createEmailBody(name, topics) {
  let topicsHtml = topics
    .map((topic) => {
      const url = topicUrls[topic];
      return `<li><a href="${url}">${topic}</a></li>`;
    })
    .join("");
  topicsHtml = `<ul>${topicsHtml}</ul>`;

  // Make sure to update the emailTemplateDocId at the top.
  const docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
  let emailBody = docToHtml(docId);
  emailBody = emailBody.replace(/{{NAME}}/g, name);
  emailBody = emailBody.replace(/{{TOPICS}}/g, topicsHtml);
  return emailBody;
}

/**
 * Downloads a Google Doc as an HTML string.
 *
 * @param {string} docId - The ID of a Google Doc to fetch content from.
 * @return {string} The Google Doc rendered as an HTML string.
 */
function docToHtml(docId) {
  // Downloads a Google Doc as an HTML string.
  const url = `https://docs.google.com/feeds/download/documents/export/Export?id=${docId}&exportFormat=html`;
  const param = {
    method: "get",
    headers: { Authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
    muteHttpExceptions: true,
  };
  return UrlFetchApp.fetch(url, param).getContentText();
}
</section>

অবদানকারীরা

এই নমুনাটি গুগল ডেভেলপার বিশেষজ্ঞদের সহায়তায় গুগল দ্বারা রক্ষণাবেক্ষণ করা হয়।

পরবর্তী পদক্ষেপ