Currently, I am working on creating a basic function for a website that involves an input field and a button. The idea is to have Twilio API send a message with the content entered in the input field when the button is clicked. Below is the index.js file which contains the function responsible for sending the message. I'm uncertain whether I should use the POST method or GET - any insights are appreciated.
let input = document.querySelector("input").value;
document.querySelector("button").addEventListener("click", whatTheHell);
let whatTheHell = () => {
fetch("/sendSms")
.then((res) => res.json())
.then((res) => console.log(res))
.catch((err) => console.log(err));
};
This is the express.js file containing the Twilio API setup for sending the SMS:
const express = require("express");
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const accountSid = process.env.accountSid;
const authToken = process.env.authToken ;
const app = express();
const client = require("twilio")(accountSid, authToken);
app.use(express.json());
app.use(express.static("public"));
app.get("/sendSms", (req, res) => {
client.messages
.create({
body: "message from me",
messagingServiceSid: "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
to: "NUMBER",
})
.then((message) => {
res.json({ message: message }).done();
});
});
app.listen(3000, () => {
console.log("Server Started");
});
In the 'body: "message from me"' section, I aim to have something like 'body: user.input' where the message content is dynamically taken from user input. I've attempted using the POST method with 'req.body.msg' and 'msg' being the input value, but it seems to not accept the post method successfully.