
Telehealth companies can enhance entry to healthcare for folks residing in distant areas. One frequent subject in distant areas is unreliable web service. It’s a problem for sufferers in distant areas to make appointments through internet purposes when web service is poor or not obtainable in any respect.
SMS is a dependable and low-cost various to succeed in folks residing in distant areas. On this article, you’ll discover ways to construct a physician appointment reserving system that permits customers to e book through SMS.
The structure of the SMS reserving system is illustrated under:
Twilio is the communication channel between Microsoft Azure Bot Service and the tip consumer. The core of the system is an Azure Bot constructed on the Microsoft Bot Framework. When the bot receives a message, it asks Language Understanding (LUIS) to investigate the message. LUIS responds with the intent of the message, which the bot makes use of to reply with a useful reply.
You’ll want these applied sciences to comply with alongside:
Azure configuration
It is advisable to create a useful resource group to retailer the Azure assets you’ll create later.
Open a PowerShell and register to Azure utilizing the Azure CLI:
az login
Run the next command to create a brand new useful resource group:
az group create --name rg-bot --location [AZURE_LOCATION]
Change [AZURE_LOCATION]
with the identify of your most popular Azure location.
“Every useful resource in Azure is saved in a useful resource group, and each useful resource and useful resource group in Azure is saved in a selected Azure area or location. There are different reasons for picking different locations, however mostly you need to choose the situation that’s closest to you and your finish customers. Yow will discover all areas by operating
az account list-locations -o desk
. Discover your most popular location and use the worth from theIdentify
column to specify the situation when creating Azure assets. Remember the fact that not all assets can be found in all Azure areas.” — Microsoft Docs
To work together with particular kinds of Azure assets, the useful resource supplier for these useful resource sorts must be enabled. Probably the most common resource providers are enabled by default, however you’ll be utilizing Language Understanding (LUIS) which is a part of the Cognitive Companies useful resource supplier that isn’t enabled by default.
You possibly can register the Cognitive Services resource provider in Azure Portal, or use the Azure CLI proven under.
First, test the standing of the Cognitive Companies useful resource supplier utilizing this command:
az supplier present --namespace Microsoft.CognitiveServices -o desk
If the RegistrationState is UnRegistered, then run the next command to register it:
az supplier register --namespace Microsoft.CognitiveServices --wait
This command could take a minute to finish.
Language Understanding (LUIS) is a cloud-based conversational AI service, a part of Azure’s Cognitive Services. It could course of pure language textual content to foretell total which means and pull out related, detailed data.
To discover ways to use LUIS, it’s essential know a couple of core ideas:
- Intent: The aim or aim expressed in a consumer’s utterance.
- Entity: An merchandise or a component that’s related to the consumer’s intent.
- Utterance: Unit of speech. Each sentence from a consumer is an utterance.
LUIS will act because the mind of the bot by serving to the bot perceive the incoming messages.
Create the LUIS app
Log in to the LUIS portal together with your Azure account.
If you’re a brand new consumer of LUIS, you may be prompted to Select an authoring useful resource.
Click on Create a brand new authoring useful resource should you haven’t created one earlier than.
One other modal will seem to Create a brand new authoring useful resource.
- Select “rg-bot” within the Azure useful resource group dropdown
- Enter “appointment-booking” within the Azure useful resource identify area.
- Choose your most popular area within the Location dropdown
- Select “F0” in Pricing Tier
- Click on Achieved
You can be redirected again to the earlier Select an authoring useful resource modal. Now, you’ll be able to click on on the Achieved button, then you may be redirected to the dashboard web page.
On the dashboard web page, click on Create new app. The Create new app modal will seem.
Enter “Appointment-booking” within the Identify area and click on Achieved.
After the app is created, a tutorial modal shall be proven. Click on outdoors the modal to go to the newly created app particulars web page.
You’ll want to gather some key data for later use.
- Click on on the MANAGE tab after which on the Settings hyperlink.
- Be aware of the App ID.
- Click on on the Azure Assets hyperlink on the left menu
- Click on on the Authoring Useful resource tab
Be aware of the Major key (LUIS API key) and the Endpoint URL.
Now, the appointment-booking LUIS app is created efficiently. The subsequent step is to create the center of the app: the LUIS mannequin.
Import the mannequin
There are two methods to create the mannequin. You possibly can navigate to the Construct tab of the app, after which create entities and intents manually. Or you’ll be able to import a predefined mannequin file into the app. To save lots of time, you’ll be able to obtain this LUIS model JSON file and import it.
- After downloading the JSON mannequin file, navigate to the Variations web page through MANAGE > Variations.
- Click on on the Import button
- Select “ Import as JSON “ to open the import popup
- Click on on the Select file button
- Choose your JSON file
- Click on on the Achieved button
The brand new mannequin shall be imported into the app.
Navigate to the Construct > App Property web page and choose “ vAppointmentBookingBot.LUISModel.json “ from the variations dropdown. Yow will discover it within the breadcrumb navigation on the top-left of the web page.
Now, you will note the newly created intents and entities.
After the intents and entities are imported, the Prepare button within the high navigation bar is enabled.
Click on the Prepare button to start out the coaching course of.
The Prepare button shall be disabled and the Check button shall be enabled after coaching is accomplished.
To check the brand new mannequin, click on on the Check button. A Check flyout panel will seem on the fitting. You possibly can kind an utterance into the check panel to attempt it out.
Within the screenshot under, “i need to see physician kathy” is given a rating of 0.973 out of 1 for the BookAppointment intent. Within the Examine window, it additionally identifies the Physician entity as “kathy” accurately.
Because the check end result seems fairly good, you’ll be able to publish the LUIS app now.
- Click on on the Publish button on the navigation bar
- Choose the Manufacturing Slot
- Click on Achieved
After publishing is accomplished, a profitable message notification is proven as proven above. Which means the LUIS app is prepared for use!
Create the bot utilizing a Bot Framework template
On this tutorial, you’re going to make use of Bot Framework v4 SDK Templates to create the bot venture. Open a shell and set up the Bot Framework templates utilizing the .NET CLI with these instructions:
dotnet new -i Microsoft.Bot.Framework.CSharp.EchoBot
dotnet new -i Microsoft.Bot.Framework.CSharp.CoreBot
dotnet new -i Microsoft.Bot.Framework.CSharp.EmptyBot
You’ll solely be utilizing the CoreBot template on this tutorial, however be happy to discover the EchoBot
and EmptyBot
template.
Now, you should utilize the newly put in template to generate a brand new bot venture. Run the next command to create the bot venture:
dotnet new corebot -n AppointmentBot
It’s also possible to create projects using the Bot Framework templates from the Visual Studio or VS Code.
After creating the venture with the earlier command, the venture is created into the AppointmentBot/CoreBot folder and the foundation namespace is about to “CoreBot”.
That is inconsistent with how .NET templates often work, however it could actually simply be rectified. The next PowerShell script will transfer the contents into the AppointmentBot folder, rename the venture, and alter all of the namespaces to “AppointmentBot”.
Run the next script utilizing PowerShell:
$CorrectProjectName = "AppointmentBot"
Push-Location "./$CorrectProjectName"
Transfer-Merchandise ./CoreBot/* ./
Take away-Merchandise ./CoreBot
Transfer-Merchandise ./CoreBot.csproj "./$CorrectProjectName.csproj"
Get-ChildItem * -Recurse -File | ForEach-Object Set-Content material $_
Pop-Location
Open the venture utilizing your most popular .NET editor. The venture construction will appear like the next:
The generated venture comes with a flight reserving bot pattern. Take away these associated fashions and dialog information as listed under:
[projectRoot]CognitiveModelsFlightBooking.cs[projectRoot]CognitiveModelsFlightBooking.json[projectRoot]CognitiveModelsFlightBookingEx.cs[projectRoot]DialogsBookingDialog.cs[projectRoot]DialogsMainDialog.cs[projectRoot]BookingDetails.cs[projectRoot]FlightBookingRecognizer.cs
To save lots of time, you’ll be able to run the script under to take away the above information. Run the script from the venture root folder by doing the next:
rm CognitiveModels/FlightBooking.cs
rm CognitiveModels/FlightBooking.json
rm CognitiveModels/FlightBookingEx.cs
rm Dialogs/BookingDialog.cs
rm Dialogs/MainDialog.cs
rm BookingDetails.cs
rm FlightBookingRecognizer.cs
Additionally, you will have to take away traces 41 to 51 within the Startup.cs
file. These are the references to the deleted information.
After the cleanup, the Startup
class will appear like the next:
Now that the venture has been cleaned up, you can begin implementing your personal logic. Subsequent, you’ll be creating the mannequin, which LUIS will return to us with evaluation information.
Subsequent, you’ll create these information underneath the CognitiveModels
folder:
DoctorBooking.cs
: This file will include theDoctorBooking
class, which represents the info returned by LUIS.DoctorBookingEx.cs
: This file will prolongDoctorBooking
utilizing a partial class to simplify accessing the entities of the LUIS outcomes
Create the CognitiveModels/Doctorbooking.cs
and add the next code:
I generated this class utilizing the Bot Framework CLI and supplied it to your comfort, however you may as well generate this your self.
You’ll want to put in node.js and the BF CLI first, if you wish to generate the
Doctorbooking.cs
your self.You possibly can set up the BF CLI utilizing the next command:
npm i -g @microsoft/botframework-cli
Obtain the LUIS model JSON file to your venture listing, after which run the next command from the venture root listing:
bf luis:generate:cs --in=AppointmentBookingBot.LUISModel.json --out=CognitiveModels/DoctorBooking.cs --className=AppointmentBot.CognitiveModels.DoctorBookin
Create the CognitiveModels/DoctorBookingEx.cs
file and add the next code:
Join the bot to the LUIS app
To combine the bot service with the LUIS app, it’s essential add the LUIS App ID, API key, and API Endpoint URL into the venture configuration.
Change the contents of appsettings.json
with the JSON under:
"MicrosoftAppType": "",
"MicrosoftAppId": "",
"MicrosoftAppPassword": "",
"MicrosoftAppTenantId": "",
"LuisAppId": "[YOUR_LUIS_APP_ID]",
"LuisApiKey": "<SET_USING_USER_SECRETS>",
"LuisApiEndpointUrl": "[LUIS_ENDPOINT_URL]"
Change [YOUR_LUIS_APP_ID]
together with your LUIS App ID, and [LUIS_ENDPOINT_URL]
with the LUIS Endpoint URL you took word of earlier.
Please word that you shouldn’t retailer delicate data together with API keys or tokens in your supply code. That’s why you’ll configure the LuisApiKey
utilizing the Secret Manager tool.
Allow the Secret Supervisor software to your venture by operating the next command on the venture root listing:
dotnet user-secrets init
Run the next command to configure the LuisApiKey
utilizing the Secret Supervisor:
dotnet user-secrets set "LuisApiKey" "[YOUR_LUIS_API_KEY]"
Change [YOUR_LUIS_API_KEY]
with the LUIS App Major Key you took word of earlier.
The bot utility will retrieve the settings you simply configured to determine the connection to your LUIS app within the AppointmentBookingRecognizer
class under. Create a brand new file AppointmentBookingRecognizer.cs
and add the next contents:
A recognizer is used to acknowledge consumer enter and return intents and entities inside a DialogContext
. Within the AppointmentBookingRecognizer
class, a connection is established to the LUIS API endpoint. It additionally implements the RecognizeAsync
technique, which is named by dialogs to extract intents and entities from a consumer’s utterance.
Management the dialog circulate utilizing dialogs
It is advisable to use Dialogs to handle conversations between the consumer and the bot.
“Dialogs are a central idea within the SDK, offering methods to handle a long-running dialog with the consumer. A Dialog will be composed with different dialogs.” — Microsoft Docs
Bot framework offers a wealthy set of dialogs to make it simpler to create a dialog circulate. On this instance, you’ll create an AppointmentBookingDialog
class to handle the principle dialog. It’s composed of some dialogs, together with a waterfall dialog and immediate dialogs.
The waterfall dialog is used to outline the sequence of steps. As illustrated within the diagram under, the bot interacts with the consumer through a linear course of.
Create a brand new file AppointmentDetails.cs
into venture root and add the next code:
The AppointmentDetails
class is the mannequin class for the dialog. Subsequent, create the AppointmentBookingDialog.cs
file into the Dialogs folder. AppointmentBookingDialog
class will implement the method above.
Add the next code to the file:
Most important Dialog
The MainDialog
class manages the principle course of circulate. Create the MainDialog.cs
file within the Dialogs folder (you’ll be able to obtain the code from here).
This diagram provides you an summary of what the MainDialog
class does.
It’s various code, however the necessary a part of the MainDialog
class is under:
In a nutshell, when a message exercise is acquired, the bot runs the MainDialog
. The MainDialog
prompts the consumer utilizing the IntroStepAsync
technique, then calls the ActStepAsync
technique.
Within the ActStepAsync
technique, the bot calls the LUIS app to get the luisResult
object which is able to embrace the consumer’s intent and entities. The consumer’s intent and entities are used to find out the following step, both performing validation or invoking different dialogs.
On the finish, the bot calls the FinalStepAsync
technique to finish or cancel the method.
Twilio adapter and controller
By default, the Azure Bot service will connect with the net chat channel which is dealt with by the default AdapterWithErrorHandler
adapter. The default adapter is injected into the default BotController
class, and the controller exposes an endpoint /api/messages.
To attach the bot to Twilio, you’ll create a brand new TwilioAdapterWithErrorHandler
class prolonged from the TwilioAdapter class. Run the next command to put in the Microsoft.Bot.Builder.Adapters.Twilio NuGet bundle:
dotnet add bundle Microsoft.Bot.Builder.Adapters.Twilio --version 4.15.0
Make sure that the Microsoft.Bot.Builder.Adapters.Twilio NuGet bundle is utilizing the identical variations as the opposite Microsoft.Bot.Builder.* packages specified within the csproj-file.
After the NuGet set up is accomplished, create the TwilioAdapterWithErrorHandler.cs
file within the venture root listing, and add the next code:
To deal with the HTTP webhook requests from Twilio, you’ll want so as to add a TwilloController
. Create a brand new file TwilioController.cs
within the Controllers folder, and add the next code:
The endpoint for TwilioController
is /api/twilio. After including the brand new endpoint, the bot can deal with messages through each internet channel and Twilio SMS channel.
Lastly, it’s essential register the dialogs and LUIS recognizer within the Startup
class. Insert the next traces on the finish of the ConfigureServices
technique within the Startup.cs
file:
You’ll want to put in the Bot Framework Emulator to check the bot regionally. To Set up the Bot Framework Emulator:
- Navigate to GitHub releases page of the Bot Framework Emulator venture
- Click on on the setup file to your OS to obtain it.
- After the obtain is accomplished, click on the file to start out the set up. Comply with the set up wizard, and use the default choices to finish the set up.
Subsequent, begin the bot venture utilizing the .NET CLI:
dotnet run
Now, begin the Bot Emulator, click on on the Open Bot button, and enter the bot’s URL in your native surroundings, which by default, is http://localhost:3978/api/messages.
Then click on on the Join button.
A chat window shall be opened. You possibly can kind the message and begin testing.
After you’re proud of the check outcomes, the bot will be deployed to Azure.
To deploy the .NET bot to Azure, it’s essential use Azure CLI to create the next assets:
To create a brand new App Service plan, run the next command:
az appservice plan create -g rg-bot -n asp-bot --location [AZURE_LOCATION] --sku F1
Right here’s what the parameters do:
-g
or— resource-group
: The useful resource group the useful resource needs to be positioned in, on this case into the “rg-bot” useful resource group you created earlier.-n
or— identify
: The identify of the App Service plan, which isasp-bot
. “asp” is brief for App Service plan.-l
or— location
: The Azure location the useful resource ought to reside in. Change[AZURE_LOCATION]
with the situation closest to you or your customers, such as you did when creating the useful resource group earlier.— sku
: The dimensions (CPU/RAM/and many others.) of the App Service plan by SKU, which on this case is F1 (free).
To verify the .NET venture shall be deployed accurately, you’ll have to generate a deployment file. The deployment file will be generated with the command under:
az bot prepare-deploy — lang Csharp — code-dir “.” — proj-file-path “AppointmentBot.csproj”
Please word that --code-dir
and --proj-file-path
have to match to resolve the trail to the venture file.
Create the managed identification utilizing the next command:
az identification create --resource-group "rg-bot" --name "identity-appointment-bot"
After the command is completed, a brand new identity-appointment-bot
managed identification has been added in Azure, which shall be used to create the brand new App Service and Bot Service within the subsequent step.
The App Service and Bot Service will be generated utilizing the present App Service plan and the Azure Useful resource Supervisor (ARM) template which is a part of the “CoreBot” .NET template.
You’re utilizing the DeploymentTemplates/template-with-preexisting-rg.json ARM template, nevertheless it requires numerous parameters, which is why it’s best to use a parameter file. Create a brand new ParameterFiles
folder within the venture root and create a brand new file RegisterAppParams.json
with the next contents:
Some parameters have already been configured with the names of the beforehand created assets, however you continue to have to replace a couple of with your personal particular settings:
appId
: The worth ofclientId
within the response of thecreate identification
command. It’s also possible to question theclientId
like this:az identification present -g rg-bot -n identity-appointment-bot --query clientId
tenantId
: The worth oftenantId
within the response of thecreate identification
command. It’s also possible to question thetenantId
like this:az account present --query tenantId
appServicePlanLocation
: The Azure area you used when creating your App Service plan.botId
: The identify to your Bot Service. This identify must be globally distinctive. Change[UNIQUE_SUFFIX]
with something that will make the identify distinctive, like “firstname-lastname1234”. If it does not settle for the identify, change it up and check out once more.newWebAppName
: The identify to your App Service. This identify must be globally distinctive as a result of will probably be used as a subdomain to azurewebsites.internet. Change[UNIQUE_SUFFIX]
with something that will make the identify distinctive, like “firstname-lastname1234”. If it does not settle for the identify, change it up and check out once more.
After the parameter file is up to date, run the next command to generate the App Service and Bot Service:
az deployment group create `
--resource-group "rg-bot" `
--template-file "./DeploymentTemplates/template-with-preexisting-rg.json" `
--parameters "@ParameterFiles/RegisterAppParams.json"
Azure will take a minute to deploy this infrastructure. After the App Service is generated, run the next command under to get the App Service hostname:
az webapp present -g rg-bot -n appointment-bot-[UNIQUE_SUFFIX] --query 'hostNames[0]'
Change [UNIQUE_SUFFIX]
with the suffix you used within the parameters file. Be aware of this hostname as you’ll need it later.
Now that the App Service infrastructure has been provisioned, you’ll be able to deploy your native .NET bot venture. Run the next command which is able to create a ZIP file and deploy the ZIP file to App Service:
az webapp up -g rg-bot -n appointment-bot-[UNIQUE_SUFFIX]
It could take about 30 seconds for the deployment to finish. When it’s carried out, you will note the success response proven under:
You’ve examined the bot regionally utilizing the net chat, however the aim of this tutorial is to make use of SMS to speak. To obtain and ship SMS messages, you’ll want a Twilio Cellphone Quantity.
- Go and buy a new phone number from Twilio. The price of the cellphone quantity shall be utilized to your free promotional credit score should you’re utilizing a trial account.
Make sure that to be aware of your new Twilio cellphone quantity. You’ll want it afterward! - If you’re utilizing a trial Twilio account, you’ll be able to solely ship textual content messages to Verified Caller IDs. Verify your phone number or the cellphone quantity you need to SMS if it isn’t on the record of Verified Caller IDs.
- Lastly, you’ll want to seek out your Twilio Account SID and Auth Token. Navigate to your Twilio account page and be aware of your Twilio Account SID and Auth Token situated on the backside left of the web page.
When your Twilio Cellphone Quantity receives a textual content message, Twilio ought to ahead it to your .NET bot hosted on Azure App Service.
To configure that, navigate to Cellphone numbers > Handle > Lively numbers, and click on in your Twilio Cellphone Quantity to entry the Configure web page.
Underneath the Messaging part, set the dropdown underneath CONFIGURE WITH OTHER HANDLERS to “Webhook”, and within the adjoining textual content area, enter “ https://”, then paste within the App Service hostname you took word of earlier, after which enter “/api/twilio”.
The URL ought to appear like https://your-hostname.azurewebsites.net/api/twilio.
Click on the Save button on the underside left. Be aware of this webhook URL, you’ll need it once more quickly.
Lastly, it’s essential add some configuration to your App Service. Run the next command to configure the app settings:
az webapp config appsettings set -g rg-bot -n appointment-bot-[UNIQUE_SUFFIX] --settings `
LuisApiKey=[YOUR_LUIS_API_KEY] `
TwilioNumber=[YOUR_TWILIO_PHONE_NUMBER] `
TwilioAccountSid=[YOUR_TWILIO_ACCOUNT_SID] `
TwilioAuthToken=[YOUR_TWILIO_AUTH_TOKEN] `
TwilioValidationUrl=[YOUR_BOT_TWILIO_ENDPOINT]
Earlier than operating the command, substitute the placeholders.
- Change
[YOUR_LUIS_API_KEY]
with the LUIS Major Key you took word of earlier. - Change
[YOUR_TWILIO_PHONE_NUMBER]
together with your Twilio Cellphone Quantity you acquire earlier. Enter the cellphone quantity utilizing the E.164 which seems like+11234567890
. - Change
[YOUR_TWILIO_ACCOUNT_SID]
together with your Twilio Account SID which you took word of earlier. - Change
[YOUR_BOT_TWILIO_ENDPOINT]
with the webhook URL you took word of earlier. It ought to appear like https://your-hostname.azurewebsites.net/api/twilio.
You possibly can restart the App Service to verify the app settings are loaded by the bot. Run the next command to restart the App Service:
az webapp restart -g rg-bot -n appointment-bot-[UNIQUE_SUFFIX]
Lastly, you’ve gotten constructed and assembled all of the shifting components. Let’s check it!
Ship a textual content message to your Twilio Cellphone Quantity, and it’s best to see a response out of your bot. Right here you’ll be able to see I despatched the under SMS messages to my Twilio cellphone quantity, and it really works!
There are some necessary bot options that aren’t coated on this article. You might wish to discover additional when growing a production-grade bot.
- Authentication: When a bot must entry assets on behalf of a consumer, you have to authenticate the consumer identification. The consumer authentication will be dealt with by Identification suppliers corresponding to Azure AD with OAuth 2.0. Your bot will use the token generated by Azure to entry these assets. The main points on the right way to add Azure AD authentication to a bot will be discovered in Microsoft’s documentation.
- Bot Framework Composer: It’s an open supply visible designer and authoring software to create a bot with Azure Bot Service. You should utilize it to construct dialogs within the UI and visualize the circulate to enterprise customers. It additionally means that you can prepare LUIS fashions inside the software, thus saving the necessity to change between totally different environments. In case your venture requires involving non-technical folks to bot improvement, it’s positively software to think about.
On this article, you walked by way of the steps to construct an SMS reserving system utilizing Twilio, Azure Bot Framework, and LUIS. You can prolong this by including extra channels, increasing the LUIS mannequin to assist real-life situations, and incorporating different options like picture recognition or a number of language assist.
Yow will discover the finished supply code in this GitHub repo.