Twilio client js status busy/cancelled no fired - node.js

I'm using the Twilio client js in my browser in order to make calls.
In the server I build the Twiml:
const dial = twiml.dial({
/*action: `http://270311bb.ngrok.io/twilio/callend`,*/
callerId: availableNum.phoneNumber || availableNumbers[0].phoneNumber
}, (n) => {
n.number(request.body.number, {
statusCallback: `http://270311bb.ngrok.io/twilio/${request.body.agentId}/status`
});
});
I also tried with the 'action' parameter.
In my status/callend route I get the callstatus only as completed or no-answer, even if the called number is busy, or not connected.

Twilio developer evangelist here.
The StatusCallbackEvent's that you can receive from a call are only initiated, ringing, answered, or completed. Busy or not connected calls are also completed calls. Can you try querying the call from the API when you receive the completed event and see what status the actual call in.

Related

Dialogflow fulfillment with Please wait functionality

I am working on custom event integration which will call one by one three event as I am using third party API which is taking more then 11 sec, I need a functionality where user will receive message from first intent saying "Please wait", meanwhile our 2nd and 3rd intent will wait for the API response, as soon as we get the response from our API we will return it to Dialogflow Agent.
In the below code I want to send message(Please wait) from the Demo intent then the other two custom event will call intent(followOne, followUpTwo) this intent will be process for specific time, after that it will send one more message with actual API response.
async function Demo(agent){
// here we call our API and wait to get an response and will set that response in Redis server
await customsleep(1500);
agent.add('Please wait');
agent.setFollowupEvent('followUpOne');
}
async function followOne(agent){
await customsleep(4500);
agent.setFollowupEvent('followUpTwo');
}
async function followUpTwo(agent){
// in this intnet will get response from Redis server which we stored from intnet Demo, This response will return to the user
await customsleep(4700);
agent.add('here we go');
}
Is there anyway to implement this kind of functionality where we can send message(Please wait). Once we get the response from API we can return it to the Dialogflow Agent.
To my knowledge you can't send any message while following up an event. (it will be ignore)
If you want to provide a message you should :
Step 1 :
- call the third party api
- send a message with a quick reply : "Please Wait" + qr: "Click here to display the answer"
Step 2 :
- check if the third party api has answered
- yes : return the answer
- no : follow up a waiting event

Delivered Status in Status Callback MMS not working

it seems like the status delivered is not available for MMS. I get the sent status in my callback for a MMS just fine, but not delivered. Why? I use node.
My call looks like this :
var sms = {
to,
from,
body: message.message,
statusCallback: 'callbackurl',
mediaUrl: [
'imageurl1',
'imageurl2',
]
}
clientTwilio.messages.create(sms)
.then((sms) => {
});
I receive the MMS as expected.
Twilio developer evangelist here.
When Twilio alerts you of the status of the message, it does so using the data it gets back from the network you send the message with. If the delivery information is not available, Twilio can only mark the message as sent.

How to use Twilio to send SMS, and wait for response from phone to finish request in NodeJS

I know how to send an SMS via Twilio to a phone number specified in the request body, however I need a way for this route to wait for the user to respond with a text message. I need to capture that messages body and send it as a response.
router.post('/', function(req, res){
var customerNumber = req.body.mobileNumber;
var twilioNumber = process.env.TWILIO_NUMBER;
client.messages
.create({
to: '+1' + customerNumber,
from: twilioNumber,
body: 'message to user',
provideFeedback: 'true',
}, function(err, message){
if(err) res.send('message err: ' + err);
else{
// wait 10-20 seconds for user to respond
res.send("A response from the user");
}
})
});
I also know how to listen to responses with Twilio, but this doesn't work because I need the above route to respond with the message I get from the user in the route below.
router.post('/sms-response', function(req, res){
var twilio = require('twilio');
var twiml = new twilio.twiml.MessagingResponse();
twiml.message('thanks for your feedback');
res.writeHead(200, {'Content-Type':'text/xml'});
res.end(twiml.toString());
});
I've been looking for a way to do this all day with no success at all. I appreciate your help!
I dont know if that is possible yet, I went through their documentation and didnt see that feature yet. I would suggest using a web socket like socket.io. When message comes in you can just pass the response in through the socket to the user.
Twilio developer evangelist here.
While this could technically be possible I strongly recommend against it. You mention that you might be waiting for 10-20 seconds, but users are much more fickle than that, as are mobile networks. So getting a response may take much longer that that or even never happen. Then you'd be left with a long running connection to your server waiting for a response that might never come.
As Hammer suggested, it would be better to return a response to the user straight away, saying something like "We're waiting for your response", then connecting that user's browser to your server via a websocket or server sent event stream. When you receive the incoming message on your webhook endpoint, you can then send the information down the websocket or event source and react to that in the front end.
Let me know if that helps at all.

