Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review existing Gmail APIs and its practical uses #4875

Closed
Tracked by #4874
niveathika opened this issue Oct 3, 2023 · 1 comment
Closed
Tracked by #4874

Review existing Gmail APIs and its practical uses #4875

niveathika opened this issue Oct 3, 2023 · 1 comment

Comments

@niveathika
Copy link
Contributor

niveathika commented Oct 3, 2023

$subject

@niveathika
Copy link
Contributor Author

niveathika commented Oct 15, 2023

Gmail API Connector

Gmail REST API exposes gmail mailbox operations over the network. This allows programmes to access users data easily for automations.

Use Cases

  1. Automated emails sending based on templates
  2. Automatically forwarding emails due to specific conditions
  3. Triggering processing of emails based on conditions
  4. Backing up of mailbox
  5. Migrating mailbox

Available APIs

  1. /users - Base API to get user profile and give mail operations
  2. /users/${userId}/drafts -
  3. /users/${userId}/history
  4. /users/${userId}/labels
  5. /users/${userId}/messages
  6. /users/${userId}/settings*
  7. /users/${userId}/threads
  • Currently settings API is not supported.

Sample code for use case 3

import ballerina/io;

configurable string refreshToken = ?;
configurable string clientId = ?;
configurable string clientSecret = ?;

public function main() returns error? {

    Client gmailClient = check new Client(
        config = {
            auth: {
                refreshToken: refreshToken,
                clientId: clientId,
                clientSecret: clientSecret
            }
        }
    );
    ListMessagesResponse messageList = check gmailClient->/users/["me"]/messages(q = "is:unread");

    Message[] messages = messageList.messages ?: [];
    foreach Message message in messages {
        Message fullMessage = check gmailClient->/users/["me"]/messages/[message.id ?: ""](format = "full");

        // Business Logic
        MessagePartHeader[] payloadHeaders = fullMessage.payload?.headers ?: [];
        MessagePartHeader[] subjectHeader = payloadHeaders.filter(payloadHeader => payloadHeader.name == "Subject");
        if subjectHeader.length() > 0 {
            string? subject = subjectHeader[0].value;
            if subject !is () {
                io:println(subject);
            }
        }

        Message _ = check gmailClient->/users/["me"]/messages/[message.id ?: ""]/modify.post(
            payload = {
                removeLabelIds: ["UNREAD"]
            }
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

1 participant