Skip to Content

Let’s do something fun with WhatsApp and SAP HANA Cloud Platform. We will build an app whereby you can take a picture from your smartphone and send it via WhatsApp, save it to HANA database and finally view the picture from the browser. I named it as “Whana” = WhatsApp and Hana 🙂

/wp-content/uploads/2014/08/b10_520892.jpg

Prerequisites

Diagram

Presentation1.jpg

Yowsup Installation & Configuration

After you have downloaded the Yowsup library, copy the folder yowsup-master to the Python folder (in my case is C:\Python34). You also need to download the python-dateutil and copy dateutil folder to C:\Python34\Lib.

Now go to https://coderus.openrepos.net/whitesoft/whatsapp_sms to get the WhatsApp code. Enter your dedicated mobile phone number and select SMS.

/wp-content/uploads/2014/08/b3_520561.jpg

If no error, you will get the SMS message and note down the number:

WhatsApp code 192-828

Under C:\Python34\yowsup-master\src, copy the config.example to yowsup-cli.config, and indicate the following:

cc=country code; for example: 65

phone=your phone number: for example: 651234567 (without +)

leave the id and password blank at the moment.

Execute the following command to register with WhatsApp:

python C:\Python34\yowsup-master\src\yowsup-cli –register 192-828 –config C:\Python34\yowsup-master\src\yowsup-cli.config

If no error, you will get the following message:

status: ok

kind: free

pw: S1nBGCvZhb6TBQrbm2sQCfSLkXM=

price: 0,89

price_expiration: 1362803446

currency: SGD

cost: 0.89

expiration: 1391344106

login: 651234567

type: new

Copy the password S1nBGCvZhb6TBQrbm2sQCfSLkXM= and paste into pw field in yowsup-cli.config. In the end your yowsup-cli.config will look like this:

cc=65

phone=651234567

id=

password=S1nBGCvZhb6TBQrbm2sQCfSLkXM=

Now let’s test to send a message to this phone number 6597312234. Execute the following command:

C:\Python34>python C:\Python34\yowsup-master\src\yowsup-cli –send 6597312234 “Test message” –config C:\Python34\yowsup-master\src\yowsup-cli.config

If no error, you will see the following result and the message will be sent to 6597312234:

Authed 651234567

Sent message

I have added the functions to save the image received from the WhatsApp to the local folder: C:\java\imageWA. Just overwrite the CmdClient.py in the  folder Example and downloader.py in folder Media.

Java Folder Monitoring


On this part, we will create the Java program to monitor the folder where the Yowsup stored the WhatsApp image (i.e., C:\java\imageWA) and check if there is any image file. If there is, the program will read the file and insert into the HANA database.

Below is the snippet of the java code:


static final String IMGFolder = "C:\\java\\imageWA";
String INSERT_PICTURE = "INSERT INTO \"NEO_CG2SX3P5XHHQEO58DKM7BWU0V\".\"p1940803061trial.fd2.data::mytable\" VALUES(?,?)";
public synchronized void insert(String fileName) throws Exception, IOException, SQLException{
  FileInputStream fis = null;
  PreparedStatement ps = null;
  Date currentDate = new Date();
  String s = Long.toString(currentDate.getTime() / 1000);
  System.out.println(s);
  try {
  System.out.println("filename: " + fileName);
  File file = new File(IMGFolder + "\\" + fileName);
  fis = new FileInputStream(file);
  ps = conn.prepareStatement(INSERT_PICTURE);
  ps.setString(1, s);
  ps.setBinaryStream(2, fis, (int) file.length());
  int rowsInserted = ps.executeUpdate();
  conn.commit();
  if (rowsInserted > 0) {
  System.out.println("A new record was inserted successfully!");
  }
  if(file.delete()){
  System.out.println(file.getName() + " is deleted!");
  }
  else{
    System.out.println("Delete operation is failed.");
    }
  } finally {
  ps.close();
  fis.close();
  }
  }


















HANA Cloud Platform Setup

I will go through the key important files. You can find the complete source code on GitHub: https://github.com/ferrygun/Whana

/wp-content/uploads/2014/08/b4_520965.jpg

