How to build skype bot with nodejs?
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:
- Install node js if you haven’t installed it in your pc yet.
2.Create a folder named “skypebot”.
3.Open command prompt by holding “shift button”and then “right clicking” on blank area.
4.Type “npm init”, A new “package.json” file will be created for you.
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.
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”.
19.Now you have your “appId”, To generate “appPassword” click on generate a password to continue.
20.Copy your “appId” and “appPassword”.
21.Now paste your “appId” right where we started creating bot.
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.
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.
34.After application starts, Open your skype account and message the bot you previously added to your “skype contacts”.
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.
Note: “If my bot isn’t working then that means I’m tweaking it to make it better so try after 24–48hrs, or worst case scenario it has been shut down”
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….