Thursday, June 23, 2016

Create Skype bot in Java - Part 1


Skype has recently introduced skype-bots which will allow your computer to talk to your contacts.
I have tried to create simple skype bot. The docuentation provided in skype is not very easy and clear.
I have referred to many soruces to put things to work.

In hight level the skype bot messaage flow is as given below





There are two things to be done.
1. Registering your bot in Skype Bot Platform - This you have to do it in skype website.
2. Bot  - This is your server application which sends and receives the messages to your contacts. This application needs to be run in some cloud like Azure, AWS.

Using skype bots, we can send/receive messages to individual contacts and also to groups. We can even make voice calls from our bots.

In this post, I will explain how to create skype bots which can send and receive message to individual contacts.

1) Let us create the bot
I am going to create my Bot on Java. Its just an REST based application which will receive message from Skype platform and prints it on the console.
I am using Jersey API here.

a) Create a Dynamic web project. File -> New -> Dynamic Web Project.

b) Fill the details as below

 * Project name- HelloBot
 * Target Rutime - Here I am using Tomcat 7
 * Dynamic Web Module Version - 3
 * Configuration -  Default Configuration for Tocat 7

Then click next and again next.
In the last screen enable the checkbox to generate web.xml file and click finish.

Now the project is created in the workspace.

c) Let us add the required jar libraries to the project.
d) Copy the below jar files to the location HelloBot/WebContent/WEB-INF/lib
<copy the list>
e) Refesh the project so that the added jars will be available like below.

f) Create a package "com.vijay.bot" and create a class as "HelloBotServer.java"
g) Copy the following code listing to HelloBotServer.java





Add the tomcat server. Open the tomcat config page by double clicking the tomcat server.
Under 'Server Location' section, choose "Use Tomcat Installation" as shown in the pic below. Save the changes made.



h) All set. Its time to test :). Start the server and observe the log for errors. Once the server is up and running, let us hit our test method first from browser.
http://localhost:8080/HelloBot/botserver/hello

 


i) Hurrah. Its success. Now the other actual method which is going to receive our message is a POST method. So, let us use Postman chrome extension to fire a POST request.



j) Yes. The message has reached our Bot server and it prints it in the console.
Received Message:hello world bot


So, our bot is ready to receive message from Skype bot platform. Let us register our bot in Skype platform in the next section.
But before that, we have to do one more thing. Skype platform expects a publicly accessible webhook URL. But our application is running in a local node. (i.e., our url is a local network resource. Cannot be acccessed from public). Skype platform recommends to run our bot in cloud services like Azure or AWS, so that it can be accessed via public URL.

Here, to enable public access, we are going to use ngrok. ngrok allows to access our application via a public url. It creates a tunnel from dynamic public ip to our local socket.

Start ngrok:
Download the ngrok and run it with the below command.

./ngrok http 8080
which displays the following screen



which allows us to access our bot which is running in port 8080 via public url appears in the screen above.

Let us test the 'hello' method via ngrok url like below
https://e85e3e1a.ngrok.io/HelloBot/botserver/hello



and it is success. So, we have publicly accessible bot url (webhook) is ready.
                       

Let us see how to register in Skype bot platform in the next post.
Registeration in Skype Bot Platform

1 comment: