How to read out WhatsApp messages with Tasker and react on their content in real time

http://technologyworkroom.blogspot.sg/2013/05/tasker-how-to-read-out-whatsapp.html

Tasker can read out incoming or stored WhatsApp Messages, I used Tasker to do the following things on WhatsApp:

  • Display the incoming messages in iOS style, no need to pull down the notification menu to read the messages. The whole message is displayed in the menu bar. See screenshot for details
  • Play different notification sounds for different contacts or different message content, this way you can for example get a special notification if your name is mentioned.
  • Mute WhatsApp at specific times or at specific locations, without muting other notifications.
  • Update: Read aloud WhatsApp messages

What we need:

  • Tasker
  • WhatsApp
  • root. I want you to root your phone now! You need root for SQLite, we need it to read out the database of WhatsApp.
  • SQLite for Android
  • Kyo-Tux Aeon HD icon pack, provides useful icons for Tasker, its free and lightweight, so grab it

Here is the complete profile:

Profile: WhatsApp Statusbar
  Priority: 9 CoolDown: 0
  Event: Notification [ Owner Application:WhatsApp Title:* ]

Enter: WhatsApp Notification
  Run Both Together
  A1: Run Shell [ Command:sqlite3 /data/data/com.whatsapp/databases/msgstore.db "SELECT timestamp,data,key_remote_jid FROM messages WHERE key_from_me='0' ORDER BY timestamp DESC LIMIT 1;" Timeout (Seconds):15 Use Root:On Store Result In:%sql_result Continue Task After Error:On ]
  A2: If [ %sql_result !~ %SQL_RESULT_OLD ]
    A3: Variable Set [ Name:%SQL_RESULT_OLD To:%sql_result Do Maths:Off Append:Off ]
    A4: Variable Split [ Name:%sql_result Splitter:| Delete Base:On ]
    A5: Variable Set [ Name:%sql_result1 To:%sql_result1/1000 Do Maths:On Append:Off ]
    A6: Variable Convert [ Name:%sql_result1 Function:Seconds to Date Time Store Result In: ]
    A7: Variable Split [ Name:%sql_result1 Splitter: Delete Base:Off ]
    A8: Variable Search Replace [ Variable:%sql_result12 Search:\. Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In: Replace Matches:On Replace With:: ]
    A9: Perform Task [ Name:Number->Name WhatsApp Stop:Off Priority:6 Parameter 1 (%par1):%sql_result3 Parameter 2 (%par2): Return Value Variable:%sql_result3 ]
    A10: Wait Until [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ] If [ %sql_result3 !~R @s.whatsapp.net ]
    A11: Array Push [ Name:%WHATSAPP_NOTIFY Position:1 Value:%sql_result3: %sql_result2 Fill Spaces:Off ]
    A12: Notify Cancel [ Title:%WHATSAPP_NOTIFY4 Warn Not Exist:Off ]
    A13: Variable Clear [ Name:%WHATSAPP_NOTIFY4 Pattern Matching:Off ]
    A14: Notify [ Title:%sql_result3: %sql_result2 Text:%sql_result2 Icon:ipack:kyotuxaeonhd:buddy_green Number:0 Permanent:Off Priority:3 ]
    A15: Perform Task [ Name:Play Sound Stop:Off Priority:6 Parameter 1 (%par1): Parameter 2 (%par2): Return Value Variable: ]
  A16: End If

The most important line is A1. Here we use the installed sqlite to save the time, the sender and the content of the last message stored in the WhatsApp database into a Tasker variable. The values are separated by a "|", the time is in ms and the sender is stored as a phone numer. So there are some conversions needed afterwards.

You may have noticed that there is a call to another task at line A9. WhatsApp saves only the number of the contact who send you the message in its database, so we need to convert the number to a real contact name as saved in your phone book. The task "Number->Name WhatsApp" does it. It searches the address book for contacts associated with a WhatsApp account and returns their name. So after line A10 you should know who texted you by name, you can put in if clauses to react different to different names. Here the description of the "Number->Name WhatsApp" task:

Number->Name WhatsApp
  A1: Run Shell [ Command:sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db "SELECT display_name FROM raw_contacts WHERE sync1 like '%par1';" Timeout (Seconds):5 Use Root:On Store Result In:%sql_result_name Continue Task After Error:On ]
  A2: If [ %sql_result_name !~R sql_result_name ]
    A3: Return [ Value:%sql_result_name Stop:On ]
  A4: Else
    A5: Return [ Value:%par1 Stop:On ]
  A6: End If

