Can't send sms with Twilio, Node.js and Sequelize - javascript

I have a program which respond automatically a sms when receive an income message.
It works when I don't use Sequelize to save data to the database. But when I add the code below, the twiml.message(msg) is never executed.
The problem is when call twiml.message(msg) inside the then it doesn't work. So how to resolve this problem? Thanks
Info.create(param)
.then(info => {
var msg = 'Numero: ' + info.id +
' Nom:' + info.nom +
' Date: ' + info.datesms);
var twiml = new MessagingResponse();
twiml.message(msg);
});

Related

Why this is showing type error in NodeJS while using ping module?

I'm trying to make a basic app to ping an IP. So my HTML form takes one input IP and post it to NodeJS.
I'm using ping module to get the results. it works fine if I enter an IP statically but when I try to get IP by HTML form it just breaks.
This is how my code looks.
app.post("/",function(req,res){
console.log(req.body);
var ip= req.body.ip;
console.log(typeof(ip));
var msg;
var hosts = [ip];
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){
console.log(isAlive);
msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
});
});
res.write(msg);
res.send();
});
This is what comes on console
The way I see it, this is what is happening:
You issue a ping request. Note, that it takes a callback function as a parameter. This suggests that this is an asynchronous I/O operation.
You execute
res.write(msg);
res.send();
At the time msg is still undefined and therefore I'm guessing that res.write(msg) is in fact the line 30 of app.js file that the error is all about
Only then the callback function is executed, but it's too late
I would recommend changing it as follows
app.post("/",function(req,res){
console.log(req.body);
const ip= req.body.ip;
console.log(typeof(ip));
ping.sys.probe(ip, function(isAlive){
console.log(isAlive);
const msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
console.log(msg);
res.write(msg);
res.send();
});
});

Log url response with js

I have this js function that posts to a url and I need to record the response that the site returns. I am wondering what the best way to do this. Here is a sample of a url string and how the post response works. I am needing to make a log file to keep track of the responses of the pings we send. Any help would be greatly appreciated.
Sample url string.
https://www.arcamax.com/cgi-bin/autosub?email=test#email.com&scextcode=bigredtestping&source=4679
function loadIframePixels(e)
{
if(inEmailpixelArray('arcamax')) {
document.getElementById('ifrm').src = 'https://www.arcamax.com/cgi-bin/autosub?email=' + e + '&fname=&lname=&listid.1=15&listid.2=52&source=' + arcmxid + '&ipaddr=' + _localip + '&ts=' + Date.now() + '&leadid=LEADID&scextcode=' + _subid;
}else {
_can_redir = true;
}
}

Event Stream data not showing in Chrome console

I have an EventSource listener on my frontend calling a complicated backend scheme. This code block is written in Typescript.
import * as EventSource from 'eventsource';
private streamData() {
let source = new EventSource('http://localhost:3000/websocket/server/stream');
source.onopen = (e) => {
};
source.onmessage = (e) => {
console.log('id: ' + (<any>e).lastEventId + '; type: ' + e.type + ' data: ' + e.data);
};
}
And I send back the following response to my server :
res.write('id: ' + this.messageId++ + '\n');
res.write('type: message\n');
res.write('data: ' + message + '\n\n');
res.flush();
Now, on the Chrome console, I get all the data needed.
However, on the xhr monitor, I cannot see the EventStream data.
I get the info on my frontend, so this is not a blocking issue for me, but may pose some problems later in debugging.
I had the same issue. The data does not show up when you are using the eventsource polyfill only when you use the built in browser implementation of the EventSource class.

retry sql connection using JavaScript

