How to build skype bot with nodejs?

Usama Tahir
May 4, 2016 · 6 min read
Still don’t have your own skype bot? Lets build one for you now.

I just realized that microsoft made changes to their “skype bot sdk” and the old steps to create “skype bot” are not working anymore, So I updated this article to new steps so that you guys can build working “skype bot” again. Read this article again if you didn’t able successfully create your own “skype bot” before.

Microsoft just released its “skype bot sdk” a couples of months ago and its amazing, The bot works great and it’s fun to have one of your own.

For now you can build “skype bot” in either C# or NodeJS.

If you are interested in learning how to build skype bot in Node Js, Then you are at a right place because in this article am gonna teach you just that. Don’t worry if you don’t know much of ‘Node Js’ because you can still follow along and create your own working “skype bot” in no time.

Lets get started……

Steps:

  1. Install node js if you haven’t installed it in your pc yet.

http://nodejs.org

2.Create a folder named “skypebot”.

Create a new empty folder

3.Open command prompt by holding “shift button”and then “right clicking” on blank area.

Click on open command prompt here

4.Type “npm init”, A new “package.json” file will be created for you.

Type npm init and hit enter

5.Now create a new file and name it as “app.js

6.Now open command prompt type and enter: “npm install — save restify”.

7.After it finishes installing,
Now type and enter: “npm install — save botbuilder”.

Note:This sign means — (dash dash) = 2 times dash

8.Save this file in the same directory.

9. Now you are done installing dependencies.

10.Please make sure your “package.json” file looks likes this.

{
"name": "bot",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"botbuilder": "^3.1.1",
"restify": "^4.1.1"
}
}

11.Now paste this code into “app.js file”.

var restify = require('restify');
var builder = require('botbuilder');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 8080, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: "Your App ID Here",
appPassword: "Your App Password Here"
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
//Bot on
bot.on('contactRelationUpdate', function (message) {
if (message.action === 'add') {
var name = message.user ? message.user.name : null;
var reply = new builder.Message()
.address(message.address)
.text("Hello %s... Thanks for adding me. Say 'hello' to see some great demos.", name || 'there');
bot.send(reply);
} else {
// delete their data
}
});
bot.on('typing', function (message) {
// User is typing
});
bot.on('deleteUserData', function (message) {
// User asked to delete their data
});
//=========================================================
// Bots Dialogs
//=========================================================
String.prototype.contains = function(content){
return this.indexOf(content) !== -1;
}
bot.dialog('/', function (session) {
if(session.message.text.toLowerCase().contains('hello')){
session.send(`Hey, How are you?`);
}else if(session.message.text.toLowerCase().contains('help')){
session.send(`How can I help you?`);
}else{
session.send(`Sorry I don't understand you...`);
}
});

12.Change “appId” & “appPassword” with your own “appId” and “appPassword”.

13.Here’s how to get your “appId” and “appPassword”.

14.First login into your microsoft account.

15.Goto this link: Click Here

16.Click on Register a bot.

Click register button to register your bot

17.Now fill in your required details, select your bot image.

18.Now what else you need is your “appId” and “appPassword”, For that click on Create Microsoft “App ID & password”.

Click on create microsoft App ID and password

19.Now you have your “appId”, To generate “appPassword” click on generate a password to continue.

You got your App ID and Pass

20.Copy your “appId” and “appPassword”.

21.Now paste your “appId” right where we started creating bot.

Now paste your App Id you copied here in Microsoft App Id input field

22. Fill in the rest of the fields, except “messaging endpoint” field.

Note: your bot handle can be any name like: my_assistant_bot

23.You don’t have any “https” messaging endpoint to listen for requests.

24.You can create one for free that will run locally on your system using tool called “ngrok”.

25.Download “ngrok” : Click Here

26.You will get a zip file, Extract that zip file into any folder and then open the file named “ngrok.exe

27.Type and enter: “ngrok http 8080

28.Now copy the “https address” 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.

Copy the https address of localhost:8080

29.Now paste this address into endpoint field and add “api/messages” in the end.

30.Click on register.

31.Now you have this link, Use this link to add your bot to your “skype” contacts for testing the bot.

32.Paste your “appId” and “appPassword” into “app.js file”.

33.Now go back to command prompt and type “node app.js” and hit enter to start your application.

type node app.js and hit enter to run your app

34.After application starts, Open your skype account and message the bot you previously added to your “skype contacts”.

Here you go your bot is ready

Great! Now you have your own skype bot up and running….

Checkout Skype Bot node.js documentation to learn more about building skype bots with node.js. | Click Here ,,,

Wanna checkout my simple bot ?? Click on this link & add it to your skype contacts and start testing it now.

Take your time and play with your newly created bot and have fun, If you wanna make it more intelligent then feel free to dive into your “app.js” code change it however you want. Make sure you have your backup copy just in case if you mess things up.

Note: This method is for just creating a simple bot and if you want your bot to be more intelligent then use this Microsoft service called “LUIS” and make your bot more intelligent so it can understand the complex pattern of language.

Thanks for reading this article, Have a nice day….

Usama Tahir

Written by

Full Stack Web Developer, A UI Designer & Digital Marketer. A blogger who love to share solutions to common problems. @amjustsam

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade