AEM OSGI and PYTHON(yowsup) | Community
Skip to main content
Level 6
January 4, 2016
Solved

AEM OSGI and PYTHON(yowsup)

  • January 4, 2016
  • 6 replies
  • 4508 views

 Hi,

 

I want to create a page in AEM that uses yowsup. Yowsup is an api written in python for messaging to whatsapp.

I would like to write an application extension of the Yosup in Python, then call that in API.

 

Am I even trying something reasonable.

 

Regards

Clive Stewart

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by kautuk_sahni

As fa as I know about Yowsup, Yowsup is a python library that enables you build application which use WhatsApp service.
yowsup-cli is a command line interface to Yowsup library.
We need to use yowsup-cli for sending or reciveing messages.

So you can create a OSGI Sling service from which you can call Python scripts.
Option 1:-
//Link:- http://blog.xebia.in/2013/11/25/yowsup-the-whatsapp-api/

To embed it in Java, all you have to do is call this command from Java's process builder framework. For this, first create a shell script to send message. Name it `sendMessage.sh` which looks like: 

                     #!/bin/bash
         python yowsup-cli -c config.example -s $1 "$2"

         
         String message = "The message goes here";
        String[] sendCommand = { "/bin/bash", “path/to/sendMessage.sh”, "<to number including cc>", message };
        ProcessBuilder processBuilder = new ProcessBuilder();processBuilder.directory(new File(<YOWSUP_HOME>));
        processBuilder.environment().put("PYTHONPATH", <YOWSUP_HOME>);
        processBuilder.command(sendCommand);
        System.out.println(processBuilder.environment());
        Process process = processBuilder.start();
        process.waitFor();

  8. Run the above program and your message will be sent to the intended receiver.
  9. You can read the ` process.getInputStream()` to get the output of the script you executed. For ex- 

                    InputStream stream = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }

Option 2:-
//Link:- http://stackoverflow.com/questions/16447410/how-to-execute-python-script-from-java
Execute Python script from Java?

Put your command to a shell script and execute that shell script with .exec() or
You can do something similar to the following

String[] cmd = {
        "/bin/bash",
        "-c",
        "echo password | python script.py '" + packet.toString() + "'"
    };
Runtime.getRuntime().exec(cmd);


I am not sure about if we can do something with REST APIs, if we have rest framework for Yowsup, then we can call the APIs from OSGI Sling Service.


I hope this would help you.

Thanks and Regards
Kautuk Sahni

6 replies

Jitendra_S_Toma
Level 10
January 4, 2016

   Clive,

  "I want to create a page in AEM that uses yowsup.   "  : Explain how do you want to create AEM page?. And how yowsup will be used. When you say, you would like to extend yowsup application, will it happen as third party application?.

---Jitendra

Clive Stewart wrote...

 Hi,

 

I want to create a page in AEM that uses yowsup. Yowsup is an api written in python for messaging to whatsapp.

I would like to write an application extension of the Yosup in Python, then call that in API.

 

Am I even trying something reasonable.

 

Regards

Clive Stewart

 

Level 3
January 4, 2016
kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
January 4, 2016

As fa as I know about Yowsup, Yowsup is a python library that enables you build application which use WhatsApp service.
yowsup-cli is a command line interface to Yowsup library.
We need to use yowsup-cli for sending or reciveing messages.

So you can create a OSGI Sling service from which you can call Python scripts.
Option 1:-
//Link:- http://blog.xebia.in/2013/11/25/yowsup-the-whatsapp-api/

To embed it in Java, all you have to do is call this command from Java's process builder framework. For this, first create a shell script to send message. Name it `sendMessage.sh` which looks like: 

                     #!/bin/bash
         python yowsup-cli -c config.example -s $1 "$2"

         
         String message = "The message goes here";
        String[] sendCommand = { "/bin/bash", “path/to/sendMessage.sh”, "<to number including cc>", message };
        ProcessBuilder processBuilder = new ProcessBuilder();processBuilder.directory(new File(<YOWSUP_HOME>));
        processBuilder.environment().put("PYTHONPATH", <YOWSUP_HOME>);
        processBuilder.command(sendCommand);
        System.out.println(processBuilder.environment());
        Process process = processBuilder.start();
        process.waitFor();

  8. Run the above program and your message will be sent to the intended receiver.
  9. You can read the ` process.getInputStream()` to get the output of the script you executed. For ex- 

                    InputStream stream = process.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        String str = null;
        while ((str = br.readLine()) != null) {
            System.out.println(str);
        }

Option 2:-
//Link:- http://stackoverflow.com/questions/16447410/how-to-execute-python-script-from-java
Execute Python script from Java?

Put your command to a shell script and execute that shell script with .exec() or
You can do something similar to the following

String[] cmd = {
        "/bin/bash",
        "-c",
        "echo password | python script.py '" + packet.toString() + "'"
    };
Runtime.getRuntime().exec(cmd);


I am not sure about if we can do something with REST APIs, if we have rest framework for Yowsup, then we can call the APIs from OSGI Sling Service.


I hope this would help you.

Thanks and Regards
Kautuk Sahni

Kautuk Sahni
Level 6
January 4, 2016

Apologies,  my question was not well formed. I am basically looking to call a python application from within Java

Level 6
January 4, 2016

Thank you,

 

This is exactly what I was looking for.

Level 6
January 4, 2016

Thank you for the links, you are correct, it is a basic Python - Java integration issue