Twilio $_REQUEST['From'] equivalent for node.js - node.js

I'm trying to use 'twilio' to grab the caller ID from an incoming phone call. I managed to do this easily in my call.php file using the following:
$callerId=($_REQUEST['From']);
I have now redirected my twilio phone number to access a different URL so that I can use it with node.js (ie call.php is now call.js). However, I cannot seem to request the ['From'] field in a similar manner as with the .php file. Is this possible? What is the easiest way to grab a caller Id and store it in a variable using node.js?
Any thoughts appreciated.

For the sake of completeness, here's a full example of getting Twilio request parameters using Express. Before running, make sure to install dependencies with npm install twilio express. You might also benefit from reading this blog post introducing the Twilio node.js module.
This code is an example of responding to an inbound phone call:
// Module dependencies
var twilio = require('twilio'),
express = require('express');
// Create an Express webapp, and use a middleware
// that parses incoming POST parameters
var app = express();
app.use(express.urlencoded());
// Create a route that responds to a phone call by saying
// the caller's number
app.post('/call', function(request, response) {
var twiml = new twilio.TwimlResponse();
twiml.say('Hello, you called from ' + request.param('From'));
response.type('text/xml');
response.send(twiml.toString());
});
// Start the app on port 3000
app.listen(3000);

Related

get data from webhook telegram bot with node js

I set the server address for the bot with /setwebhook, now how can I get the data with only node js and https package to access {message :{...} }? (like /getupdates method) but by webhook , probably I mean like the php code but in node js: file_get_contents("php://input") .
Should I use async or https.createServer ? If yes, how and please explain a little
You have to create a server that will handle receiving the data. You can do this by using http.createServer or, more conveniently, using a library such as Express. Simple implementation using Express would look like this:
const app = express();
app.post(`/api/telegram${process.env.TELEGRAM_BOT_TOKEN}`, async (req, res) => {
const message = req.body.message || req.body.edited_message;
...
});
In this example, I use bot token in the webhook url (since I and Telegram are the only entities that know the bot token, I can be sure that the requests to the url come from Telegram). There are better ways to make the webhook secure, such as using secret_token as specified in the docs for setWebhook method.
For development purposes, you can use a service like ngrok to redirect requests from the bot webhook to localhost.
To get an idea of a fully functioning example of implementing webhook with node.js and Express, you can check my open-source project on Github.

Twilio.Device.connect() not sending body to Express

I'm developing in JS, React on the frontend, and Node, Express on the backend.
I have a call button that I press that I want to start an outgoing call via Twilio. I've got a Node server with a couple endpoints, one to generate a token and the other is the voice url.
On the frontend, I'm making a Twilio.Device and have it logging when it's ready. I click on the button, that hit's Twilio's example SDK function that calls Twilio.Device.connect() and I am passing {number: n} into it.
On the backend, the request is made and the voice url is hit, but without a body. When I try logging req.body, it's just an empty object.
When I try hitting the Node server directly from Postman, with the same body ({number: '+11231231122'}) I see everything in the log.
Something is happening between the front and backends, but I cannot figure out what it is.
Twilio developer evangelist here.
Twilio will be sending the body, but as you are using Express it is likely that you aren't parsing that body properly.
Twilio sends requests as url encoded parameters, so you need to use body-parser to parse the body into req.body within a request. Try setting your app like so:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));

How do I read an SMS message sent to my Twilio phone number?

I couldn't find this in Twilio's API docs.
I use NodeJS. When a user texts my Twilio number, I want to use NodeJS to retrieve the message.
Where can I find this in Twilio's API reference?
Twilio developer evangelist here.
When a user texts your Twilio number there are two ways to get that message.
The first is the most efficient and uses webhooks. When you purchase your Twilio number you can configure a URL for Twilio to make a request to every time it receives an SMS for that number. The request will include a bunch of parameters, including the number the message is from and the body of the message. You can receive the request with a Node.js app, here's a quick example using express:
var app = require("express")();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: false});
app.post("/messages", function(req, res) {
console.log(req.body.Body); // the message body
console.log(req.body.From); // the number that sent the message
res.send("<Response/>"); // send an empty response (you can also send messages back)
});
app.listen(3000); // listens on port 3000
When developing an app that uses webhooks like this, I recommend tunneling to your local machine using ngrok.
The other way to get your messages is by using Twilio's REST API. You can list all the messages to your numbers using the messages resource. In Node.js, this would look like:
var client = require('twilio')(accountSid, authToken);
client.messages.list(function(err, data) {
data.messages.forEach(function(message) {
console.log(message.body);
});
});
Let me know if this helps at all.

How to Handle a Coinbase Callback in NodeJS?

How to Handle a Coinbase Callback in NodeJS to Recieve Instant Bit Coin Payment Notifications ?
Please I need example.
Note : I'm using SailsJS MVC Framework.
OK, based on your comment, I will give it a go.
I am assuming you have (or will have) an ExpressJs app.
UPDATE Sorry, I just noticed you're using sailsjs. The below should still be valid but you'll need to adapt it to work with the sails routing engine.
In your app, you need to define the post route:
// the app variable is the express js server
// name the route better than this...
app.post('/coinbase', function(req, res){
var data = req.body;
var orderId = data.order.id;
// etc...
});

How can I respond to incoming Twilio calls and SMS messages using node.js?

In my application I'm using the twilio node.js module to receive an sms, send an sms, receive a call and make an outgoing call. I figured out how to send an sms and make a call. But I don't know how to respond to incoming calls and SMS messages. How can I use node to respond to these?
When Twilio receives a call to your phone number, it will send an HTTP request to a URL you configure in the admin console:
What Twilio expects in return from this HTTP request is a set of XML instructions called TwiML that will tell Twilio what to do in response to the call. For example, let's say that you wanted to respond to a phone call by saying "thank you" and then playing a music file. If you wanted to do this in node, you might consider using this node library and the express framework to send a TwiML response:
var twilio = require('twilio'),
express = require('express');
// Create express app with middleware to parse POST body
var app = express();
app.use(express.urlencoded());
// Create a route to respond to a call
app.post('/respondToVoiceCall', function(req, res) {
//Validate that this request really came from Twilio...
if (twilio.validateExpressRequest(req, 'YOUR_AUTH_TOKEN')) {
var twiml = new twilio.TwimlResponse();
twiml.say('Hi! Thanks for checking out my app!')
.play('http://myserver.com/mysong.mp3');
res.type('text/xml');
res.send(twiml.toString());
}
else {
res.send('you are not twilio. Buzz off.');
}
});
app.listen(process.env.PORT || 3000);
Good luck - hope this is what you were looking for.

Resources