-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require("express");
const bodyParser = require("body-parser");
const twilio = require("twilio");
const User = require("./models/config");
require("./db");
const app = express();
// Middleware
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Twilio credentials
const accountSid = process.env.accountSid;
const authToken = process.env.authToken;
// API route for sending Twilio message
app.post("/send-twilio-message", async (req, res) => {
try {
const { body, from, to, uid } = req.body;
const findUser = await User.findOne({ user_id: uid });
if (!findUser || !findUser.twilio_enabled) {
return res.status(401).json({
error: true,
message: "Your Twilio is not enabled",
});
}
const client = new twilio(accountSid, authToken);
const message = await client.messages.create({ body, from, to });
res.status(200).json({
message: "Message sent successfully",
messageId: message.sid,
});
} catch (error) {
console.error("Error sending Twilio message:", error);
res.status(500).json({
error: true,
message: "Internal Server Error",
});
}
});
module.exports.handler = app;