Blazor Helpers App Twilio: Mobile Number Check
twilio blazor csharp blazor-server sms twilio mobilenumber
Explicit verification of a mobile number before it is used to send SMS.
Background
A Blazor Helpers app as discussed on this blog site (but not published) is used by a sporting club for members (and contacts) to volunteer to assist the club by performing specific tasks for a specific timeslot on a day of competition. Helpers are registered with a name and mobile number. A day prior to competition all volunteers for a day of competition are sent a text message in SMS using Twilio listing the tasks with timeslots asking them to confirm with a Yes or No that they are available; like with doctors’ and other appointments. The responses are sent to club secretary. The app is also used to email club members when needed using the club database (Azure SQL Server). The database has detailed information as to which groups a member is part of such as age-based, gender-based,athletes, coaches, officials and committee members, and so emails can be sent all or to to specific groups. But in this day and age, many are oblivious to email messages or they go through to their spam box. So at times there is an urgent need to send messages as SMSs to club members. It was found that the sporting association, with whom members register and from whom the club gets its database data, does not fully validate members’ entry in the mobile field and so significant checks and massaging is needed before that field is used with Twilio to send SMSs to members.
Links
- Send SMS with Twilio Programmable Messaging: Twilio
- Twilio blog posts here
- Helpers App blog posts here
- Blazor blog posts here
- Introduction to ASP.NET Core Blazor (NET6): Microsoft Docs
Mobile Numbers
The format used for a mobile number in the app is a 10 digit number starting with 04. The stored data in the database is a string and whilst it doesn’t contain letters may contain spaces, dashes not have a leading 0, not be 10 digits or even be a land line number! In some cases it is a blank string. One additional fix is if it leads with +614<8 digits> which would be valid as that app is used in Australia. This is converted into 04<8 digits>.
Members Table
This is where the raw data is imported to from the sporting association’s data. The relevant field is Mobile from which two read only properties are generated MobileNo and IsMobileNo:
public class Member
{
...
...
[Column("Mobile")]
[JsonPropertyName("Mobile")]
public string Mobile { get; set; }
[NotMapped]
[System.Text.Json.Serialization.JsonIgnore]
public string MobileNo
{
get { return convertPhoneNo(Mobile); }
}
[NotMapped]
[System.Text.Json.Serialization.JsonIgnore]
public bool IsMobileNo
{
get { return isMobileNumber(Mobile); }
}
}
MobileNo massages Mobile into a proper phone number. A land line would pass through this OK. These two read-only properties call two private methods. Whilst the mumbo-jumbo of Regex could be used, I prefer explicit/formal checking. IsMobileNo checks if MobileNo leads with 04 and contains 10 digits in total: It actually just checks the string length then sees if it converts to a number.
The Fix Up
private string convertPhoneNo(string numStr)
{
if (!string.IsNullOrEmpty(numStr))
{
// Could just remove any non digit here (allow for +61 though)
numStr = numStr.Replace(" ", "");
numStr = numStr.Replace("-", "");
numStr = numStr.Replace("_", "");
if (!string.IsNullOrEmpty(numStr))
{
if (numStr.Length < 4)
return "";
//Mobile numbers
if (numStr.Substring(0,3) == "+614")
numStr = "0" + numStr.Substring(3);
else if (numStr[0] == '4')
numStr = "0" + numStr;
else if (numStr[0] == '3')
//Victoria Country
numStr = "0" + numStr;
if (numStr.Length <8)
return "";
return numStr;
}
}
return "";
}
The Checks
private bool isMobileNumber(string mobileStr)
{
string num = convertPhoneNo(mobileStr);
if (num.Length != 10)
return false;
if (num.Substring(0,2) != "04")
return false;
if (ulong.TryParse(num, out ulong lnum))
{
return true;
}
else
return false;
}
Conclusion
When first run with a message about finals to members, without these checks and balances, the Twilio messaging to 200 members failed after 40 members. When the cause was analysed and fixed as above, it ran without failure and members did receive the important message.
Topic | Subtopic | |
Next: > | Google Home Windows Bridge | TRIGGERcmd |
< Prev: | Meltdown | A simple Markup language. |
This Category Links | ||
Category: | Twilio SMS Index: | Twilio SMS |
< Prev: | Blazor Helpers App Twilio | Onforwarding SMS |