Blazor Helpers App Twilio: Sending SMS
twilio blazor csharp blazor-server sms twilio
An outline of getting started with sending request for confirmation SMS messages from Blazor based upon a database of Helpers and self-assigned tasks using Twilio, including getting started.
Ref: Programmable SMS Quickstart for C# with .NET Core
Nb: Being .NET Core the at this link is easily merged into a Blazor Service
Message Generation
A Blazor Service is used to send the request for confirmation of availability to the helpers who have volunteered for activity tasks at an athletics meet. The message to be sent is generated in a razor page which calls the service with that message. The message is generated using activity and helper properties, one message being generated for each helper on the day. Where a helper has volunteered for more than one activity, the message is a compound of all their activities. The message is coded in the razor page using C# string interpolation that facilitates the use of string templates that allow you to embed expression values into string literal, including allowing the string to be spread over several lines with concatenation formalism.
An example is:
msg =$@"
Please reply Y to confirm you are still available for this or N if not ASAP
Helpers Round: {activity.Round.Round}
At: {activity.Location} Date: {activity.DatewithDayTS}
Thanks {activity.Helper.Name} for agreeing to do the following job:
{activity.Task} Pit:{activity.PitTS} Sheet: {activity.SheetTS}
{activity.StartTimeOfDayTS} to {activity.EndTimeOfDayTS}"
A string is generated for each Activity using its properties.
Sending the SMS Message
Nb: This section is not in “chronological” order. It starts with the actual code to send and SMS message then embellishes the naraitive with the coding requirements to make it work.
Once a confirmation request message has been constructed for the activity it is sent using a call to the custom TwilioService. The specific code for sending the message is:
var message = MessageResource.Create(
body: txt,
from: new Twilio.Types.PhoneNumber(from),
to: new Twilio.Types.PhoneNumber(to)
);
Note that there is no actual message.Send() required.
Parameters
-
txt:
the message string
-
from:
the Twilio phone number that has been purchased (the monthe $US7)
-
to:
helper.Mobile number
Nuget Packages
The Twilio code requires a couple of references:
using Twilio;
using Twilio.Rest.Api.V2010.Account;
..which requires the Nuget package Twilio.AspNet.Core:
Twilio.AspNet.Core on GitHub
… and the Twilio REST API helper Nuget Package:
twilio-csharp on GitHub
In the project the file they appear as:
<ItemGroup>
<PackageReference Include="Twilio" Version="5.53.0" />
<PackageReference Include="Twilio.AspNet.Core" Version="5.37.2" />
</ItemGroup>
Initialisation
There needs to be a initialisation call before the call to send, to initialise the TwilioClient:
private string from { get; set; }
public void Init()
{
string accountSid = Settings.TwSetting.AccountSid;
string authToken = Settings.TwSetting.AuthToken;
from = Settings.TwSetting.TwilioMobile;
TwilioClient.Init(accountSid, authToken);
}
The Settings are read from the Settings table in the database. from
is the Twilio Mobile number used to send the SMS messages.
Next: Receiving and processing SMS Y/N responses using Twilio.
Topic | Subtopic | |
This Category Links | ||
Category: | Twilio SMS Index: | Twilio SMS |
Next: > | Blazor Helpers App Twilio | Receiving SMS |
< Prev: | Blazor Helpers App Twilio | Setting up |