Node.JS and Asynchronous Messaging

Ok, this is not what you think it is, I am not asking for help with the async/wait pattern or asynchronous programming I am well versed with those. I am however querying whether something is possible within a Node.JS Express service.
The Scenario
I have a web service which is developed in Node.JS and uses Express.JS to expose some REST endpoints that a client can connect to and send a POST request. For the most part these are Synchronous and will create a SOAP message and send that on to an external service and receive an immediate response which can then be returned to the client, all really simple stuff which is already implemented. So what's your point I hear you say, I am coming to that.
I have a couple of POST interactions that will build a SOAP message to send to an Asynchronous external endpoint where the response will be received asynchronously through an inbound endpoint.
Option 1: What I am looking for in these cases is to be able to build the SOAP message, create a listener (so I can listen for the response to my request), and then send the request to the external service which immediately returns a 200.
Option 2: When I setup the service I want to also setup and listen for incoming requests from the external service whilst also listening for REST requests from the internal service.
The Question
Is either option possible in Node and Express? and, if so how would one achieve this?
NOTE: I know its possible in C# using WCF or a Listener but I would like to avoid this and use Node.JS so any help would be greatly appreciated.
First of all check node-soap if it fits your needs.
Option 1: What I am looking for in these cases is to be able to build the SOAP message, create a listener (so I can listen for the response to my request), and then send the request to the external service which immediately returns a 200.
Here's a very basic non-soap service implementation.
let request = require('request-promise');
let express = require('express');
let app = express();
//Validate the parameters for the request
function validateRequest(req) { ... }
//Transform the request to match the internal API endpoint
function transformRequest(req) { ... }
app.post('/external', function(req, res) {
if(!validateRequest(req))
return res.status(400).json({success: false, error: 'Bad request format');
res.status(200).send();
let callbackUrl = req.query.callback;
let transformedRequest = transformRequest(req);
let internalServiceUrl = 'https://internal.service.com/internal'
request.post(internalServiceUrl, {body: transformedRequest}).then(function (internalResponse){
//Return some of the internal response?
return request.get(callbackUrl, {success: true, processed: true});
}).catch(function (e) {
request.get(callbackUrl, {success: false, error: e});
});
});
Option 2: When I setup the service I want to also setup and listen for incoming requests from the external service whilst also listening for REST requests from the internal service.
There is no "listening" in http. Check socket.io if you need realtime listening. It uses websockets.
Your other option is to poll the internal service (say if you want to check for its availability).

Twilio Client Making a Call status

I am writing a node.js application to make a call or send sms to users.
However, after making a call or sending SMS, I wish to know its status.
client.makeCall({
to:'+358xxxxxxxxx',
from: '+15005550006',
url: "https://demo.twilio.com/welcome/voice/",
}, function(err, responseData) {
}
I know on responseData, but its status indicates 'Queued'
I wish to know the real call status after the actual call takes place.
Is there anyway to do so?
I haven't used twilio node.js client, but here's some that might help you -
You are not getting the call status because the voice call or SMS wont complete immediately when the API call is returned. You need to make subsequent requests again until the status is complete (polling) or configure twilio/pass parameters so that twilio will notify you when the call is actually complete (push).
To let twilio push the status to your server, pass application_sid or status_callback field while making the call request as explained in the API doc http://www.twilio.com/docs/api/rest/making-calls.
To manually request the call status, do the get request from the client after few seconds (or whatever time you think call takes to complete) perhaps using a timer until you get the status you want. http://www.twilio.com/docs/api/rest/call
Something like below: (Note: I have not tested or verified this)
client.calls(<sid>).get(function(err, call) {
console.log(call.status);
});

Resources