Category Archives: Nodejs

Bots are artificially intelligent programs that can do many useful things like search for news, summarize web pages, play games, and more. You can start chatting with a bot just like you chat with friends – simply click on the bot and start typing.Microsoft released its skype bot sdk which works great and its fun to have one of your own. As of now we can build skype bot in either C# or nodejs. These SDKs provide features such as dialogs and built-in prompts that make interacting with users much simpler. Lets get started building a simple bot for skype using nodejs.

Steps:

  1. Install node js if you haven’t installed it in your pc yet (http://nodejs.org/).
  2. Create a folder (skypebot).
  3. Now navigate to the folder from command prompt, then type npm init, A new package.json file will be created.
  4. Now create a new file and name it as index.js
  5. Now open command prompt type and enter npm install restify –save ( installs restify in the skypebot directory and save it in the dependencies list).
  6. After it finishes installing, Now type and enter: npm install  –save botbuilder.
  7. After finishing installation please make sure your package.json looks as below.

  8. Now add the below code to your index.js file
  9. Change appId & appPassword with your own appId and appPassword.
  10. Here’s how to get your appId and appPassword.
  11. First login into your Microsoft account.(Click Here), and click on Register Bot. Fill the required details and select your bot image.
  12. For APP ID and Password click on create Microsoft APP ID and password.
  13. Now you have your appId, To generate app Password click on generate a password to continue by copying the appId and password.
  14. Paste your appId in the bot creation page and you can fill rest of the fields except Messaging endpoint field for which you may need a https message endpoint to listen for requests.
  15. You can paste your https endpoint of your server if you have hosted it on any public servers and also You can create one for free which will run locally on your system using tool called ngrok.
  16. You can get the installation steps for ngrok from here.
  17. After running your ngrok on port 8080 you will get a https link from the command line of ngrok. Make sure you don’t close the ngrok after creating address cause then the endpoint address will not listen to any request and you have to create new address with ngrok again.
  18. Now paste this address into endpoint field by clicking edit on your bot and add “api/messages” in the end.
  19. Now you have this link, Use this link to add your bot to your skype contacts for testing the bot as shown below
    .
  20. Now after creation after the bot you have to start your server running, to do that go back to your index.js file and update your appId and password which you configured above.
  21. Now go back to command prompt and type ‘ node app.js ‘ and hit enter to start your application.
  22. After application starts, Open your skype account and message the bot your previously added to your skype contacts.

Now you have your own skype bot up and running.

Are you planning to move your existing GB’s or TB’s data to s3?
Below nodejs script might help you to get started and upload your data to S3.

PreRequsite:

Ensure that aws-sdk node module is configured. If not follow AWS documentation https://aws.amazon.com/sdk-for-node-js/

s3 Options:

Usually accessKeyId and secretAccessKey is enough to make it happening. But if you are behind proxy, use those commented s3Options fields accordingly.

Source Directory

For windows directory delimiter needs tobe delimited, eg c:\\mysite\\uploads

For Linux you can use something like ‘/var/www/site/uploads’

 

Use Case:

A PHR(Patient Health Record) application  gets health reports of patient from various hospitals and labs. Patient Health records are in XML format which needs to be processed  and stored into Database.

Lets see how we can use Amazon AWS to implement this use case.

Step1: Create a Amazon Simple Storage Service (S3) bucket where documents to be uploaded.

Step2: Create a AWS Lambda Function  using NodeJs. Configure  Lambda function to get triggered on   XML upload to S3. Check below screen shot.

Step3: 

Create a Mysql Database using AWS Relational Database‎ (RDS).

Step 4: 

Lambda NodeJs code can be dowloaded from Wavelabs Git Repo.

Package the JS file into zip file and upload the NodeJS Lambda function. Make necessary changes in index.js file as per your requirements.

Ouput:

From AWS dashboard, upload  sample xml file( use file  patient.xml from repo) to s3 bucket.

Connect to Mysql using command line or using workbench and see the results.

It’s easy for us to store/ get data in mongoDB using nodejs through POST and GET Api methods.. when comes to store an image.. it’s different as of storing normal fields data.. while i was trying to store and read the image , it faced it bit tough..

So this post shows you the easy and understandable way of storing and getting the image from MongoDB using nodejs, Express, and Mongoose..

The way i am trying to store the image is like.. i am uploading and storing the image in my project directory(uploads) and using the image path to get store in Mongo collection.. while getting the image, getting the image path from mongoDB and referring it to get the exact image..

My project structure looks like :

node1

  • Uploads is the directory which stores images/files we upload..
  • routes has the class ‘imagefile.js’ which contains the logic of image/file store and get
  • views has the class ‘index,ejs’ which contains Html upload logic
  • app.js is the main class
  • package.json has the information about the packages installed

The code inside the files is as follows :

package.json


{
"name": "my-nodejs-api",
"version": "1.0.0",
"description": "simple api app",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"express":"*",
"body-parser":"*",
"mongoose":"*",
"multer":"*",
"path":"*"
},
"author": "Kamran Athar",
"license": "ISC"
}

