Read via scripting modified WhatsApp message aloud

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Read via scripting modified WhatsApp message aloud

Post by Rhonin » 19 Apr 2015 18:22

Hi folx,

meanwhile i'm a little desperated :(

My little project should read an incoming WhatsApp message via car's BT
Is it a single message reading works without a problem.
If there are more than 1 unread message the used {notification_text_big} cause the TTS Modul to read all unread messages - including pictures and videos.

So i tried to split the {notification_text_big} with the following Scrtipaction:

Code: Select all

Test = ({notification_text_big });
zeilen = split(Test, "\\n");
wa = newList();
for (Zeile in zeilen)
{
result = matches(Zeile, "\\w+");
if (result = true)
{
wa = addElement(wa, Zeile);
}
But the resulting list called wa appears as follows:
[WhatsApp, , CM12: Test1, Adc rhonin: Test 2, , , , , , …, 2 Nachrichten in 2 Chats.]
i tried different pattern matches to prevent such "empty" fields like in between WhatsApp and CM12 and at the end between test2 and .... but all my efforts were not successful wheter whitespaces \\s nor " " or testing if there are "words" "\\w+"

Someone has an idea where is the bug in my thinking ? Maybe someone has a solution ?

If i can delete the "empty spaces" ( which seem to be NOT EMPTY ) i would reverse the list then delete the field with 2 Nachrichten and also the field containing ... and read aloud the resulting first field which is the latest incoming message.

THX in advance

Rhonin
Automagic on rooted Xperia Z3c running on Android 5.1.1

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Read via scripting modified WhatsApp message aloud

Post by Martin » 20 Apr 2015 18:56

Hi,

The problem is probably caused by this line:
if (result = true)

This assigns true to variable result and also evaluates to true so the if will never be false.
You can either change it to
if (result == true)
or
if (result)

I think that the pattern \\w+ will not match lines containing spaces. I would probably use trim to remove all leading and trailing whitespace (just in case a line consists of a number of spaces) and see if the line is not empty:
if(trim(zeile)!="")

Regards,
Martin

User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Re: Read via scripting modified WhatsApp message aloud

Post by Rhonin » 22 Apr 2015 03:50

First of all - THX Martin for all your efforts,

meanwhile i tried around a little more and succeeded in generating a working script :D It is not elegant but there is enough room to improve it later...

This is my code:

Code: Select all

zeilen = split({notification_text_big }, "\\n");
wa = newList();
for (Zeile in zeilen)
{
wa = addElement(wa, Zeile);
}
muster = getElement(wa, 1);
wa_kurz = removeElementValue(wa, muster);
index = length(wa) - 1;
muster = getElement(wa,  index);
wa_kurz = removeElementValue(wa, muster);
if (length(wa_kurz) > 3)
{
index = length(wa) - 1;
muster = getElement(wa,  index);
wa_kurz = removeElementValue(wa, muster);
}
wa = reverse(wa);
if (length(wa)< 3)
{
text = getElement(wa,  0);
}
else
{
tex = getElement(wa,0);
tee = split(tex, ":");
if (length(tee)<2)
{
text = getElement(wa,0);
}
if (length(tee)>1)
{
text = getElement(tee,1);
}
}
Here is a little description:
zeilen = split({notification_text_big }, "\\n");
wa = newList();
for (Zeile in zeilen)
{
wa = addElement(wa, Zeile);
}
First the {notification_text_big} string is devided by lines and written in a list. It looks like this :

[WhatsApp, , CM12: Test1, Adc rhonin: Test 2, , , , , , …, 2 Nachrichten in 2 Chats.]
muster = getElement(wa, 1);
wa_kurz = removeElementValue(wa, muster);
From my tests i knew that the second element of the list with listindex 1 is always an empty field. Getting this pattern i delete all empty fields in the list.
index = length(wa) - 1;
muster = getElement(wa, index);
wa_kurz = removeElementValue(wa, muster);
From the tests i also know that the last list's entry is alway something with "Nachrichten" - messages which can also be deleted
if (length(wa_kurz) > 3)
{
index = length(wa) - 1;
muster = getElement(wa, index);
wa_kurz = removeElementValue(wa, muster);
}
If there are more than one message (the length of the resulting list is bigger than 3 elements) i have also to delete the last list's entry "...."
wa = reverse(wa);
if (length(wa)< 3)
{
text = getElement(wa, 0);
}
else
{
tex = getElement(wa,0);
tee = split(tex, ":");
if (length(tee)<2)
{
text = getElement(wa,0);
}
if (length(tee)>1)
{
text = getElement(tee,1);
}
}
In a last step i reverse the list to have the newest message on the first position of the list. If there is only one message the read text is identical with the first entry. If there are more than one message from ONE sender i could also read the first entry aloud. If there are more than one sender the entry looks like:

Sender: This is my message

So it must be devided again and this time the second entry in the new created list is read aloud.

The flow in which the script is used is triggered by the notification in the statusbar. Then first the ticker text is read aloud:

Message from Sender

After that TEXT from the script is read aloud

This is my message

To prevent double actions while 2 or more messages come in at nearly the same time i have to set the option

Wait until the actual running instance has been ended (or similar - i have the german version ;) )