mytable.hdbtable

We need to create a table, mytable.hdbtable in SAP HANA database to store the image from the WhatsApp.

mytable.hdbtable

table.schemaName = “NEO_CG2SX3P5XHHQEO58DKM7BWU0V”;

table.tableType = COLUMNSTORE;

table.description = “My Table”;

table.columns = [

          {name = “File_Name”; sqlType = NVARCHAR; nullable = true; length=20;},

          {name = “File_Content”; sqlType = BLOB; nullable = true;}

];

/wp-content/uploads/2014/08/b5_520787.jpg

GetImage.xsjs


Create the GetImage.xsjs to retrieve the image binary stream from the HANA database:


  var id = $.request.parameters.get('id');
    var conn = $.db.getConnection();
    try {
        var query = "Select \"File_Content\" From \"NEO_CG2SX3P5XHHQEO58DKM7BWU0V\".\"p1940803061trial.fd2.data::mytable\" Where \"File_Name\" = " + id;
        var pstmt = conn.prepareStatement(query);
        var rs = pstmt.executeQuery();
        rs.next();
        $.response.headers.set("Content-Disposition", "Content-Disposition: attachment; filename=filename.jpg");
        $.response.contentType = 'image/jpg';
        $.response.setBody(rs.getBlob(1));
        $.response.status = $.net.http.OK;
    } catch (e) {
        $.response.setBody("Error while downloading : " + e);
        $.response.status = 500;
    }
    conn.close();



























GetFileName.xsjs

Create the GetFileName.xsjs to list down all the image file names so user can select  from the SAPUI5 table:


var query = "Select \"File_Name\" From \"NEO_CG2SX3P5XHHQEO58DKM7BWU0V\".\"p1940803061trial.fd2.data::mytable\" ";
function close(closables) {
    var closable;
    var i;
    for (i = 0; i < closables.length; i++) {
        closable = closables[i];
        if (closable) {
            closable.close();
        }
    }
}
function getFileName() {
    var FNameList = [];
    var connection = $.db.getConnection();
    var statement = null;
    var resultSet = null;
    try {
        statement = connection.prepareStatement(query);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            var fname = {};
            fname.file_name = resultSet.getString(1);
            FNameList.push(fname);
        }
    } finally {
        close([resultSet, statement, connection]);
    }
    return FNameList;
}
function doGetFileName() {
    try {
        $.response.contentType = "application/json";
        $.response.setBody(JSON.stringify(getFileName()));
    } catch (err) {
        $.response.contentType = "text/plain";
        $.response.setBody("Error while executing query: [" + err.message + "]");
        $.response.returnCode = 200;
    }
}
doGetFileName();




























Embed an Image in HTML Code


In order to view the image, we will call the function GetImage.xsjs and embed the picture into the HTML code with base64 encoding using the following tag:


<img src="data:{mime};base64,{data}" alt="My picture"/>
























where {Mime} is in ‘image/imagetype’ format (eg. ‘image/jpg’ or image/png) and {data} is the base64 encoded

Here is an example when I viewed the source code of the “WhatsApp” image in the Chrome browser:

/wp-content/uploads/2014/08/b6_520805.jpg

/wp-content/uploads/2014/08/b7_520806.jpg

The actSearch function in the view controller will call the GetImage.xsjs based on the given image ID (filename) and encode the content in the base64 format.


actSearch: function(fname) {
        var xmlHTTP = new XMLHttpRequest();
        xmlHTTP.open('GET', '../fd2/image/GetImage.xsjs?id=' + fname, true);
        xmlHTTP.responseType = 'arraybuffer';
        xmlHTTP.onload = function(e) {
            var arr = new Uint8Array(this.response);
            var raw = String.fromCharCode.apply(null, arr);
            var b64 = btoa(raw);
            var dataURL = "data:image/jpeg;base64," + b64;
            document.getElementById("image").src = dataURL;
        };
        xmlHTTP.send();
    }






















Running the App


Firstly you need to open the tunnel to the SAP HANA Cloud Platform. Under folder neo-javaee6-wp-sdk, execute the following command:

