Customer send a voicemail if user doesn't answer, in an incoming phone call

53 Views Asked by At

I am making an application where a user can buy a masked number and customer can contact that user via masked number. Now what I want to do is that when a customer calls a user's masked number, the call gets forwarded or redirected to his original number. I am doing it successfully But if the user is unreachable, busy or does not answers the phone, the customer should be able to record and send a voicemail to the user's masked number, which gets stored in our DB and we forward that voicemail to user's email using speech-to-text twilio API,

I am using a ngrok webhook, which is already configured in our masked number using twilo console.

I need some assistance in this regard .

Here is my code just for reference

const { getNumberWithoutUser, updateQuota } = require("../db/dbOperations");
const { sendMessageNotificationEmail } = require("../emailing/email");
const { sendSms, client } = require("../twilioFunctions");
const { appendMessage } = require("../db/messagingCollectionUtils");
const { appendCall } = require("../db/callsCollectionUtils");

const twilio = require("twilio");
const express = require("express");
const router = express.Router();
router.post("/webhook/voice", async (req, res) => {
  const { To, From, CallStatus } = req.body;

  const [numbers] = await getNumberWithoutUser(To);
  if (!numbers) return res.status(400).send("User does not own this number");

  const type = numbers.numbers.subscriptions.find(
    (subscription) => subscription.active
  ).type;
  const isToPrimaryPhone =
    numbers?.numbers?.settings?.forwarding?.toPrimaryPhone;
  const primaryPhoneNumber =
    numbers?.numbers?.settings?.forwarding?.primaryPhoneNumber;
  console.log("CallStatus", CallStatus);

  if (isToPrimaryPhone) {
    switch (CallStatus) {
      case "ringing":
        // --> Add any welcome message (optional)
        const twiml = new twilio.twiml.VoiceResponse();
        // twiml.say("Petras Brinko Test message");
        twiml.dial(primaryPhoneNumber);
        await updateQuota(numbers._id, To, "callForwarding", type);
        res.type("text/xml");
        return res.send(twiml.toString());

      case "completed":
        await appendCall(numbers._id, To, From, req.body);
        return res.send("success");
    }
  }
  res.send("Call Forwarding is disabled or package has finished");
});

I tried following TWILIO REST API documentation but got no relevant response

1

There are 1 best solutions below

0
stevennic On

But if the user is unreachable, busy or does not answers the phone, the customer should be able to record and send a voicemail to the user's masked number, which gets stored in our DB and we forward that voicemail to user's email using speech-to-text twilio API

To manage/handle the outbound dialed call, you would want to configure a timeout. This attempts the dial but stops the attempt if the callee doesn't answer within the expected timeframe.

Example:

twiml.dial({
  timeout: 7
}, '415-123-4567');

After that point, you are able to implement a recording of your own that you can configure as needed. Callbacks can be used to pass the completed recording or completed transcription to the proper email address as needed.

Example:

twiml.record({
  recordingStatusCallback: "https://example.com/callback/masked_number/",
  maxLength: 20,
  finishOnKey: '*'
});

Twilio - Voice - TwiML - Dial - Timeout attribute: https://www.twilio.com/docs/voice/twiml/dial#timeout Twilio - Voice - TwiML - Record: https://www.twilio.com/docs/voice/twiml/record