Skip to content

Commit

Permalink
Added gmail http endpoint example (triggerdotdev#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
biplobsd committed Dec 1, 2023
1 parent 8d602e5 commit bb8ae5d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ AWS_SECRET_ACCESS_KEY=

# Zapier
ZAPIER_TRIGGER_SECRET=

# Gmail
GMAIL_SERVICE_ACCOUNT_EMAIL=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"discord": "node -r dotenv/config -r ts-node/register src/discord.ts",
"github": "node -r dotenv/config -r ts-node/register src/github.ts",
"gmail": "node -r dotenv/config -r ts-node/register src/gmail.ts",
"gmail-http-endpoint": "node -r dotenv/config -r ts-node/register src/gmail-http-endpoint.ts",
"google-calendar": "node -r dotenv/config -r ts-node/register src/google-calendar.ts",
"google-drive": "node -r dotenv/config -r ts-node/register src/google-drive.ts",
"google-maps": "node -r dotenv/config -r ts-node/register src/google-maps.ts",
Expand Down
57 changes: 57 additions & 0 deletions src/gmail-http-endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { TriggerClient } from "@trigger.dev/sdk";

// hide-code
const client = new TriggerClient({ id: "api-reference" });
// end-hide-code

// Docs https://developers.google.com/gmail/api/guides/push#protocol
// Verification response https://cloud.google.com/pubsub/docs/authenticate-push-subscriptions

//create an HTTP Endpoint, with the gmail details
export const gmail = client.defineHttpEndpoint({
id: "gmail.com",
title: "gmail",
source: "gmail.com",
icon: "gmail",
verify: async (request) => {
// Getting bearer token from request
const bearer = request.headers.get("Authorization");
if (!bearer) {
return { success: false, reason: "No bearer token" };
}
const tokens = bearer.match(/Bearer (.*)/);
if (!tokens || tokens.length < 2) {
return { success: false, reason: "Invalid bearer token" };
}
const token = tokens[1];

// Verifying token
const data = await fetch(`https://oauth2.googleapis.com/tokeninfo?id_token=${token}`);
const payload = await data.json();
if (payload.email === process.env.GMAIL_SERVICE_ACCOUNT_EMAIL) {
return { success: true };
}

return { success: false, reason: "Invalid token" };
},
});

client.defineJob({
id: "http-gmail",
name: "HTTP gmail",
version: "1.0.0",
enabled: true,
// Create a trigger from the HTTP endpoint
trigger: gmail.onRequest(),
run: async (request, io, ctx) => {
const body = await request.json();
const message = Buffer.from(body.message.data, 'base64').toString('utf-8');
await io.logger.info(`Body`, JSON.parse(message));
},
});

// hide-code
// These lines can be removed if you don't want to use express
import { createExpressServer } from "@trigger.dev/express";
createExpressServer(client);
// end-hide-code

0 comments on commit bb8ae5d

Please sign in to comment.