app.js 


var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var path = require('path');
app.use(bodyParser.json());

//To get the access for the functions defined in index.js class
var routes = require('./routes/imagefile');

// connect to mongo,
//i have created mongo collection in mlab.com.. the below is my database access url..
//So make sure you give your connection details..
mongoose.connect('mongodb://nodejsapi:nodejsapi@ds041516.mlab.com:41516/mynodejsapp');

app.use('/', routes);

//URL : http://localhost:3000/images/
// To get all the images/files stored in MongoDB
app.get('/images', function(req, res) {
//calling the function from index.js class using routes object..
routes.getImages(function(err, genres) {
if (err) {
throw err;

}
res.json(genres);

});
});

// URL : http://localhost:3000/images/(give you collectionID)
// To get the single image/File using id from the MongoDB
app.get('/images/:id', function(req, res) {

//calling the function from index.js class using routes object..
routes.getImageById(req.params.id, function(err, genres) {
if (err) {
throw err;
}
//res.download(genres.path);
res.send(genres.path)
});
});

app.listen(3000);

console.log('Running on port 3000');

index.ejs


<html>
<head>
 <title>test</title>
</head>
<body>
<form action "/" method="POST" enctype="multipart/form-data">
 <input type="file" name="myimage" ></input>
 <input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>

imagefile.js


var express = require('express');
var router = express.Router();
var multer = require('multer');
var mongoose = require('mongoose');

//path and originalname are the fields stored in mongoDB
var imageSchema = mongoose.Schema({
 path: {
 type: String,
 required: true,
 trim: true
 },
 originalname: {
 type: String,
 required: true
 }

});


var Image = module.exports = mongoose.model('files', imageSchema);

router.getImages = function(callback, limit) {

 Image.find(callback).limit(limit);
}


router.getImageById = function(id, callback) {
 
 Image.findById(id, callback);

}

router.addImage = function(image, callback) {
 Image.create(image, callback);
}


// To get more info about 'multer'.. you can go through https://www.npmjs.com/package/multer..
var storage = multer.diskStorage({
 destination: function(req, file, cb) {
 cb(null, 'uploads/')
 },
 filename: function(req, file, cb) {
 cb(null, file.originalname);
 }
});

var upload = multer({
 storage: storage
});

router.get('/', function(req, res, next) {
 res.render('index.ejs');
});

router.post('/', upload.any(), function(req, res, next) {

 res.send(req.files);

/*req.files has the information regarding the file you are uploading...
from the total information, i am just using the path and the imageName to store in the mongo collection(table)
*/
 var path = req.files[0].path;
 var imageName = req.files[0].originalname;

 var imagepath = {};
 imagepath['path'] = path;
 imagepath['originalname'] = imageName;

 //imagepath contains two objects, path and the imageName

 //we are passing two objects in the addImage method.. which is defined above..
 router.addImage(imagepath, function(err) {

 });

});

module.exports = router;

URL's to use :
http://localhost:3000/   : Html page which shows the button to upload.. and submitting it store the image/ file in project directory and then image path in MongoDB
http://localhost:3000/images : to get All images/files from the MongoDB
http://localhost:3000/images/(mongo collection ID) : To get path of single image