yowsup icon indicating copy to clipboard operation
yowsup copied to clipboard

Yowsup for Home Automation

Open PierfrancescoElia opened this issue 11 years ago • 37 comments

Hi, we will create a private secretary with YowSup.

Version 1.2

You need three files. http://pastebin.com/KbjPc7Tx <- run.py http://pastebin.com/aXza2W2B <- layer.py http://pastebin.com/KNhv8Kea <- whatsapp.sh

You need also a WhatsApp account! :smile:

Premise: my scripts are in /home/pi/domo/

Ok now You just create a directory where you put in run.py and layer.py.

You must edit run.py with your credentials and you must edit layer.py for allowed senders and the commands (question - answer).

whatsapp.sh care to keep the app always active.

Make start whatsapp.sh on startup.

Ok, now You are active!


Changelog:

V1.2: Script more flexible

V1.0: First stable and functional version.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

PierfrancescoElia avatar Feb 19 '15 20:02 PierfrancescoElia

Hi, heres an example. I dont know how to poste code here

http://pastebin.com/XWicY72z If you created both files you can start this script with: python /path/to/file/run.py

Greetings, eXayte

ghost avatar Feb 21 '15 15:02 ghost

Hi, tanks for your help. Your scripts don't work :( When I run the run.py script I receive:

root@raspberrypi:/home/pi/domo# python run.py Traceback (most recent call last): File "run.py", line 14, in EchoLayer, NameError: name 'EchoLayer' is not defined

I think you haven't include the layer.py but I'm not shure..... How can I fix? Thanks:)

PierfrancescoElia avatar Feb 21 '15 16:02 PierfrancescoElia

Ok. I've corrected the scripts.

http://pastebin.com/02vEGkt7

Now it works fine. I would like to add more conditions (ex. Message= hi -> yowsup = hei!; message = how are you -> yowsup = Fine, thanks! ) and I would like to filter the number who send the message...

PierfrancescoElia avatar Feb 21 '15 17:02 PierfrancescoElia

It's pretty easy.

http://pastebin.com/5Yvi1rKN

ghost avatar Feb 21 '15 19:02 ghost

Hi, I've make my script with more response (ex. Message= hi -> yowsup = hei!; message = how are you -> yowsup = Fine, thanks! ).

Now, my problem is this: I would like to filter the message's sender. But senders allowed to interact with yowsup are more of one! I tryed this command:

if '391234567' or '3913243432' or '123123412242' in messageProtocolEntity.getFrom(): ... else: answer('You are not authorized.')

the script does not check whether the number actually is among those who have entered, the script allows everyone to interact with yowsup.

What should I do?

PS: sorry for my english but I'm italian :)

PierfrancescoElia avatar Feb 21 '15 21:02 PierfrancescoElia

Hi. allowedPersons = ['491234567892', '431234567890', '1123456789'] ap = set(allowedPersons) if messageProtocolEntity.getFrom(False) in ap: print 'Authorized'

ghost avatar Feb 21 '15 22:02 ghost

Hi, I came across an error but don't know how to fix it. I used the latest pastebin link from EliaComputers. But I am getting this:

No handlers could be found for logger "yowsup.stacks.yowstack" ^CTraceback (most recent call last): File "run.py", line 36, in stack.loop() File "/usr/local/lib/python2.7/dist-packages/yowsup2-2.2.78-py2.7.egg/yowsup/stacks/yowstack.py", line 170, in loop asyncore.loop(_args, *_kwargs) File "/usr/lib/python2.7/asyncore.py", line 216, in loop poll_fun(timeout, map) File "/usr/lib/python2.7/asyncore.py", line 145, in poll r, w, e = select.select(r, w, e, timeout) KeyboardInterrupt

I also tried to add from yowsup.layers.logger import YowLoggerLayer and layers = ( ... YowLoggerLayer )

But it threw me another error back: ImportError: No module named layer.logger

blackjack4494 avatar Feb 22 '15 19:02 blackjack4494

I'm also getting this "error". Dont know how to fix this, but the app is just working, also with this error.

ghost avatar Feb 22 '15 20:02 ghost

