Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Deleting pending requests in Javascript

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
};
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda