nodejs email.send doesnt work inside function - node.js

In the code below email with subject 'OK' is sent but not email 'not OK'? The second one is inside a function.
var JSFtp = require("jsftp");
var config = require('./config/config');
var email = require('./modules/mailer');
var ftp = new JSFtp({
host: config.ftphost
});
email.send(config.emailTo, "emailText", "OK");
ftp.list(config.ftpdir, function(err, res) {
console.log(config.emailTo,res);
email.send(config.emailTo, res, "not OK");
//process.exit();
});

it appears you are fetching and scraping a log - - wrong approach.
read up on sendmail(1). Packages like Postfix, Exim, Sendmail have protocols which provide direct access to the server responses and you can respond as necessary.

Related

Change issue closing pattern for gitlab user account

I want to close an issue matching the file name pushed with Issue title (My source files are named with unique integers, e.g. 34521.cpp and there are corresponding issues on Gitlab e.g. Problem #34521).
How can I do so?
The default pattern is not suitable as I have 2000+ issues and I do not want to refer issues with the issue ID's each time. I want it to be automated. So I was checking the page :
Change the issue closing pattern.
It says I need to have access to the server where gitlab is installed. Does that mean I cannot change the issue closing pattern for Gitlab cloud's user account hosted at http://gitlab.com ?
You can't define a custom closing pattern on gitlab.com, only on your own hosted gitlab instance. But what you can do is to use webhooks to listen on push events on a remote server. You can then parse the commit messages yourself and take decision on closing issues. You can use Gitlab API to close issue on your server instance (with a hard coded access token)
This can be tested locally using an http tunnel like ngrok
The following nodejs script starts a server serving a /webhook endpoint. This webhook endpoint is called when any push occurs on your repo.
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const to = require('await-to-js').to;
const port = 3000;
const projectId = "4316159";
const accessToken = "YOUR_ACCESS_TOKEN";
const app = express();
app.use(bodyParser.json())
app.post('/webhook', async function(req, res) {
console.log("received push event");
let result, err, closeRes;
for (var i = 0; i < req.body.commits.length; i++) {
for (var j = 0; j < req.body.commits[i].added.length; j++) {
filenameWithoutExt = req.body.commits[i].added[j].split('.').slice(0, -1).join('.');
[err, result] = await to(axios({
url: `https://gitlab.com/api/v4/projects/${projectId}/issues?search=#${filenameWithoutExt}`,
method: 'GET',
headers: {
"PRIVATE-TOKEN": accessToken
}
}));
if (err) {
console.log(err);
} else {
if (result.data.length !== 0) {
//close those issues
for (item in result.data) {
console.log(`closing issue #${result.data[item].iid} with title ${result.data[item].title}`);
[err, closeRes] = await to(axios({
url: `https://gitlab.com/api/v4/projects/${projectId}/issues/${result.data[item].iid}?state_event=close`,
method: 'PUT',
headers: {
"PRIVATE-TOKEN": accessToken
}
}));
if (err) {
console.log(err);
} else {
console.log(`closing status : ${closeRes.status}`);
}
}
} else {
console.log("no issue were found");
}
}
}
}
res.sendStatus(200);
});
app.listen(port, () => console.log(`listening on port ${port}!`))
In the above you need to change the access token value & projectId. Also note that above code will check only added file, you can modify it to include updated or deleted file matching your requirements.
Launch ngrok on port 3000 ngrok http 3000 & copy the given url in integrations sections of your repo :
Now when you add any file it will check for the filename without extension and search all issue with within title #filename_without_extension and close it right away

How to crawl data from yelp.com without block our IP

I am using node packages of cheerio and nightmare for crawling from " Yelp.com ". I am retrieved data from Yelp.com.
But yelp has blocked my IP.
Please any one can provide solution or suggestions.Thanks in advance
Here is my code
var Nightmare = require('nightmare');
var fs = require('fs');
var http = require('http');
var cheerio = require('cheerio');
var request = require('request');
function yelpmenuitemsscrap(url)
{
// console.log(url);
var menuitems = new Nightmare();
menuitems.goto(url);
menuitems.wait();
menuitems.evaluate(function () {
var objs = [];
$('div.menu-sections div.media-block.menu-
item').each(function(index){
objs.push( $(this).find('div.media-story h4').text().trim());
});
return objs;
},function (html) {});
menuitems.run(function(err, nightmare) {
if (err)
{
return console.log(err);
}
else
{
console.log(nightmare);
};
});
}
IP block is done because all the requests are being generated from the same server IP address which appears to be a hack attack on the server. In such requirements the data crawl should be done from the clients who are accessing the applications from different IP addresses. This will generate the traffic from different IP addresses and Yelp will not block it.
The other option is to use multiple HTTP Proxy servers which generate requests from all the different servers randomly so as not to block any specific IP address.
Another option is to use something like http://www.screen-scraper.com
If you don't mind using and api, you can try https://gimmeproxy.com which has nice wrapper gimmeproxy-request.
It automatically gets proxies from GimmeProxy and re-routes requests through another proxy when one fails.
Example how to make request with this wrapper:
const setup = require('gimmeproxy-request').setup;
const request = require('gimmeproxy-request').request;
setup({
api_key: 'your api key',
query: 'get=true&cookies=true&country=US&supportsHttps=true&maxCheckPeriod=1800&minSpeed=10', // additional gimmeproxy query parameters
retries: 5, // max retries before fail
test: (body, response) => body.indexOf('captcha') === -1 && response.statusCode === 200 // test function
});
request('https://example.com', {
timeout: 10000 // additional request parameters, see https://github.com/request/request
},
function(err, res, body) {
console.log('err', err)
console.log('res', res)
console.log('body', body)
process.exit()
});

Ionic Cordova FileTransfer upload options or How to set value to be available on nodejs express middleware using req.value?

I am new to Ionic. I am trying to upload a file to server and basically what I need is to push a json object to be sent with the FileTransfer.upload and be able to recober this object on the server side using an express middleware from the request:
req.value = ${value_sent_by_ionic_client_upload};
I am current setting the object as a params entry and I can see the object in the FileUploadOptions but the object is not accessible on the server side as a value of request.
Current client side:
var options = new FileUploadOptions();
options.filename = fileURL.substr(fileURL.lastIndexOf('/') + 1);
var params = {};
params.user = StorageService.getUser();
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURL,
encodeURI("http://192.168.192.62:3000/api/meals/picture"),
pictureUploaded,
uploadError,
options);
On server side express middleware:
var user = req.user;
but user is undefined on server side.
How to I pass the user using cordova FileTransfer.upload to make it available from a req.user call?
this is a bit late but maybe it can help someone looking for a code to upload
by the way i don't think cloudinary provides any option to send users
here you're using an upload made with jquery while you're using angularjs for ionic which is not really optimal i might suggest a code like this
$scope.uploadimage = function()
{
var options = new FileUploadOptions()
options.fileKey = "image";
$cordovaFileTransfer.upload('Link-to-your-server', $scope.yourpicture, options).then(function(result) {
console.log("File upload complete");
console.log(result);
$scope.uploadResults = "Upload completed successfully"
}, function(err) {
console.log("File upload error");
console.log(err);
$scope.uploadResults = "Upload failed"
}, function (progress) {
// constant progress updates
console.log(progress);
});
}

twilio say something to caller (node.js)