Hi, I'm also get the errors, but it works. Now I've got the last problem. :smile: This is an app that must run forever... For run the app at boot, I write the script whatsapp.sh:

#!/bin/bash sudo python /home/pi/domo/run.py

The script works but the app is stopped after a few minutes.... How can I fix??

Thanks and sorry for my english :laughing:

PierfrancescoElia avatar Mar 08 '15 12:03 PierfrancescoElia

@EliaComputers it will disconnect after some time, you can just put it into infinite loop in your bash script.

while true: do sudo python run.py done

fore more info checkout https://github.com/tgalal/yowsup/issues/648#

gautamMalu avatar Mar 08 '15 18:03 gautamMalu

Thank you, it works!

Ok, now I post the scripts:

run.py: http://pastebin.com/KbjPc7Tx layer.py: http://pastebin.com/kSXNEehK whatsapp.sh: http://pastebin.com/sJ8nEJJ0

I will edit the first post, to make a 'guide' :) thanks to all!

PierfrancescoElia avatar Mar 08 '15 18:03 PierfrancescoElia

Ok, now the scripts work fine, but I would like to control the 'online' and 'last access at xx.xx'.

Now while I message with the machine, it is online, but a few minutes later the last access isn't of a few second ago, but the last access is when i have launch the yowsup-cli and I've wrote /presence set available...

Sorry for my english, I'm italian :)

How can I fix?:)

PierfrancescoElia avatar Mar 12 '15 17:03 PierfrancescoElia

Wow, thank you very much for your great secretary project. I am trying to use it to do my home-automation as well. And might turn into business as well. But I found I ran into a little problem when I do run.py in Version 1.2(First post) There is a del risposta in layer.py Also, I am thinking using a variable to store messageProtocolEntity.getBody() might be a easier solution, say message_body = messageProtocolEntity.getBody(), as we can then turn that variable to lower case by message_body.lower() then we can save a few lines in the script.

ngalongc avatar Mar 17 '15 03:03 ngalongc

http://pastebin.com/48UP8UEd (layer.py) I have explore yowsup a little bit. After adding AvailablePresenceProtocolEntity, and UnavailablePresenceProtocolEntity and send to the lower layer, now antwort is pretending to be human, echo back message after 5 seconds of receival, and be online within 5 seconds, then echo back the message and send an unavailable presence to server.

One point to add is,

from yowsup.layers.protocol_presence import YowPresenceProtocolLayer

this line should be added to run.py in order to utilize presence function of whatsapp.

ngalongc avatar Mar 17 '15 09:03 ngalongc

Hi,

The Script above works great for me. But the Disconnect problem is still there. After a few minutes, the "yowsup" script receives no longer Messages from WhatsApp. Ive got the Bash "infinity" code, the run and layer, exactly same as yours @EliaComputers . Is there a way to solve my problem?

Kevke avatar Mar 17 '15 13:03 Kevke

Hello, I am trying to record the numbers of users in a file but I can not, can you help me?

f = open ("user.txt", 'w') f.write ('User Number in Here \ n') f.close ()

You know in which variable is the user number?

sr2ds avatar Mar 18 '15 01:03 sr2ds

@srdavidsilva try this:

f = open("user.txt", 'a') #append mode f.writelines('User Number in Here'+'\n') f.close ()

edugargon avatar Mar 30 '15 10:03 edugargon

same problem with @Kevke , using latest script provided on first post. initially worked fine (able to respond messages) but suddenly stopped working after few minutes, and the disconnect problem still exist. I tried to add these line to layer.py to output debug message

import logging
logging.basicConfig(level=logging.DEBUG)

and script outputs these line:

DEBUG:yowsup.stacks.yowstack:Initializing stack
DEBUG:yowsup.stacks.yowstack:Constructed Network Layer
DEBUG:yowsup.stacks.yowstack:Constructed Stanza Regulator Layer
DEBUG:yowsup.stacks.yowstack:Constructed Crypt Layer
DEBUG:yowsup.stacks.yowstack:Constructed Coder Layer
WARNING:yowsup.stacks.yowstack:Implicit declaration of parallel layers in a tuple is deprecated, pass a YowParallelLayer instead
DEBUG:yowsup.stacks.yowstack:Constructed Authentication Layer - Messages Layer - Receipt Layer - Ack Layer
DEBUG:yowsup.stacks.yowstack:Constructed Interface Layer
***script just paused here for 1 minute or so***
***then it disconnected***
DEBUG:yowsup.layers.network.layer:Disconnected, reason: Connection Closed
DEBUG:yowsup.layers.network.layer:Disconnected, reason: Connection Closed

