Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM OSGI and PYTHON(yowsup)

Avatar

Level 6

 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

1 Accepted Solution

Avatar

Correct answer by
Administrator

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

View solution in original post

6 Replies

Avatar

Level 9

   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

 

Avatar

Correct answer by
Administrator

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

Avatar

Level 6

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

Avatar

Level 6

Thank you,

 

This is exactly what I was looking for.

Avatar

Level 6

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