From da6839fc36b7beb212e83b3978fec590b3ee277f Mon Sep 17 00:00:00 2001 From: BGV <26331505+bgv2@users.noreply.github.com> Date: Sun, 30 Mar 2025 00:54:32 -0400 Subject: [PATCH] sending texts --- React/src/app/page.tsx | 18 ++++++++++++++++++ React/src/pages/api/sendMessage.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 React/src/pages/api/sendMessage.ts diff --git a/React/src/app/page.tsx b/React/src/app/page.tsx index 2a20b5d..bdaada2 100644 --- a/React/src/app/page.tsx +++ b/React/src/app/page.tsx @@ -10,6 +10,24 @@ export default async function Home() { console.log("Session:", session?.user); + const handleEmergency = async () => { + // send texts + const response = await fetch("/api/sendMessage", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + message: `yo i need help`, + }), + }); + + if (!response.ok) { + console.error("Error sending message:", response.statusText); + return; + } + } + // If no session, show sign-up and login buttons if (!session) { diff --git a/React/src/pages/api/sendMessage.ts b/React/src/pages/api/sendMessage.ts new file mode 100644 index 0000000..cf5c2da --- /dev/null +++ b/React/src/pages/api/sendMessage.ts @@ -0,0 +1,28 @@ +import { NextApiRequest, NextApiResponse } from 'next'; +import twilio from 'twilio'; + +const accountSid = process.env.TWILIO_ACCOUNT_SID; +const authToken = process.env.TWILIO_AUTH_TOKEN; +const client = twilio(accountSid, authToken); + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === 'POST') { + const { message } = req.body; + + try { + const response = await client.messages.create({ + body: message, + from: process.env.TWILIO_PHONE_NUMBER, + to: '+18777804236', + }); + + res.status(200).json({ success: true, sid: response.sid }); + } catch (error) { + console.error('Error sending message:', error); + res.status(500).json({ success: false, error: 'Failed to send message' }); + } + } else { + res.setHeader('Allow', ['POST']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} \ No newline at end of file