AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { createTrigger, MarkdownVariant, TriggerStrategy, Property } from '@activepieces/pieces-framework';
import { HttpError, HttpMethod } from '@activepieces/pieces-common';
import crypto from 'crypto';
import { resendAuth } from '../..';
import { resendClient } from '../common/client';
const STORE_KEY = 'webhookData';
const TIMESTAMP_TOLERANCE_SECONDS = 300;
const EVENT_OPTIONS = [
{ label: 'Email Sent', value: 'email.sent' },
{ label: 'Email Delivered', value: 'email.delivered' },
{ label: 'Email Delivery Delayed', value: 'email.delivery_delayed' },
{ label: 'Email Complained', value: 'email.complained' },
{ label: 'Email Bounced', value: 'email.bounced' },
{ label: 'Email Opened', value: 'email.opened' },
{ label: 'Email Clicked', value: 'email.clicked' },
{ label: 'Email Failed', value: 'email.failed' },
{ label: 'Email Received', value: 'email.received' },
{ label: 'Email Scheduled', value: 'email.scheduled' },
{ label: 'Email Suppressed', value: 'email.suppressed' },
{ label: 'Domain Created', value: 'domain.created' },
{ label: 'Domain Updated', value: 'domain.updated' },
{ label: 'Domain Deleted', value: 'domain.deleted' },
{ label: 'Contact Created', value: 'contact.created' },
{ label: 'Contact Updated', value: 'contact.updated' },
{ label: 'Contact Deleted', value: 'contact.deleted' },
];
function verifyResendWebhook({
rawBody,
headers,
signingSecret,
}: {
rawBody: unknown;
headers: Record<string, string>;
signingSecret: string;
}): boolean {
const svixId = headers['svix-id'];
const svixTimestamp = headers['svix-timestamp'];
const svixSignature = headers['svix-signature'];
if (!svixId || !svixTimestamp || !svixSignature) {
return false;
}
const now = Math.floor(Date.now() / 1000);
if (Math.abs(now - parseInt(svixTimestamp, 10)) > TIMESTAMP_TOLERANCE_SECONDS) {
return false;
}
const bodyString = Buffer.isBuffer(rawBody) ? rawBody.toString('utf8') : String(rawBody);
const secretBytes = Buffer.from(signingSecret.replace('whsec_', ''), 'base64');
const toSign = `${svixId}.${svixTimestamp}.${bodyString}`;
const computed = crypto.createHmac('sha256', secretBytes).update(toSign).digest('base64');
return svixSignature.split(' ').some((sig) => {
const [version, sigValue] = sig.split(',');
if (version !== 'v1' || !sigValue) return false;
try {
return crypto.timingSafeEqual(
Buffer.from(sigValue, 'base64'),
Buffer.from(computed, 'base64'),
);
} catch {
return false;
}
});
}
export const emailBounced = createTrigger({
name: 'email_bounced',
classification: 'READ',
auth: resendAuth,
displayName: 'New Event',
description: 'Triggers when a selected email, domain or contact event happens.',
aiMetadata: {
description: 'Fires when one of the subscribed Resend events occurs, such as an email being sent, delivered, bounced, opened, clicked, complained about, or failing, as well as domain and contact lifecycle events. The payload carries the event type and the affected email, domain, or contact. Subscribe to the specific event types you care about.',
},
type: TriggerStrategy.WEBHOOK,
props: {
info: Property.MarkDown({
value: 'The Resend webhook is created for you when the flow is published.',
variant: MarkdownVariant.INFO,
}),
events: Property.StaticMultiSelectDropdown({
displayName: 'Events',
required: true,
options: {
options: EVENT_OPTIONS,
},
}),
},
async onEnable(context) {
const response = await resendClient.sendRequest<{
id: string;
signing_secret: string;
}>({
auth: context.auth.secret_text,
method: HttpMethod.POST,
path: '/webhooks',
body: {
endpoint: context.webhookUrl,
events: context.propsValue.events,
},
});
await context.store.put(STORE_KEY, {
id: response.id,
signingSecret: response.signing_secret,
});
},
async onDisable(context) {
const data = await context.store.get<{ id: string; signingSecret: string }>(STORE_KEY);
if (data?.id) {
try {
await resendClient.sendRequest({
auth: context.auth.secret_text,
method: HttpMethod.DELETE,
path: `/webhooks/${data.id}`,
});
} catch (error) {
if (!(error instanceof HttpError) || error.response.status !== 404) {
throw error;
}
}
}
},
async run(context) {
const data = await context.store.get<{ id: string; signingSecret: string }>(STORE_KEY);
if (
!verifyResendWebhook({
rawBody: context.payload.rawBody,
headers: context.payload.headers,
signingSecret: data?.signingSecret ?? '',
})
) {
return [];
}
return [context.payload.body];
},
sampleData: {
type: 'email.bounced',
created_at: '2024-03-08T09:37:40.912Z',
data: {
created_at: '2024-03-08T09:37:40.911Z',
email_id: '562e3f00-6bad-4910-b1e3-38c2fca1e2ff',
from: '[email protected]',
to: ['[email protected]'],
subject: 'Re: Follow up',
bounce: {
message: "The recipient's mailbox does not exist.",
type: 'Permanent',
subType: 'General',
},
},
},
});