sending texts
This commit is contained in:
@@ -10,6 +10,24 @@ export default async function Home() {
|
|||||||
|
|
||||||
console.log("Session:", session?.user);
|
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 no session, show sign-up and login buttons
|
||||||
if (!session) {
|
if (!session) {
|
||||||
|
|
||||||
|
|||||||
28
React/src/pages/api/sendMessage.ts
Normal file
28
React/src/pages/api/sendMessage.ts
Normal 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`);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user