Meteor Js + sms verification - javascript

I'd like to implement a sms verification and I wonder how I can send sms to a given phone number by using Meteor?

I've been able to do this using the meteor-twilio package built from the node library (you'd need a Twilio account). The package exports a global called Twilio that you can use like this:
// server-side code
...
var twilio = Twilio(accountSid, authToken);
this.unblock(); // make the request asynchronously
twilio.sendSms({
to:'+445678984', // any number Twilio can deliver to
from: '+12125551212', // must be your Twilio account phone number
body: 'here is your confirmation'
}, function(err, responseData) { //executed when a response is received from Twilio
if (!err) {
// "responseData" is a JavaScript object containing data received from Twilio.
console.log(responseData.body); // outputs "here is your confirmaton"
}
...
This can be done inside a Meteor.method call.

I'd recommend to look for a SMS API which you can use, by searching for something like javascript sms api. Then you could for example send a verification code by SMS and prompt the code in your app.

Ancient thread, but for searchers who discover this, take a look at:
https://github.com/DispatchMe/meteor-accounts-sms
or
https://github.com/okland/accounts-phone
They are roughly similar solutions that can use Twilio.

Related

Twilio Phone Calling - Fetch User's Keypad Input from a node.js application (not web server)

const args = message.content.split(' ')
const phNumber = args[1]
const ttsMessage = args.slice(2).join(' ')
if (phNumber) {
// Call phNumber and say ttsMessage and collect keypad input code
const call = await TwilioClient.calls.create({
url: 'http://demo.twilio.com/docs/voice.xml',
to: phNumber,
from: process.env.TWILIO_PHONE_NUMBER,
})
}
// somehow get the input the user entered and do something with it here. Maybe by using await or Promises etc...
I want to get the number the user entered and do some stuff with it!
When you create a call resource in the Twilio API, it will initiate a phone call between the two numbers you specified, and also send a webhook request to the url you specified.
This url should point to your webserver where you can handle the webhook request and respond with TwiML instructions. These instructions are how you manage the phone call. One of the TwiML verbs is <Gather> where you can ask for input using DTMF (digits on the phone dialer) or voice.
The user response will be posted back to you with another webhook request, this time to the URL specified in the action attribute from the <Gather> verb.
You cannot provide TwiML instructions using the Twilio API or get the input back, you have to use webhooks.
It's not easy, but you technically could use a websocket connection (or some other way of communication) to connect your webhook server and your node.js app, so that your webhook server can stream the data to your node.js app, and your node.js app could send information back. This way you could exchange information from the call in realtime between the node.js app that created the call, and the webhook server that is managing the call.
I recommend checking out this tutorial on responding to phone calls using node.js.

How to call simple Node.js script from Javascript?

I'm trying to create a basic web application with SMS capability. However, I'm a bit stumped. I'm using Twilio's SMS service, but it utilizes Node.js. Obviously, you can run it with a terminal command such as "node send_text.js", but I'm trying to make the call to the send_text.js file without using the terminal command.
I understand that you can use Express to host your web application, but I'm not too sure how you would go about calling a Node.js file from javascript.
The following is the send_text.js file I would like to call from Javascript.
var twilio = require('twilio');
// Find your account sid and auth token in your Twilio account Console.
var client = new twilio('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');
// Send the text message.
client.messages.create({
to: 'YOUR_NUMBER',
from: 'YOUR_TWILIO_NUMBER',
body: 'Hello from Twilio!'
});
Essentially, I'll detect a change in data using Javascript, and when the change is detected, call the send_text.js file to send a text to the user.
EDIT:
I've tried my own basic implementation of using require, but it doesn't seem to work properly.
Here's what I have in my index.html file:
<script type="text/javascript" src="send_text.js"></script>
<input type="button" onclick="sendText()" value="run external javascript">
And when I try to call the send_text.js, which has the following, I do not get any text message sent:
function sendText()
{
var twilio = require('twilio');
// Find your account sid and auth token in your Twilio account Console.
var client = new twilio('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');
// Send the text message.
client.messages.create({
to: 'YOUR_NUMBER',
from: 'YOUR_TWILIO_NUMBER',
body: 'Hello from Twilio!'
});
}
However, the code does work if send_text.js has the following:
function sendText()
{
alert("Hello world")
}
You can use the child process in Nodejs to call a js script from another js file using childProcess.fork(filepath);
according to the question I understood. should look something like this
let childProcess = require('child_process');
childProcess.fork('./send_text.js);
for more information see
https://nodejs.org/api/child_process.html
The code you're showing us is some example source code. You can copy and paste the relevant parts directly into your Express app.
Twilio developer evangelist here.
You could use a tool like Netlify--here's a tutorial on how to send text messages from a static website using Twilio, Netlify, and Serverless Functions.
You could also use Twilio's serverless environment for web apps called Twilio Functions--here's a tutorial on that using the Twilio CLI as well.
Similarly, you could send a SMS from a Gatsby website with Serverless Functions and React.js, or use an AWS Lambda Function to send a SMS.

Best way to check if Twilio authentication is successful with JS SDK

What is the best way to check if Twilio auht_token, account_sid are correct and sms can be sent, number checked? Some call which doesn't cost extra credits?
E.g. I see https://www.twilio.com/docs/api/rest/usage-records on RESTfull documentation but can't find how to get the same thing with JS SDK. Can't see dedicated endpoint for config checking so looking for anything else.
Environment: NodeJS 8.9
Twilio developer evangelist here.
Most API calls to the Twilio REST API don't cost, particularly those where you retrieve a resource or list resources. Since you mentioned SMS you could, for example, list your latest messages like this:
const client = require('twilio')(accountSid, authToken);
client.messages.list({ limit: 10 })
.then(function(messages) {
console.log("Everything is good!");
})
.catch(function(err) {
console.error("Something went wrong: ", err)
})
Take a look through the API reference and pick one that takes your fancy.
Using JS SDK might be insecure here. Because of that I think they didn't include a method in the JS API which may present the user the account_sid and the auth_token, which may be exploited. I assume you can use a server bridge between your client JS and Twilio API. Like this:
Client makes a JS AJAX request to http://my.domain.tld/checkstatus
Server connects to the Twilio API with C#, PHP, NodeJS or whatever tech it uses
Twilio returns that the credentials and tokens are still valid or expired
Server prepares the client response as true/false or 0/1
Client reads the status and continues or redirects somewhere else.
Edit There's a GET method here which you can also use with JS AJAX call:
https://www.twilio.com/docs/api/rest/usage-records#list-get
which is requested by this format:
/2010-04-01/Accounts/{AccountSid}/Usage/Records/{Subresource}

node.js - Dial to Number with Twilio

I'm building myself a click-to-call website that utilizes Twilio. After I configured in TwiML app and wrote Twilio JavaScript SDK client-side to make request to Twilio, then Twilio will make a POST request to this route of mine:
app.post('/callcenter',function(req,res){
const twilio=require('twilio');
var twiml=new twilio.TwimlResponse();
res.type('text/xml');
twiml.dial({},function(node){
node.number('MY_PHONE_NUMBER');
});
res.send(twiml.toString());
);
This is the most simplified use of Dial in REST API for TwiML that I want to respond to Twilio to make a call to MY_PHONE_NUMBER. But I always ended up hearing voice of "An error occured..."
Please someone help me point out what did I do wrong in this route handler? Server is built in ExpressJS
try this out:
twiml.dial({callerId : process.env.TWILIO_PHONE_NUMBER}, MY_PHONE_NUMBER);
which comes from: https://www.twilio.com/docs/tutorials/walkthrough/browser-calls/node/express
There click to the page call.js.

Where to store javascript send grid api key safely?

Where to store javascript send grid api key safely?
I have an angular2 app and want to store the api key in a safe place.
I'm using firebase and have no server side code like c#.
How is best to do this in angular js without someone loking through source files online to nick it.
This is my current code:
sendGridTestEmail = () => {
//TODO - store this in safe place
var sendgrid_api_key = "";
var sendgrid = require('sendgrid')(sendgrid_api_key);
sendgrid.send({
to: 'test#gmail.com',
from: 'support#gmail.com',
subject: 'Hello Test',
text: 'My first email through SendGrid.'
}, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
}
There is no way safely store any private key on the client.
You can run a server though that sends emails and stores this API key. AppEngine is one of the easiest ways to run a server with Firebase. The only supported Firebase client on AppEngine is Java client.
You can use the Firebase JVM Client with the SendGrid Java SDK, and run it on AppEngine.
Here's a tutorial that takes you through the process. The tutorial does build an Android app, but you can use Android Studio for the Google Cloud tools.

Categories