From cbe251565bb855af4b55dbf81ccd5cde7033ebca Mon Sep 17 00:00:00 2001 From: Eric Abouaf Date: Thu, 15 May 2025 16:32:30 +0200 Subject: [PATCH] Add methods to handle the global unsubscribe list --- src/services/sendgrid.ts | 30 ++++++++++++++++++++++ src/tools/index.ts | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/services/sendgrid.ts b/src/services/sendgrid.ts index faa5841..53b71cf 100644 --- a/src/services/sendgrid.ts +++ b/src/services/sendgrid.ts @@ -316,4 +316,34 @@ export class SendGridService { }); return response.body; } + + // Global Unsubscribe + async isInGlobalUnsubscribe(email: string) { + const [response] = await this.client.request({ + method: 'GET', + url: `/v3/asm/suppressions/global/${email}` + }); + return response.body.hasOwnProperty('recipient_email'); + } + + // Add to Global Unsubscribe + async addToGlobalUnsubscribe(email: string) { + const [response] = await this.client.request({ + method: 'POST', + url: `/v3/asm/suppressions/global`, + body: { + recipient_emails: [email] + } + }); + return response.body; + } + + // Delete from Global Unsubscribe + async deleteFromGlobalUnsubscribe(email: string) { + const [response] = await this.client.request({ + method: 'DELETE', + url: `/v3/asm/suppressions/global/${email}` + }); + return response.body; + } } diff --git a/src/tools/index.ts b/src/tools/index.ts index 511d707..58b1b83 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -370,11 +370,65 @@ export const getToolDefinitions = (service: SendGridService) => [ }, required: ['list_id', 'emails'] } + }, + { + name: 'is_in_global_unsubscribe', + description: 'Check if an email address is in the global unsubscribe list', + inputSchema: { + type: 'object', + properties: { + email: { + type: 'string', + description: 'Email address to check' + } + }, + required: ['email'] + } + }, + { + name: 'add_to_global_unsubscribe', + description: 'Add an email address to the global unsubscribe list', + inputSchema: { + type: 'object', + properties: { + email: { + type: 'string', + description: 'Email address to add to the global unsubscribe list' + } + }, + required: ['email'] + } + }, + { + name: 'delete_from_global_unsubscribe', + description: 'Delete an email address from the global unsubscribe list', + inputSchema: { + type: 'object', + properties: { + email: { + type: 'string', + description: 'Email address to delete from the global unsubscribe list' + } + }, + required: ['email'] + } } ]; export const handleToolCall = async (service: SendGridService, name: string, args: any) => { switch (name) { + case 'is_in_global_unsubscribe': + const isInGlobalUnsubscribe = await service.isInGlobalUnsubscribe(args.email); + return { content: [{ type: 'text', text: JSON.stringify(isInGlobalUnsubscribe, null, 2) }] }; + + case 'add_to_global_unsubscribe': + await service.addToGlobalUnsubscribe(args.email); + return { content: [{ type: 'text', text: `Email address ${args.email} added to the global unsubscribe list` }] }; + + case 'delete_from_global_unsubscribe': + await service.deleteFromGlobalUnsubscribe(args.email); + return { content: [{ type: 'text', text: `Email address ${args.email} deleted from the global unsubscribe list` }] }; + case 'delete_contacts': await service.deleteContactsByEmails(args.emails); return { content: [{ type: 'text', text: `Successfully deleted ${args.emails.length} contacts` }] };