sending texts

This commit is contained in:
BGV
2025-03-30 00:54:32 -04:00
parent e83162c347
commit da6839fc36
2 changed files with 46 additions and 0 deletions

View File

@@ -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) {

View File

@@ -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`);
}
}