Page 1 of 1
OTP reading flow
Posted: 07 Dec 2018 11:42
by icefox56
How to create a flow that will
Trigger: when I receive an SMS containg the word 'OTP'
Action: Copy only the number part of that SMS to clipboard, so that I can paste it.
Note: I tried search first and found this
viewtopic.php?f=5&t=2944
But i didn't understand anything because they were taking about variables and scripts, which I have no idea about.
What I tried:
1. Trigger: SMS Received, containg the word OTP
2. Action1: Copy Text From Clipboard: to Variable clip_data1 (This is to backup my orginal clipboard)
3. Action2: Copy Text to Clipboard:{sms_text}
4. Stuck
How can I fill the clipboard with the numbers contained in SMS?
Re: OTP reading flow
Posted: 07 Dec 2018 14:47
by anuraag
First you need to find out otp using regex. If your otp contains only numbers then use script
otp=findAll(sms_text, '\\d+');
otp[0] is your otp.
1. Trigger: SMS Received, containg the word OTP
2. Action: Script: otp=findAll(sms_text, '\\d+');
3. Action2: Copy Text to Clipboard:{otp[0]}
Re: OTP reading flow
Posted: 07 Dec 2018 18:42
by icefox56
It worked. Thanks a lot.
Questions:
1. Will you tell me the meaning of
otp=findAll(sms_text, '\\d+');
And :{otp[0]}
I looked for this '\\d+' all over the script documentation but couldn't find an explanation. I understand it's some kind of regex or something. Where can I learn more about stuff like these?
2. Will I be able to make flows like this if Google decide to remove SMS permission for apps like Automagic?
3. Is there any way to specify more than one 'conains the word' in SMS received trigger?
Currently I have only 'OTP'. what if I want to include 'code' also. The help section says no wildcards supported. Does that mean I have to create a separate flow for all possible 'words contain' part?
Re: OTP reading flow
Posted: 07 Dec 2018 18:57
by digitalstone
At the very bottom inside the Script action, you'll find the "REGULAR EXPRESSION TESTER" button. Click it.
Here you can test your regex (REGular EXpression) strings on all supported script functions.
In the upper-right corner there's the 3-dot menu with the Help-page inside. Click.
Now you can learn about regex
Btw: '\\d+' is not 1 thing, so it makes sense you can't find it.
d = detecting digits
+ = 1 or more
Re: OTP reading flow
Posted: 07 Dec 2018 22:28
by icefox56
Thanks for pointing out the help section in regex tester. I didn't noticed that.
After reading help I tried to tweak the script. But I have problem making the flow work.
Some of the otp I receive include "-" eg. It look like this 123-456. So after reading help section in Regular expression tester I made this Regex
[[[a-z]&&[^\S]][\d][-]]{4,7}
The regex tester now correctly recognises 123-456 or 123456. And will not include words or dot(.) Or comma(,) but when I put this in script as
otp=findAll(sms_text,'[[[a-z]&&[^\S]][\d][-]]{4,7}')
And Action2 as Copy text to clipboard: {otp[0]}
It copy {error} into my clipboard.
What am I doing wrong?
How to correct it?
Re: OTP reading flow
Posted: 07 Dec 2018 23:12
by anuraag
Inside Regular Expression Tester you need only one back slash but in script you need 2 backslash.
So your regex is
[[[a-z]&&[^\\S]][\\d][-]]{4,7}
Re: OTP reading flow
Posted: 07 Dec 2018 23:26
by icefox56
Thanks Anuraag that fixed it.
Now can you please tell me what {otp[0]} is in Action2 copy to clipboard?
Re: OTP reading flow
Posted: 07 Dec 2018 23:47
by anuraag
findAll returns a list containing matching pattern. So otp is a list.
To get first element of your list you need to either use
getElement(otp, 0) or otp[0]
Re: OTP reading flow
Posted: 08 Dec 2018 01:54
by icefox56
Thanks for the explanation.