Tuesday, August 18, 2015

WhatsApp tailored notifications

I always mute WhatsApp group chat notifications (don’t we all?). But I still wanted to get a notification when someone mentions my name.
Did a little bit of googling and found out Yowsup, a Python API for WhatsApp. You can install it with pip install yowsup2
Then register it with a new number. Keep in mind that you can’t use your existing WhatsApp number. I used Ammamma’s (aka grandma)number (pretty sure she won’t come to WhatsApp)
Once it’s done I modified the sample app given in Yowsup’s github to track some keywords.
I also used pushbullet as mentioned in one of my previous posts to issue push notifications.
from yowsup.layers.interface                           import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities  import TextMessageProtocolEntity
from yowsup.layers.protocol_receipts.protocolentities  import OutgoingReceiptProtocolEntity
from yowsup.layers.protocol_acks.protocolentities      import OutgoingAckProtocolEntity
from Push import Push

pb = Push('KEY')

KEYWORDS = ['list', 'of', 'keywords']

class EchoLayer(YowInterfaceLayer):

    @ProtocolEntityCallback("message")
    def onMessage(self, messageProtocolEntity):
        #send receipt otherwise we keep receiving the same message over and over

        if True:
            receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), 'read', messageProtocolEntity.getParticipant())

            body, frm = messageProtocolEntity.getBody(), messageProtocolEntity.getFrom()
            if any(kw in body.lower() for kw in KEYWORDS):
                pb.notify(body, 'WhatsApp')

    @ProtocolEntityCallback("receipt")
    def onReceipt(self, entity):
        ack = OutgoingAckProtocolEntity(entity.getId(), "receipt", "delivery", entity.getFrom())
        self.toLower(ack)
Hosted the scripts on the Raspberry pi and now it sends me notifications whenever someone mentions my name :)
whatsappbot2.png
p.s. Okay that’s a bad example and it’s not what it means.

No comments:

Post a Comment