I have created an API using a code ready made from a twilio blog post which I have modified. The code's use is to parse an incoming mail that is sent to "anything"@parse."mydomain".gr and forward it to a mobile number as an sms message. The problem is that if a mail cannot be forwarded for some reason (e.g. text body is more than 1600 characters) it keeps trying and sends error messages in the logs, or sends the messages long after the actual email was forwarded. So I was wondering if it is possible to delete requests already made, and/or "drop" future requests if not successfully fulfilled after say 10000ms. Also I would like to know if I can see the "pending" requests that exist right now somehow. Below is the code I am using.
const util = require('util');
const multer = require('multer');
const addrs = require("email-addresses");
const twilio = require('twilio');
//rev.1
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);
//end rev.1
module.exports = async (req, res) => {
const client = twilio(process.env.TWILIO_ACCOUNT_SID,process.env.TWILIO_AUTH_TOKEN);
await util.promisify(multer().any())(req, res);
const from = req.body.from;
const to = req.body.to;
const subject = req.body.subject;
const body = req.body.text;
//Using email-addresses library to extract email details.
const toAddress = addrs.parseOneAddress(to);
const toName = toAddress.local;
const fromAddress = addrs.parseOneAddress(from);
const fromName = fromAddress.local;
//Sending SMS with Twilio Client
client.messages.create({
to: '<mynumber>',
from: process.env.TWILIO_PHONE_NUMBER,
body: `Message from:${fromName}\n${body}`
}).then(msg => {
console.log(msg)
res.status(200).send(msg.sid);
}).catch(err => {
console.log(err);
});
//rev.1
//define cancelation function
function cancelation () {
client.messages(msg.sid)
.update({status: 'canceled'})
.then(message => console.log(message.to));
};
//execute cancelation after 10000 milliseconds (10 seconds)
setTimeout(cancelation, 10000);
//define delete_message function
function message_delete () {
client.messages(msg.sid).remove();
}
//execute cancelation after 10000 milliseconds (10 seconds)
setTimeout(message_delete, 10000);
//end rev.1
};