Maybe i upload the flow later in the sharing area if someone is interested...

THX again

Rhonin
Automagic on rooted Xperia Z3c running on Android 5.1.1

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Read via scripting modified WhatsApp message aloud

Post by MURTUMA » 22 Apr 2015 06:19

I'm interested. I have some hard time working with lists and I prefer learning through trial and error rather than asking ready solutions for every noob question I have. :/ Your script seems to have some elements I could try to use.

Would be bonus, if you could translate it to English, but that's up to you.

User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Re: Read via scripting modified WhatsApp message aloud

Post by Rhonin » 22 Apr 2015 10:14

MURTUMA wrote:.... I prefer learning through trial and error rather than asking ready solutions for every noob question I have.....

Would be bonus, if you could translate it to English, but that's up to you.
Hi Murtuma,

that is exactly the way i reached my goal - trial and error - verified by logging nearly every variable befor and after applying a list function. If i find the time i will give Martin's tipp a chance to see if the script could be shortened.

Which parts need to be translated in your eyes ? Give me the part(s) and i will translate them. It shuold be just a litte report on how i get the different steps working.

Cheers

Rhonin
Automagic on rooted Xperia Z3c running on Android 5.1.1

User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Re: Read via scripting modified WhatsApp message aloud

Post by Rhonin » 16 Aug 2015 16:22

After several Updates - Firmware to 5.1.1 and Whatsapp to the newest Version my version isn't working any more :(

It seems as if under 5.1.1 the notification_text_big is empty and the notification text isn't updated to the newest one...

Has anyone a tip how to get the affordable information ? Maybe initializing the above mentioned variables ? If so how to succeed in it ?

THX in advance for your tips
Automagic on rooted Xperia Z3c running on Android 5.1.1

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Read via scripting modified WhatsApp message aloud

Post by Martin » 17 Aug 2015 13:36

Hi,

Maybe whatsapp changed the way the information is shown in the notification. When you receive multiple texts from whatsapp, how does the notification look like? Can you still see the text of all the messages in one notification?

I'm not sure if we already discussed this but how does whatsapp behave when you read out the first received message and then remove the notification with action Remove Notification on Statusbar/Benachrichtigung aus Statusbar entfernen? Will the notification for the next message contain the text of both messages again or just the last one?

You can also use condition Notification on Statusbar Displayed/Benachrichtigung in Statusbar angezeigt to query the notifications and fill the variables but this should not make a difference since the variables filled by the condition and the trigger should be the same.

Regards,
Martin

User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Re: Read via scripting modified WhatsApp message aloud

Post by Rhonin » 17 Aug 2015 21:36

Hi Martin,

THX for your answer - i tried different adjustments but it appears as if the latest message couldn't be found in the provided variables. (notification_text_big) for example.

The Headup-notification shows all messages summarized to 5 messages in 3 Chats for example - in the big text there are only 4 Messages in 3 Chats. It seems as if the internal variables are not updated or updated too late....

At the moment i have no idea how to get the actual message :twisted: Remove Notification has also no effect

Could it be that under 5.1.1 the internal variables (especially - all around the notifications) are assigned in a different manner so that they keep their old value... ???

THX for your help ...

Rhonin
Automagic on rooted Xperia Z3c running on Android 5.1.1

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Read via scripting modified WhatsApp message aloud

Post by Martin » 19 Aug 2015 20:10

Hi,

Automagic should be notified by Android about all posted/updated notifications from other apps. Maybe whatsapp updates the notification twice when a new message is posted and the Automagic flow only handles the first update for some reason.
Could you share the flow and the log when the flow is executed with the old variables (send to info@automagic4android.com)?

When you execute a condition Notification on Statusbar DIsplayed and package filter set to whatsapp, are the variables updated properly?

Regards,
Martin

User avatar
Rhonin
Posts: 19
Joined: 08 Apr 2015 05:23

Re: Read via scripting modified WhatsApp message aloud

Post by Rhonin » 22 Aug 2015 22:50

Hi @ all - a special hello @ martin :D ,

after testing around and analyzing during my nightshifts i discovered that the trigger notification on statusbar was activated twice in different ways.

So i let my little flow only run if notification_ticker_text is not null. With this little condition i succeeded in getting it work again :mrgreen:

Now i have to optimize it again...

Have a nice Rest WE

Rhonin
Automagic on rooted Xperia Z3c running on Android 5.1.1

Post Reply