At line A15 of the task "WhatsApp Notification" task I call a Tasker task called "Play Sound". That's because I disabled notification sounds in WhatsApp to handle them myself (They are time and WhatsApp message content depandant). Here an example of how a simple "Play Sound" task could look like:

Play Sound
  Run Both Together
  A1: If [ %par1 ~R .*[Rr]+[Uu]+[Aa]+[Rr]+.* ]
    A2: Variable Set [ Name:%tmp To:%VOLS Do Maths:Off Append:Off ]
    A3: Wait [ MS:50 Seconds:0 Minutes:0 Hours:0 Days:0 ]
    A4: System Volume [ Level:7 Display:Off Sound:Off ]
    A5: If [ %SILENT ~ off ]
      A6: Play Ringtone [ Type:Notification Sound:Dinosaur Roar Stream:1 ]
    A7: Else If [ %HEADSET ~ 1 ]
      A8: Play Ringtone [ Type:Notification Sound:Dinosaur Roar Stream:1 ]
    A9: End If
    A10: Vibrate [ Time:1000 ]
    A11: Vibrate [ Time:1000 ]
    A12: Vibrate [ Time:1000 ]
    A13: Vibrate [ Time:1000 ]
    A14: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ]
    A15: System Volume [ Level:%tmp Display:Off Sound:Off ]
  A16: Else If [ %par1 ~R [Mm][Ii][Aa][Uu]+\b ]
    A17: If [ %SILENT ~ off ]
      A18: Play Ringtone [ Type:Notification Sound:Miau Stream:1 ]
    A19: Else If [ %HEADSET ~ 1 ]
      A20: Play Ringtone [ Type:Notification Sound:Miau Stream:1 ]
    A21: End If
  A22: Else
    A23: If [ %par2 !~ 1 ]
      A24: If [ %HEADSET !~ 1 ]
        A25: If [ %SILENT ~ off ]
          A26: Vibrate [ Time:100 ]
          A27: Play Ringtone [ Type:Notification Sound:Good news Stream:5 ]
        A28: End If
      A29: End If
    A30: End If
  A31: End If

If headphones are connected or phone is on silent mode no sound is played at all. If the message contains something like "Rrruaaarrrr" a dinosaur roar is played at full volume (even with headphones connected) and the phone vibrates for 4 seconds. If a "Miauuu" is in the message a cats miau is played... I'm sure you can do better :)

You can even make Tasker react on a click on one of the notifications, just add the following profile:

Profile: WhatsApp Notification1 Click
 Event: Notification Click [ Owner Application:Tasker Title:%WHATSAPP_NOTIFY1 ]
Enter: Open WhatsApp
 A1: Load App [ App:WhatsApp Data: Exclude From Recent Apps:Off ]

Duplicate this profile two times so you can click on any of our three custom notifications.

Profile: WhatsApp Notification2 Click
 Event: Notification Click [ Owner Application:Tasker Title:%WHATSAPP_NOTIFY2 ]
Enter: Open WhatsApp
 A1: Load App [ App:WhatsApp Data: Exclude From Recent Apps:Off ]

Profile: WhatsApp Notification3 Click
 Event: Notification Click [ Owner Application:Tasker Title:%WHATSAPP_NOTIFY3 ]
Enter: Open WhatsApp
 A1: Load App [ App:WhatsApp Data: Exclude From Recent Apps:Off ]

I added another profile which deletes all our custom made WhatsApp notifications automatically after opening WhatsApp. Looks like this:

Profile: WhatsApp opened
 Application: WhatsApp

Enter: WhatsApp Notification clear
 A1: Notify Cancel [ Title:%WHATSAPP_NOTIFY1 Warn Not Exist:Off ]
 A2: Notify Cancel [ Title:%WHATSAPP_NOTIFY2 Warn Not Exist:Off ]
 A3: Notify Cancel [ Title:%WHATSAPP_NOTIFY3 Warn Not Exist:Off ]
 A4: Variable Clear [ Name:%WHATSAPP_NOTIFY* Pattern Matching:On ]
 A5: Stop [ With Error:Off Task:WhatsApp Notification ]

Have fun and use this knowledge wisely. Please leave your thoughts in the comments, I'm very excited about your implementations. Hope you read this Ruy Aguilar ;)

cv