Did you know ... Search Documentation:
Pack sms_otp_parse -- prolog/sms_otp_parse.pl
PublicShow source

Verification texts come in many shapes: "G-482913 is your Google verification code", "[Bank] Code: 482 913. Expires in 10 minutes", "<#> 4829 is your code\nFA+9qCX9VSu". This library finds the code, the sending service, the stated expiry and the Android SMS Retriever hash, while skipping phone numbers, prices, dates, times and percentages that often sit in the same message.

A candidate is a run of 4 to 8 digits, or two groups of three digits joined by a single space or hyphen ("482 913", "482-913"). Runs that touch other digit groups (phone numbers, dates), start with "+", or sit next to a currency sign, a percent sign, a colon (times) or a decimal point (amounts) are dropped. Remaining candidates are scored: a code keyword in the 60 characters before it (code, OTP, passcode, PIN, verification, ...) adds 3, or 2 if the keyword only follows it; six digits adds 1, a 4-digit value that looks like a year (1900-2099) loses 2. The best score wins; ties go to the earliest candidate.

 otp_code(+Text, -Code:atom) is semidet
Code is the most likely one-time passcode in Text, as an atom of digits with any space or hyphen removed.
 otp_candidates(+Text, -Candidates:list) is det
Candidates is a list of otp(Code, Score, Offset) terms in the order they appear in Text.
 otp_service(+Text, -Service:atom) is semidet
Service is the sender named in the message: a leading "[Name]", "<#> Name:" or "Name:" prefix, or the word after "your" in "your Name verification code".
 otp_expiry_minutes(+Text, -Minutes:integer) is semidet
Minutes is the validity stated in Text ("expires in 10 minutes", "valid for 1 hour", "valid for 90 seconds" gives 2, rounded up).
 sms_retriever_hash(+Text, -Hash:atom) is semidet
Hash is the 11-character app hash that Android's SMS Retriever API expects as the last token of the message.
 otp_message(+Text, -Info:dict) is det
Info is otp{code:C, service:S, expiry_minutes:M, retriever_hash:H}; a field is the atom none when it cannot be found.