Hey guys I am working on something and have following Problem :
ACTUAL AIM - If someone calls, I want to let the voice say: "Try to reach someone" and then call some numbers out of an array. ( cant do that atm because i need to fake a call )
ATM AIM - Thats why I at least want to say something to the one who answers the twilio call, that I can be sure it "would" work.
So i faked a call by sending the url that twilio would send (if it gets a call) via a localhost http server. So far so good, my phone gets called. But the woman dont say what I wanted her to say... She sais : Thank you for trying our documentation and then waiting music is following.
AND: The call.status is ALLWAYS queued, or I dont catch it at the right place :/ , Remember my phone is ringing so it should have at least the status ringing ...
This is what I have at the moment:
requestHandler.js:
var querystring = require("querystring");
var emergency = require("./emergency");
var twilio = require('twilio');
function callRequest(response) {
var resp = new twilio.TwimlResponse();
resp.say({voice:'woman'}, 'ahoy hoy! Testing Twilio and node.js');
console.log("call incomming ! EMERGENCY 1 1 11 !");
//emergency.handleIncommingCall();
response.writeHead(200, {"Content-Type": "text/xml"});
response.end(resp.toString());
}
exports.callRequest = callRequest;
server.js:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname, response, request);
}
http.createServer(onRequest).listen(1337);
console.log("Server has started");
}
exports.start = start;
exports.http = http;
router.js:
function route(handle, pathname, response, request) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname](response, request);
}else{
console.log("No request handler found for " + pathname);
response.writeHead(404, {"Content-Type": "text/html"});
response.write("404 Not found");
response.end();
}
}
exports.route = route;
index.js:
var server = require("./server");
var router = require("./router");
var requestHandler = require("./requestHandler");
var handle = { };
handle["/demo.twilio.com/welcome/voice/"] = requestHandler.callRequest;
server.start(router.route, handle);
emergency.js:
var twilio = require('twilio');
var accountSid = 'cant tell ya';
var authToken = "cant tell ya";
var client = require('twilio')(accountSid, authToken);
var firstCascadeNumber = "cant tell ya";
var secondCascadeNumber;
var privateNumber; //enter privateNumber here
var twiml = new twilio.TwimlResponse();
function handleIncommingCall (){
//twilio should say : we contact team internet pls wait
//twilio should make music
call(1,firstCascadeNumber);
//cb if staus ist nicht rangegangen call(2)
}
function call (cascade,cascadeNumber){
client.makeCall({
url: "http://demo.twilio.com/docs/voice.xml",
to: cascadeNumber,
from: "cant tell ya"
}, function(err, call) {
if(err){
console.log("ERROR:", err.message);
}else{
console.log("calling " + cascadeNumber);
console.log("status: " + call.status);
if(cascade == 1){
//twiml.say("Hello this i a test. Thank you for calling.");
console.log("should say what i want it to say ! god damn it ");
console.log("status: " + call.status);
//if user geht ran
//startConference()
//if user geht nicht ran
//call(2,secondCascadeNumber)
}else if(cascade == 2){
//if user geht ran
//startConference()
//if user geht nicht ran
//inform caller that no one is there
}else{
console.log("Error: cascade doesnt exsist");
}
}
});
}
function openConference(call,from,to){
//call.joinConference([roomName, option, cbOnEnd])
}
exports.handleIncommingCall = handleIncommingCall;
Twilio developer evangelist here.
You're most of the way there, but you've not quite set your application out right here.
When you make the call, the callback you get only refers to whether the call started correctly. It is not a callback that you need to return TwiML to in order to tell Twilio what to do with the call.
Instead, what happens is when Twilio makes the call, it will send an HTTP request to the URL you supply when you make the call in the first place. That URL should be in your application and available to Twilio.
This blog post on using Twilio with Node.js should be able to show you what I mean by all of that and set you up with a good way of testing this locally too.
Edit
Thanks for updating your code.
Your problem is that you are not telling Twilio to ask you what to do with the call once it connects.
When you create a call with the API you need 3 parameters, the number to call, the number to call from and a URL. When Twilio connects the call it will make an HTTP request to the URL you supply asking what to do next and this is where you supply some TwiML to tell Twilio what to do with the call.
Currently you are supplying this URL: http://demo.twilio.com/docs/voice.xml
If you click through the demo Twilio URL there, you will see the TwiML that is being returned and why you hear a message you are not expecting. Because the URL is not yours, your application cannot take control of the call.
You need to send a URL that points at a route in your application, and that route needs to respond with the TwiML you want. You can expose your local server to Twilio using a tool called ngrok which will allow you to test this.
I recommend you follow the tutorial I linked before, this tutorial on using ngrok to text your incoming HTTP requests from Twilio and this tutorial on creating a click to call application with Twilio.
In the case of your application, instead of trying to handle "/demo.twilio.com/welcome/voice/" which is not a URL you have control of, you should handle, say:
var handle = { };
handle["/calls"] = requestHandler.callRequest;
and then use ngrok to create a tunnel to your application and pass the URL to client.makeCall like this:
function call (cascade,cascadeNumber){
client.makeCall({
url: "http://YOUR_NGROK_SUBDOMAIN.ngrok.io/calls",
to: cascadeNumber,
from: "cant tell ya"
}, function(err, call) {
if (err) {
console.log("Call could not be made", err);
} else {
console.log("Call created successfully. Call ID:", call.sid);
}
}
}
Let me know if that helps.

Correct way to respond to client on Node.js?

This being my first attempt at building a server...
I have set up a server to handle a contact form submission that also includes a predefined captcha string.
When the server receives the contact form, if the captcha string is the one expected then I want it to simply respond with a JSON of the parsed contact form query using response.end(JSON.stringify(parsedURL));
If the captcha string is wrong, I want the server to respond "saying" the captcha was wrong, so that the client asks the user to try again. But I don't know how to do that.
ON THE SERVER :
var httpServer = http.createServer(function (request, response)
{
if (/\/contactform\?....../.test(request.url))
{
var parsedURL = url.parse(request.url, true);
var name = parsedURL.query.name;
var email = parsedURL.query.email;
var subject = parsedURL.query.subject;
var enquiry = parsedURL.query.enquiry;
var captcha = parsedURL.query.captcha;
if (captcha !== "testing")
{
// send a "bad" response to the client and include the message "bad captcha"
}
else response.end(JSON.stringify(parsedURL.query));
}
}).listen(8080);
ON THE CLIENT :
$.ajax({
url: "/contactform?..............",
success: function(msg)
{
console.log(msg);
},
error: function(msg)
{
// the "bad captcha" response should be handled here right ?
console.log(msg); // which should be equivalent to console.log("bad captcha");
}
});
When I use response.end(JSON.stringify(parsedURL)); the client (jQuery) considers that a "success".
How should I respond from the server so that the "error" part of the ajax request on the client is executed?
Or is the "error" part supposed to handle only cases where the server doesn't respond at all, i.e. when something has gone horribly wrong server-side, like an exception, a real error, and not cases where simply my evaluation on the server doesn't have the expected outcome ?
Should I instead useresponse.end(...); in both cases as in :
ON THE SERVER :
var httpServer = http.createServer(function (request, response)
{
if (/\/contactform\?....../.test(request.url))
{
var parsedURL = url.parse(request.url, true);
var name = parsedURL.query.name;
var email = parsedURL.query.email;
var subject = parsedURL.query.subject;
var enquiry = parsedURL.query.enquiry;
var captcha = parsedURL.query.captcha;
var response = JSON.stringify(parsedURL.query);
if (captcha !== "testing") response = "bad captcha";
response.end(response);
}
}).listen(8080);
ON THE CLIENT :
$.ajax({
url: "/contactform?..............",
success: function(msg)
{
console.log(msg); // msg will either be the stringified object or "bad captcha"..
}
});
In other words, when a request is successfully received on the server but the server wants to let the client know that something was missing or whatever, should the response from the server be sent as an "error" (i.e. handled by the "error" block on the client's ajax code) or as a "success" with the appropriate message saying what actually happened?
I think what you need to do is set headers of your response.
Here is an example:
var body = 'Sorry!';
response.writeHead(404, {
'Content-Length': body.length,
'Content-Type': 'text/plain' });
Refer to http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers for more information.

Resources