Idk why, sometimes the script is just working fine, but a moment later its not working :confused:

gojigeje avatar Apr 07 '15 04:04 gojigeje

Hi, I'm very very happy forma your attenction for my scripts :) OK, at the moment I'have also your problemi: the scripts stop working after a few minutes... I create a script wich upload the sender, a number (if sender is my number n=1 else n=0), the text, and the time to a MySQL table. I will upload this script tomorrow :).

Now I'm going to try @ngalongc 's script and I will edit the first post to the correct scripts.. @ngalongc 's scripts are useful but I can't wait 5 or 10 sec forma a response, because I would like to get an instantly reply..

OK now I do some experiments and I will update this post, stay tuned ;)

PierfrancescoElia avatar Apr 10 '15 22:04 PierfrancescoElia

HI, i use your example and change for my system.

  1. I change to any image received i send to email and save in folder.

    def onMediaMessage(self, messageProtocolEntity): <------><------> <--><------>if messageProtocolEntity.getMediaType() == "image": <--><------> receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom()) outImage = ImageDownloadableMediaMessageProtocolEntity( <--><------><------>messageProtocolEntity.getMimeType(), messageProtocolEntity.fileHash, messageProtocolEntity.url, messageProtocolEntity.ip, <--><------><------>messageProtocolEntity.size, messageProtocolEntity.fileName, messageProtocolEntity.encoding, messageProtocolEntity.width,. <--><------><------>messageProtocolEntity.height, <--><------><------>messageProtocolEntity.getCaption(), <--><------><------>to = messageProtocolEntity.getFrom(), preview = messageProtocolEntity.getPreview()) <------><------> variavel="%s" % (messageProtocolEntity.url) <------><------> varrand=randint(0,13) <------><------> v1 = variavel.split("/",4) <------><------> v2 = v1[4].split(".") <------><------> now = datetime.now() <------><------> arquivo="%s"%(messageProtocolEntity.getFrom(False))+"-"+str(now.year)+str(now.month)+str(now.day)+str(now.hour)+str(now.minute)+ <------><------> str(now.second)+str(varrand)+"."+v2[1] <------><------> print arquivo <------><------> urllib.urlretrieve ("%s" % (messageProtocolEntity.url), "/home/"+arquivo)

<------><------>.... <------><------> msg = MIMEMultipart() <------><------> msg['Subject'] = 'Whatsapp - Sent by %s'% messageProtocolEntity.getFrom(False) <------><------> msg['From'] = '[email protected]' <------><------> msg['To'] = '[email protected]' <------><------>..... <------><------> # That is what u see if dont have an email reader: <------><------> msg.preamble = 'Multipart massage.\n' <------><------>...... <------><------> # This is the textual part: <------><------> part = MIMEText("Foto enviada pelo numero %s" % messageProtocolEntity.getFrom(False) ) <------><------> msg.attach(part) <------><------>....... <------><------> # This is the binary part(The Attachment): <------><------> part = MIMEApplication(open("/home/"+arquivo,"rb").read()) <------><------> part.add_header('Content-Disposition', 'attachment', filename=arquivo) <------><------> msg.attach(part) <------><------>........ <------><------> # Create an instance in SMTP server <------><------> smtp = SMTP("xxxxxx") <------><------> # Start the server: <------><------> # Send the email

work fine!!!

In other example i use postgres to permit number send message to system but not work. How i use array in python??

con = psycopg2.connect("dbname='whatsapp' user='xxxx' password='xxxx'") cur = con.cursor() cur.execute("SELECT celular FROM celular_liberados") row = cur.fetchall() allowedPersons=(row) ap = set(allowedPersons)

rafaelpstein avatar Apr 22 '15 20:04 rafaelpstein