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:
- Install node js if you haven’t installed it in your pc yet (http://nodejs.org/).
- Create a folder (skypebot).
- Now navigate to the folder from command prompt, then type npm init, A new package.json file will be created.
- Now create a new file and name it as index.js
- Now open command prompt type and enter npm install restify –save ( installs restify in the skypebot directory and save it in the dependencies list).
- After it finishes installing, Now type and enter: npm install –save botbuilder.
- After finishing installation please make sure your package.json looks as below.
123456789101112131415{"name": "skypebot","version": "1.0.0","description": "Sample Skypebot","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "Anudeep","license": "ISC","dependencies": {"botbuilder": "^3.7.0","restify": "^4.1.1"}} - Now add the below code to your index.js file
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051var restify = require('restify');var builder = require('botbuilder');var server = restify.createServer();server.listen(process.env.port || process.env.PORT || 8080, function () {console.log('%s listening to %s', server.name, server.url);});// Skypebot credentialsvar 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('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...");}}); - Change appId & appPassword with your own appId and appPassword.
- Here’s how to get your appId and appPassword.
- First login into your Microsoft account.(Click Here), and click on Register Bot. Fill the required details and select your bot image.
- For APP ID and Password click on create Microsoft APP ID and password.
- Now you have your appId, To generate app Password click on generate a password to continue by copying the appId and password.
- 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.
- 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.
- You can get the installation steps for ngrok from here.
- 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.
- Now paste this address into endpoint field by clicking edit on your bot and add “api/messages” in the end.
- Now you have this link, Use this link to add your bot to your skype contacts for testing the bot as shown below
. - 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.
- Now go back to command prompt and type ‘ node app.js ‘ and hit enter to start your application.
- 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.