Twilio | On-Campus | What's in a number ?
Anonymous User
1486

What's in the number ? In Addition to using typical "address" (i.e. phone number) for SMS messages, Twilio supports sending messages through different channels such as wechat, whatsapp and messenger. These other channels have their own address formats.

Twilio uses a standardized phone number format for SMS messages called E.164 phone numbers.

Some examples of valid E.164 numbers are:

+15555555555
+34864683619
+61491570156
+913858210056

For other channels messaging, they have addresses that are formatted differently to indicate the provider responsible to route the message to.
Some examples of these provider/identifier combinations are:

. whatsapp:34864683619
. messenger:+12025550001
. wechat:gh_alf37dacefe3

All these values are passed around as strings. It is important that we validate the format is correct for these addresses, as well as identify the appropriate provider to route the message
to.

We are asking you to build a function that validates that the address (which is either a standard phone number, or a provider/identifier combo) is formatted correctly, and then returns which channel to route the message to.

Given:
. address: A string containing the telephone number or provider:identifier channel string

Write
. A function that validates the address and to route the message to

. The return value will be one of
o For address strings with invalid formatting, return "INVALID_ADDRESS
. For valid E. 164 format addresses, return

"SMS"
For other channels, return the provider string as "WHATSAPP", "WECHAT", or
"MESSENGER"

Constraints
. The address will be a string between 1 - 258 characters long
. E.164 numbers

**May optionally start with '+'

  • Will have up to 15 digits in length (not
    including the '+")
  • Digits can be between 0-9. The first digit can
    never be 0.**

Other channel addresses

  • No whitespace allowed

  • The provider and identifier will always be separated by one colon {provider}: identifier}
    o Valid providers are: whatsapp, wechat, and
    messenger

  • Inputs could be lower or upper case, but outputs should always be uppercase

  • Valid identifiers Identifiers are between 1 and 248 characters long

  • whatsapp and messenger identifiers must specifically be E.164 numbers

Other providers can have identifiers that are made up of any English alphanumeric character and the characters '+', '-, ", '@',.
" Including any other characters would make the identifier invalid.

#include <bits/stdc++.h>
using namespace std;

bool isE164(string s) {
    int i = 0, count = 0;
    if(s[0]=='+')
        i=1;
    
    if(i<s.length() && s[i]=='0')
        return false;
        
    for(;i<s.length(); i++, count++){
        if(count == 15)
            return false;
        if(s[i]>'9' || s[i]<'0')
            return false;
    }
    
    return true;
}

bool isValidWeChat(string s) {
    for(char c : s) {
        if(isalnum(c) && c=='+' || c=='-' || c=='_' || c=='@' || c=='.')
            return true;
    }
    return false;
}

string check(string s) {
    int len = s.length();
    
    if (len<1 || len>256)
        return "INVALID_ADDRESS";
        
    if(isE164(s)) 
        return "SMS";
        
    size_t found = s.find(':');
    if (found == string::npos)
        return "INVALID_ADDRESS";
        
    string provider = s.substr(0, found);
    transform(provider.begin(), provider.end(), provider.begin(), ::toupper);
    if(provider== "WHATSAPP" || provider== "MESSENGER") {
        string identifier = s.substr(found+1);
        if(identifier.length() < 1 || identifier.length() > 256 || !isE164(identifier))
            return "INVALID_ADDRESS";
        return provider;
    }
    if(provider== "WECHAT") {
        string identifier = s.substr(found+1);
        if(identifier.length() < 1 || identifier.length() > 256 || isValidWeChat(identifier))
            return provider;
        return "INVALID_ADDRESS";
    }
    
    return "INVALID_ADDRESS";
}

int main()
{
    cout<<check("wechat:identifier:dasdad@d932232");

    return 0;
}

correct me if any error!

Comments (1)