Skip to content

Commit

Permalink
feat: add support for whatsapp messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-saini committed Aug 25, 2024
1 parent ed90e54 commit 8ec42be
Showing 1 changed file with 62 additions and 29 deletions.
91 changes: 62 additions & 29 deletions src/app/api/assistant/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { openai } from '@ai-sdk/openai';
import { convertToCoreMessages, streamText } from 'ai';
import { convertToCoreMessages, generateText, streamText } from 'ai';
import MessagingResponse from 'twilio/lib/twiml/MessagingResponse';
import { z } from 'zod';

import { getPersonAvailability } from '@/app/api/assistant/tools/get-person-availability';
Expand All @@ -10,40 +11,72 @@ import { scheduleMeeting } from '@/app/api/assistant/tools/schedule-meeting';
export const maxDuration = 30;

export async function POST(req: Request) {
const url = new URL(req.url);
const channel = url.searchParams.get('channel');

const isWhatsapp = channel === 'whatsapp';

if (isWhatsapp) {
const body = await req.text();
const message = new URLSearchParams(body).get('Body');

if (!message) return new Response('message is required', { status: 404 });

const result = await generateText({
model: openai('gpt-4-turbo'),
messages: convertToCoreMessages([
{
role: 'user',
content: message,
},
]),
tools,
});

const twiml = new MessagingResponse();
twiml.message(result.text);

return new Response(twiml.toString(), {
headers: { 'Content-Type': 'text/xml' },
});
}

const { messages } = await req.json();

const result = await streamText({
model: openai('gpt-4-turbo'),
messages: convertToCoreMessages(messages),
tools: {
getPersonName: {
description: "Parse the string to extract the user's name.",
parameters: z.object({
message: z
.string()
.describe('The message from which to extract the name.'),
}),
execute: getPersonName,
},
getPersonAvailability: {
description: "Find the user's availability for a meeting.",
parameters: z.object({
name: z.string().describe('user name'),
}),
execute: getPersonAvailability,
},
scheduleMeeting: {
description: 'Schedule a meeting with the user.',
parameters: z.object({
host: z.string().describe('meeting host name'),
invitee: z.string().describe('meeting invitee name'),
startTime: z.string().describe('meeting start time in format: '),
endTime: z.string().describe('meeting end time in format: '),
}),
execute: scheduleMeeting,
},
},
tools,
});

return result.toDataStreamResponse();
}

const tools = {
getPersonName: {
description: "Parse the string to extract the user's name.",
parameters: z.object({
message: z
.string()
.describe('The message from which to extract the name.'),
}),
execute: getPersonName,
},
getPersonAvailability: {
description: "Find the user's availability for a meeting.",
parameters: z.object({
name: z.string().describe('user name'),
}),
execute: getPersonAvailability,
},
scheduleMeeting: {
description: 'Schedule a meeting with the user.',
parameters: z.object({
host: z.string().describe('meeting host name'),
invitee: z.string().describe('meeting invitee name'),
startTime: z.string().describe('meeting start time in format: '),
endTime: z.string().describe('meeting end time in format: '),
}),
execute: scheduleMeeting,
},
};

0 comments on commit 8ec42be

Please sign in to comment.