How to call simple Node.js script from Javascript? - 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.

Related

Run nodejs script on button click

please can someone explain how I can made this nodejs script run when a button is clicked?
var Twit = require('twit');
var t = new Twit({
consumer_key: '******',
consumer_secret: '******',
access_token: '******',
access_token_secret: '******',
});
t.post('statuses/update', {status:'hello world'},function(err, data, response){
console.log(data);
});
Currently I get:
Uncaught ReferenceError: require is not defined
Thanks
You will need to have a NodeJS process running as a server in which a callback can be hooked to an API endpoint which will then be run when you click on a button in a website.
Client gets HTML with button (from a webserver of any type)
Client clicks on button
HTTP request is sent to NodeJS server at a particular url (yourip.com/yourscript)
NodeJS server calls a function that you assigned to that particular URL
NodeJS is used on the server-side, and can be described as an extension of JavaScript which can do more stuff, like file operations.
Normal JavaScript is used on the client-side.
Therefore, you cannot just use nodeJS in the browser, because the browser is lacking features that nodeJS offers. What you can do is use nodeJS to build an API and connect the API to the client-side script. You will need a server for this to work, though.

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.

Is it possible to allow a user to (after pushing a button) call a specific number?

I'm making an app using apparchitect and I would like to know how to use JavaScript to call a number. I have tried using different ones already uploaded to no avail. Is it possible to allow a user to (after pushing a button) call a specific number?
In the simplified app maker I can use javascript if they do not have the function I want. This is for an iphone app. Please help this app is really important. Thank you.
The easiest answer for your iPhone app is to include the following HTML (as seen from this answer:
tap to call
Tapping the "tap to call" text on your phone will activate the link and send a message to your app to launch to the provided URL. As long as your app is responding to your webview properly and to the OS, everything should work without JavaScript:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: activatedURLString]]
twilio nodejs library. It's incredibly easy.
1.) Make an account with twilio.
2.) Hook up your button with this code:
// Your accountSid and authToken from twilio.com/user/account go in between quotes
var accountSid = "";
var authToken = "";
var client = require('twilio')(accountSid, authToken); // don't forget to install the twilio libary from npm
client.messages.create({
body: "Jenny please?! I love you <3",
to: "+14159352345",
from: "+14158141829"
}, function(err, message) {
process.stdout.write(message.sid);
});
In fact, I feel like being really nice, so I just uploaded a repository to github which does exactly what you are asking. It uses the Meteor framework. To run it, clone the repo, navigate to the folder in your command line and simple type "meteor" into the command prompt. The site will be running in your browser at localhost3000.

Node JS for Google Mirror API

I am trying to implement the Oauth using Node JS for Glass Mirror API amd found this useful. In the "app.js" file, we need to provide the credentials of the project created in the Google developer console.
I have the client ID, client secret but am not able to define what should my callback URL be? What should be in the callback URL script? Logically I understand that there should be a program that accepts the token and runs the further steps.
But how to write one? kindly help.
Thanks in advance
base on the sample code from your link, the callback URL will be
// running in localhost
http://localhost/oauth2callback
// or running on your server
http://yourdomain.com/oauth2callback
when the google redirect to oauth2callback, your server code will run to redirect back to index page
grabToken(req.query.code, failure, function () {
res.redirect('/');
});

Meteor Js + sms verification

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.

Categories