I know using javascript is not the best way to connect to a SQL server but this is for an in-house application. I connect using the following:
dbNSConnection = new ActiveXObject("ADODB.Connection") ;
var sNSConnectionString="Driver={SQL Server};TrustedConnection=Yes;Server=" + sNSServer + ";Database=" + sNSDatabase + ";UID=" + sNSUID + ";PWD=" + sNSPWD;
dbNSConnection.Open(sNSConnectionString);
How can I make sure connection has gone thru and how do I retry if not connected?
Isn't there any other way you could get this done on the codebehind, and pass the results to the javascript? This may be an example of what I'm talking about originally.
If not, can't you simply do the following?
try
{
dbNSConnection = new ActiveXObject("ADODB.Connection") ;
var sNSConnectionString="Driver={SQL Server};TrustedConnection=Yes;Server=" + sNSServer + ";Database=" + sNSDatabase + ";UID=" + sNSUID + ";PWD=" + sNSPWD;
dbNSConnection.Open(sNSConnectionString);
//Run rest of query here
}
catch(err)
{
var message = err.message;
//Finish building errors message here
}

invalid_request from getToken in Javascript from Node.js

I have the following code in a node.js server application
app.get('/AuthorizeGoogle.html',function(req,res) {
var auth = new googleapis.OAuth2Client(config.google_login.client_id, config.google_login.client_secret, config.google_login.redirect_uri);
var queryData = url.parse(req.url,true).query;
var code = encodeURIComponent(queryData.code);
console.log('Authorization Request recieved ');;
console.log('Retrieiving token');
auth.getToken(code,function(err,tokens) {
console.log('Retrievied token ');
console.log(tokens);
console.log('**** ERROR ****');
console.log(err);
console.log('Calling setCredentials');
auth.setCredentials(tokens);
console.log('*****FINISHED!!!!!');
});
res.send('Authorization recieved ' + queryData.code)
});
This is called when Google returns and the user has authorised access to their Google account.
I get a code. However when auth.getToken() I am getting invalid_request. I have done this successfully in C#, but I am now moving to more open source tools hence moving the project to Node.js
Thanks in advance
OK - I looked again at the page suggested and did some refactoring of my code and that worked. I think what may have been the problem was the Url used to get the token in the first place.
I was already initialising the oauth2Client
var OAuth2Client = googleapis.OAuth2Client;
var oauth2Client;
oauth2Client = new OAuth2Client(config.google_login.client_id, config.google_login.client_secret, config.google_login.redirect_uri);
The required Client Id, secret and redirect Url have been defined in a configuration file
So first all I changed the way I was generating that url. First off I set the Url to Login to Google to GoogleLogin.html which executes the following when the server receives a request for this page.
app.get('/GoogleLogin.html',function(req,res) {
var scopes = "";
// retrieve google scopes
scopes += config.google_login.scopes.baseFeeds + " "
scopes += config.google_login.scopes.calendar + " "
scopes += config.google_login.scopes.drive + " "
scopes += config.google_login.scopes.driveMetadata + " "
scopes += config.google_login.scopes.profile + " "
scopes += config.google_login.scopes.email + " "
scopes += config.google_login.scopes.tasks
var url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
res.writeHead(302, {location: url});
res.end();
});
This first building a string of the scopes then generating the Authorization Url before redirecting to the generated url
When Google redirects back to the site the following is executed
app.get('/AuthorizeGoogle.html',function(req,res) {
// var auth = new googleapis.OAuth2Client(config.google_login.client_id,config.google_login.client_secret, config.google_login.redirect_uri);
var queryData = url.parse(req.url,true).query;
var code = queryData.code;
console.log('Authorization Request recieved ');;
console.log('Retrieving token');
oauth2Client.getToken(code,function(err,tokens) {
console.log('Retrieved token ');
console.log(tokens);
console.log('**** ERROR ****');
console.log(err);
console.log('Calling setCredentials');
oauth2Client.setCredentials(tokens);
console.log('*****FINISHED!!!!!');
});
res.send('Authorization recieved ' + queryData.code)
});
And this is now returning the Token and Refresh token correctly. Since it is the same code the only thing I can was wrong was the original call to Google.
I think your best bet would be to research what is done in the following github example: https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js

Categories

Resources