0

Background information:

I am writing a Python application which will receive WhatsApp messages and perform some actions based on the provided commands. For that, I have installed yowsup, which is also a Python application which deals with the WhatsApp messaging.

Strategy:

Yowsup has a nice interface to send and receive messages, so my idea was to start the process via Popen and deal with the stdin, stdout and stderr. For that I wrote the code below:

from subprocess import Popen, PIPE
statement = "sudo /home/pi/yowsup/yowsup-cli demos --yowsup --config /home/pi/yowsup/config"
yowsup = Popen(statement.split(" "), stdout=PIPE, stdin=PIPE, stderr=PIPE)

while True:

    output = yowsup.stdout.readline()
    if output != "":
        print output.rstrip()

Issue:

The code did not return any message, so I changed the statement to check if my code was fine:

statement = "ping 192.168.0.9 -c 4"

Bingo! With the command ping the code works just great.

My question:

  • Why am I not able to handle the stdin and stdout by using the yowsup application and how I can make it work?

  • Does it play any role that yowsup is also a Python application?

  • Try using pexpect, I don't think subprocess can do this kind of thing. – matsjoyce Apr 02 '15 at 08:42
  • Why can't you use Python API (import module, call functions) instead of controlling a cli application? why do you set `stdin=PIPE` if you don't write anything into the pipe. Why do you set `stderr=PIPE` if you don't read it? Provide an example of successful interaction with `yowsup-cli` (manually). [Read the discussion (including the links in the comments)](http://stackoverflow.com/q/28616018/4279) – jfs Apr 03 '15 at 18:51

1 Answers1

0

"sudo /home/pi/yowsup/yowsup-cli demos --yowsup --config /home/pi/yowsup/config", here --yowsup starts an interactive client waiting for input. You can't get output until it finishes running.
Also make sure yowsup-cli is executable or start as python yowsup-cli.
yowsup-cli has no option to receive messages. You have to import yowsup and use the library to recieve messages.

Nizam Mohamed
  • 8,751
  • 24
  • 32