Gmail की बेहतर सेवा

बेहतर Gmail सेवा आपको Apps Script में Gmail एपीआई का इस्तेमाल करने की अनुमति देती है. Apps Script की पहले से मौजूद Gmail सेवा की तरह, यह एपीआई भी स्क्रिप्ट को Gmail मेलबॉक्स में थ्रेड, मैसेज, और लेबल को ढूंढने और उनमें बदलाव करने की अनुमति देता है. ज़्यादातर मामलों में, पहले से मौजूद सेवा का इस्तेमाल करना आसान होता है, लेकिन यह बेहतर सेवा कुछ और सुविधाएं उपलब्ध कराती है. साथ ही, Gmail कॉन्टेंट के बारे में ज़्यादा जानकारी वाली जानकारी का ऐक्सेस भी देती है.

रेफ़रंस

इस सेवा के बारे में ज़्यादा जानकारी के लिए, Gmail API के रेफ़रंस दस्तावेज़ देखें. Apps Script की सभी बेहतर सेवाओं की तरह, Gmail की बेहतर सेवा में भी उन ही ऑब्जेक्ट, तरीकों, और पैरामीटर का इस्तेमाल किया जाता है जो सार्वजनिक एपीआई में मौजूद होते हैं. ज़्यादा जानकारी के लिए, हस्ताक्षर तय करने का तरीका लेख पढ़ें.

समस्याओं की शिकायत करने और अन्य सहायता पाने के लिए, Gmail की सहायता गाइड देखें.

नमूना कोड

नीचे दिए गए सैंपल कोड में, एपीआई के वर्शन 1 का इस्तेमाल किया गया है.

लेबल की जानकारी की सूची बनाएं

नीचे दिए गए उदाहरण में, उपयोगकर्ता के लेबल की पूरी जानकारी देने का तरीका बताया गया है. इसमें लेबल का नाम, टाइप, आईडी, और 'किसको दिखे' सेटिंग शामिल हैं.

advanced/gmail.gs
/**
 * Lists the user's labels, including name, type,
 * ID and visibility information.
 */
function listLabelInfo() {
  try {
    const response =
      Gmail.Users.Labels.list('me');
    for (let i = 0; i < response.labels.length; i++) {
      const label = response.labels[i];
      console.log(JSON.stringify(label));
    }
  } catch (err) {
    console.log(err);
  }
}

इनबॉक्स के स्निपेट की सूची बनाना

नीचे दिए गए उदाहरण में, उपयोगकर्ता के इनबॉक्स में हर थ्रेड से जुड़े टेक्स्ट स्निपेट को लिस्ट करने का तरीका बताया गया है. नतीजों की पूरी सूची ऐक्सेस करने के लिए, पेज टोकन के इस्तेमाल पर ध्यान दें.

advanced/gmail.gs
/**
 * Lists, for each thread in the user's Inbox, a
 * snippet associated with that thread.
 */
function listInboxSnippets() {
  try {
    let pageToken;
    do {
      const threadList = Gmail.Users.Threads.list('me', {
        q: 'label:inbox',
        pageToken: pageToken
      });
      if (threadList.threads && threadList.threads.length > 0) {
        threadList.threads.forEach(function(thread) {
          console.log('Snippet: %s', thread.snippet);
        });
      }
      pageToken = threadList.nextPageToken;
    } while (pageToken);
  } catch (err) {
    console.log(err);
  }
}

हाल ही के इतिहास की सूची बनाएं

यहां दिए गए उदाहरण में, हाल की गतिविधि का इतिहास लॉग करने का तरीका बताया गया है. खास तौर पर, यह उदाहरण उपयोगकर्ता के हाल ही में भेजे गए मैसेज से जुड़े इतिहास रिकॉर्ड आईडी को वापस लाता है. इसके बाद, उस समय से बदले गए हर मैसेज के मैसेज आईडी को लॉग करता है. बदला गया हर मैसेज सिर्फ़ एक बार लॉग किया जाता है. इससे कोई फ़र्क़ नहीं पड़ता कि इतिहास के रिकॉर्ड में कितने बदलाव हुए हैं. नतीजों की पूरी सूची ऐक्सेस करने के लिए, पेज टोकन के इस्तेमाल पर ध्यान दें.

advanced/gmail.gs
/**
 * Gets a history record ID associated with the most
 * recently sent message, then logs all the message IDs
 * that have changed since that message was sent.
 */
function logRecentHistory() {
  try {
    // Get the history ID associated with the most recent
    // sent message.
    const sent = Gmail.Users.Threads.list('me', {
      q: 'label:sent',
      maxResults: 1
    });
    if (!sent.threads || !sent.threads[0]) {
      console.log('No sent threads found.');
      return;
    }
    const historyId = sent.threads[0].historyId;

    // Log the ID of each message changed since the most
    // recent message was sent.
    let pageToken;
    const changed = [];
    do {
      const recordList = Gmail.Users.History.list('me', {
        startHistoryId: historyId,
        pageToken: pageToken
      });
      const history = recordList.history;
      if (history && history.length > 0) {
        history.forEach(function(record) {
          record.messages.forEach(function(message) {
            if (changed.indexOf(message.id) === -1) {
              changed.push(message.id);
            }
          });
        });
      }
      pageToken = recordList.nextPageToken;
    } while (pageToken);

    changed.forEach(function(id) {
      console.log('Message Changed: %s', id);
    });
  } catch (err) {
    console.log(err);
  }
}