sending texts
This commit is contained in:
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