neo open-db-tunnel -h “hanatrial.ondemand.com” -u “p1940803061” -a “p1940803061trial” –id “fd2”

Replace “p1940803061” with your HANA trial account ID and “fd2” with your HANA instance.

/wp-content/uploads/2014/08/b8_520844.jpg


If successfull, you will get the the password: Df2sxp5HH8H6BGv and user: DEV_5GKYRR9GKKT9NTV5RD7U0DGMG. Update the pwd and user value in the config.properties:

user=DEV_5GKYRR9GKKT9NTV5RD7U0DGMG

pwd=Df2sxp5HH8H6BGv


Run the Yowsup. Execute below command:

python C:\Python34\yowsup-master\src\yowsup-cli –interactive <phone_number> –wait –autoack –keepalive –config C:\Python34\yowsup-master\src\yowsup-cli.config


/wp-content/uploads/2014/08/b9_520867.jpg


And finally run the Java Folder Monitoring app:

java FolderMonitor



Demo Video

Summary

In other case, I have also built a CCTV home security and upload the video clip to the DropBox (or HANA). Here is the video:

Hope your are enjoy “Whana” and thank you for reading my blog. Please let me know if you have any question/comment.

To report this post you need to login first.

16 Comments

You must be Logged on to comment or reply to a post.

  1. Dibyajyoti Nanda

    Hi Ferry,

    Thanks for a beautiful blog.

    I am trying with this command but i am getting below error.

    would you please help me with this

    Thanks & Regards

    Dibyajyoti Nandapython.JPG

    (0) 
    1. Ferry Gunawan Post author

      Hi Dibyajyoti,

      Thanks. Are you able to run the yowsup itself? try to run this first:

      python C:\Python34\yowsup-master\src\yowsup-cli

      Any error message came out?  Please also uncomment the line 207 in yowsup-cli code:

      parser.add_argument(‘-D’,’–dbus’, help=’Start DBUS interface’, action=”store_true”, required=False, default=False)

      Regards,

      Ferry Djaja

      (0) 
      1. Dibyajyoti Nanda

        Thanks Ferry,

        I have tried with below command

        python C:\Python33\yowsup-master\src\yowsup-cli


        but it is also throwing same error


        FYI-I have changed below information in the file

        cc=country code; for example: 65

        phone=your phone number: for example: 651234567 (without +)


        Thanks & Regards

        Dibyajyoti Nanda

        (0) 
        1. Ferry Gunawan Post author

          Which version of python that you are using currently? I am using Python 3.4.1

          When executing python C:\Python34\yowsup-master\src\yowsup-cli, you should see the below message:

          /wp-content/uploads/2014/08/b11_521025.jpg

          Please attach your yowsup-cli code so I can try on my side.

          (0) 
    1. Ferry Gunawan Post author

      Hi Manna,

      It looks like there is no yowsup-master in your Python folder.

      Please copy yowsup-master to your C:\Python34 and also copy the dateutil folder to C:\Python34\Lib. Let’s do that first and let  me know the outcome.

      /wp-content/uploads/2014/08/b12_521697.jpg

      Regards,

      Ferry Djaja

      (0) 
      1. Manna Das

        Hi Ferry,

        Thanks for the prompt reply. I have copied yowsup-master to c:\python34, only dateutil folder needs to copied or complete python-dateutil-2.0 folder? please see the screenshot below

        Untitled.jpg

        Regards

        Manna Das

        (0) 
  2. BRUNO LUCATTELLI

    Some people think this is the kind of app that it’s just a toy. To me, it’s a serious POC! Thanks for sharing this great content with all of us. Very inspiring!

    (0) 
      1. Bhanu Pamu

        Hi Ferry,

        Indeed, its a great idea. I am a newbie to the cloud platform and I am already excited about how it can be leveraged. Your post is apt for starting up. I will try. Thank you. Cheers.

        (0) 
  3. Alexandre Cardoso

    Hello guys,

    I’m trying to follow this post, but I have some problems.

    Today I am using yowsup2 and now I’m trying to save photos received from yowsup the PC (for example: C:\java\imageWA).

    I’m getting that answer.

    How do I change the way?

    Image.jpg

    (0) 

Leave a Reply