Call LUIS from FormFlow in C# - c#

I am using the Microsoft bot framework to create a bot for questioning the user and then understanding the answer. The user is questioned using the FormFlow API in bot framework and answers are retrieved. Here is the code for the formflow:
public enum Genders { none, Male, Female, Other};
[Serializable]
public class RegisterPatientForm
{
[Prompt("What is the patient`s name?")]
public string person_name;
[Prompt("What is the patients gender? {||}")]
public Genders gender;
[Prompt("What is the patients phone number?")]
[Pattern(#"(<Undefined control sequence>\d)?\s*\d{3}(-|\s*)\d{4}")]
public string phone_number;
[Prompt("What is the patients Date of birth?")]
public DateTime DOB;
[Prompt("What is the patients CNIC number?")]
public string cnic;
public static IForm<RegisterPatientForm> BuildForm()
{
OnCompletionAsyncDelegate<RegisterPatientForm> processHotelsSearch = async (context, state) =>
{
await context.PostAsync($"Patient {state.person_name} registered");
};
return new FormBuilder<RegisterPatientForm>()
.Field(nameof(person_name),
validate: async (state, value) =>
{
//code here for calling luis
})
.Field(nameof(gender))
.Field(nameof(phone_number))
.Field(nameof(DOB))
.Field(nameof(cnic))
.OnCompletion(processHotelsSearch)
.Build();
}
}
The user may enter when asked for name :
my name is James Bond
Also the name could be of variable length. I would be better to call luis from here and get the entity(name) for the intent. I am currently not aware on how could i call a luis dialog from the formflow.

You can use the API method of LUIS, instead of the dialog method.
Your code would be - RegisterPatientForm Class :
public enum Genders { none, Male, Female, Other };
[Serializable]
public class RegisterPatientForm
{
[Prompt("What is the patient`s name?")]
public string person_name;
[Prompt("What is the patients gender? {||}")]
public Genders gender;
[Prompt("What is the patients phone number?")]
[Pattern(#"(<Undefined control sequence>\d)?\s*\d{3}(-|\s*)\d{4}")]
public string phone_number;
[Prompt("What is the patients Date of birth?")]
public DateTime DOB;
[Prompt("What is the patients CNIC number?")]
public string cnic;
public static IForm<RegisterPatientForm> BuildForm()
{
OnCompletionAsyncDelegate<RegisterPatientForm> processHotelsSearch = async (context, state) =>
{
await context.PostAsync($"Patient {state.person_name} registered");
};
return new FormBuilder<RegisterPatientForm>()
.Field(nameof(person_name),
validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
//Query LUIS and get the response
LUISOutput LuisOutput = await GetIntentAndEntitiesFromLUIS((string)response);
//Now you have the intents and entities in LuisOutput object
//See if your entity is present in the intent and then retrieve the value
if (Array.Find(LuisOutput.intents, intent => intent.Intent == "GetName") != null)
{
LUISEntity LuisEntity = Array.Find(LuisOutput.entities, element => element.Type == "name");
if (LuisEntity != null)
{
//Store the found response in resut
result.Value = LuisEntity.Entity;
}
else
{
//Name not found in the response
result.IsValid = false;
}
}
else
{
//Intent not found
result.IsValid = false;
}
return result;
})
.Field(nameof(gender))
.Field(nameof(phone_number))
.Field(nameof(DOB))
.Field(nameof(cnic))
.OnCompletion(processHotelsSearch)
.Build();
}
public static async Task<LUISOutput> GetIntentAndEntitiesFromLUIS(string Query)
{
Query = Uri.EscapeDataString(Query);
LUISOutput luisData = new LUISOutput();
try
{
using (HttpClient client = new HttpClient())
{
string RequestURI = WebConfigurationManager.AppSettings["LuisModelEndpoint"] + Query;
HttpResponseMessage msg = await client.GetAsync(RequestURI);
if (msg.IsSuccessStatusCode)
{
var JsonDataResponse = await msg.Content.ReadAsStringAsync();
luisData = JsonConvert.DeserializeObject<LUISOutput>(JsonDataResponse);
}
}
}
catch (Exception ex)
{
}
return luisData;
}
}
Here GetIntentAndEntitiesFromLUIS method does the querying to LUIS using the endpoint exposed by your Luis app. Add the endpoint to your Web.config with key LuisModelEndpoint
Find your luis endpoint by going to Publish tab in your luis app
Your web.config would look like this
<appSettings>
<!-- update these with your BotId, Microsoft App Id and your Microsoft App Password-->
<add key="BotId" value="YourBotId" />
<add key="MicrosoftAppId" value="" />
<add key="MicrosoftAppPassword" value="" />
<add key="LuisModelEndpoint" value="https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/YOUR_MODEL_ID?subscription-key=YOUR_SUBSCRIPTION_KEY&verbose=true&timezoneOffset=0&q="/>
</appSettings>
I have created a LUISOutput class to deserialize the response:
public class LUISOutput
{
public string query { get; set; }
public LUISIntent[] intents { get; set; }
public LUISEntity[] entities { get; set; }
}
public class LUISEntity
{
public string Entity { get; set; }
public string Type { get; set; }
public string StartIndex { get; set; }
public string EndIndex { get; set; }
public float Score { get; set; }
}
public class LUISIntent
{
public string Intent { get; set; }
public float Score { get; set; }
}
Emulator Response

In this situation, it may be best to call a luis intent outside of formflow. The functionality you are looking for does not exactly exist. You could call a "Name" intent then call you form like this:
[LuisIntent("Name")]
public async Task Name(IDialogContext context, LuisResult result)
{
//save result in variable
var name = result.query
//call your form
var pizzaForm = new FormDialog<PizzaOrder>(new PizzaOrder(), this.MakePizzaForm, FormOptions.PromptInStart, entities);
context.Call<PizzaOrder>(pizzaForm, PizzaFormComplete);
context.Wait(this.MessageReceived);
}

Related

Create a schedule for a runbook in automation with C#

So, I have been struggling with this for a while now, but I am getting a bit further each day. But now I am really stuck.
What I am trying to do:
I have an automation account with a runbook (PowerShell)
I want to create a schedule
Then I want to connect that schedule to the runbook so the runbook is executed at a specific date and time.
I want to achieve this through C# with the Management API of Azure. I found a few webpages that should give me all the information I needed, but it doesn't work.
The first one is this: https://learn.microsoft.com/en-us/rest/api/automation/schedule/create-or-update?tabs=HTTP
I get this to work and I can see the schedule after I ran the code.
The second one is this: https://learn.microsoft.com/en-us/rest/api/automation/job-schedule/create?tabs=HTTP
And here I get stuck. I get a 404 error when I execute the request and I have no idea what I am doing wrong.
The code below is far from perfect, but it's a POC and not the actual application. I want to make it work before I refactor.
This class has all the logic for the schedule:
public class ScheduleLogic
{
const string subscriptionId = "######";
const string resourceGroupName = "######";
const string automationAccountName = "######";
const string tenantId = "######";
const string clientId = "######";
const string clientSecret = "######";
const string resourceId = "https://management.azure.com/";
public string accessToken { get; set; }
// THIS ONE DOESN'T WORK!
public async Task ConnectScheduleToRunbook(RunbookSchedule runbookSchedule)
{
StringContent body = new(JsonSerializer.Serialize(runbookSchedule));
body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/jobSchedules/{runbookSchedule.Properties.Schedule.JobScheduleId}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Put, url, body);
}
public async Task CreateSchedule(Schedule schedule)
{
if (string.IsNullOrEmpty(schedule.JobScheduleId))
throw new Exception("Job schedule ID is empty");
StringContent body = new(JsonSerializer.Serialize(schedule));
body.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/schedules/{schedule.JobScheduleId}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Put, url, body);
}
public async Task DeleteSchedule(string scheduleName)
{
string url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/schedules/{scheduleName}?api-version=2019-06-01";
await ExecuteRequest(RequestType.Delete, url, null);
}
private async Task ExecuteRequest(RequestType requestType, string url, StringContent? body)
{
HttpResponseMessage response;
using (HttpClient client = new())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {await GetAccessToken()}");
switch (requestType)
{
case RequestType.Put:
if (body == null)
throw new Exception("Body cannot be empty");
response = await client.PutAsync(url, body);
break;
case RequestType.Delete:
response = await client.DeleteAsync(url);
break;
default:
throw new Exception("Unknown request type");
}
if (!response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
throw new Exception(content);
}
}
}
private async Task<string> GetAccessToken()
{
if (!string.IsNullOrEmpty(accessToken))
return accessToken;
AuthenticationContext authContext = new("https://login.microsoftonline.com/" + tenantId);
ClientCredential clientCreds = new(clientId, clientSecret);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceId, clientCreds);
accessToken = authResult.AccessToken;
return accessToken;
}
}
Models:
public class RunbookSchedule
{
public RunbookScheduleProperties Properties { get; set; }
}
public class RunbookScheduleProperties
{
public Schedule Schedule { get; set; }
public Runbook Runbook { get; set; }
public object Parameters { get; set; }
}
public class Runbook
{
public string Name { get; set; }
}
public class Schedule
{
public string Name { get; set; }
[JsonIgnore]
public string JobScheduleId { get; set; }
[JsonPropertyName("properties")]
public ScheduleProperties ScheduleProperties { get; set; }
}
public class ScheduleProperties
{
public string Description { get; set; }
public DateTime StartTime { get; set; }
public DateTime ExpiryTime { get; set; }
public string Frequency { get; set; }
public object AdvancedSchedule => new { };
}
And the unit tests I use to test the logic:
public class ScheduleTests
{
private readonly string jobId = "d52004cc-b7ec-4b9b-99c1-3922492f6e1b-1";
private readonly string runbookName = "RunFunctionApp";
private readonly ScheduleLogic scheduleService;
public ScheduleTests()
{
scheduleService = new();
}
[Fact]
public async Task Should_CreateASchedule()
{
Schedule newScheduleSettings = new()
{
Name = jobId,
JobScheduleId = jobId,
ScheduleProperties = new()
{
Description = "Here is another example",
StartTime = new DateTime(2024, 03, 27, 9, 0, 0, 0),
ExpiryTime = new DateTime(2024, 03, 27, 10, 0, 0, 0),
Frequency = "OneTime"
}
};
await scheduleService.CreateSchedule(newScheduleSettings);
}
[Fact]
public async Task Should_DeleteSchedule()
{
await scheduleService.DeleteSchedule(jobId);
}
[Fact]
public async Task Should_ConnectScheduleAndRunbook()
{
await scheduleService.ConnectScheduleToRunbook(new RunbookSchedule
{
Properties = new()
{
Schedule = new() { Name = jobId, JobScheduleId = jobId },
Runbook = new() { Name = runbookName },
Parameters = new { id = 12 }
}
});
}
}
Somehow I think I am doing something stupid and the fix/answer is really simple.
I tried to mess around with the URL because I think there is a problem with that, not sure why... Just a feeling.
Google did show me some results, but those were for schedules of other services. Runbooks and automation don't seem to be very popular.
ChatGPT comes with older, obsolete solutions. Visual Studio gives a lot of green and even red lines when I try those suggestions.

Posting in blazor returns exception stating that "The JSON value could not be converted to System.Int32."

I am new to Blazor and I am encountering the following issue when trying to post for data with an authentication token : at the time of the API call, an exception is lifted with the message "The JSON value could not be converted to System.Int32. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
Here's the code in my blazor page's code-behind :
public partial class ContactCreate : AuthenticatedPageBase
{
[Inject]
public IContactDataService ContactDataService { get; set; }
[Inject]
public ICountryDataService CountryDataService { get; set; }
public Contact.Post Model { get; set; } = new Contact.Post();
protected string CountryIdString { get; set; } = string.Empty;
protected string TokenString { get; set; } = string.Empty;
public string ErrorMessage { get; set; } = string.Empty;
protected List<Country.ListItem> Countries { get; set; } = new List<Country.ListItem>();
protected async override Task OnInitializedAsync()
{
await base.OnInitializedAsync();
Countries = (await CountryDataService.GetCountryListAsync(Token.Token)).ToList();
TokenString = Token.Token;
}
protected async Task HandleValidSubmit()
{
try
{
Model.CountryId = int.Parse(CountryIdString);
var response = await ContactDataService.PostContactAsync(TokenString, Model);
NavManager.NavigateTo("/contacts");
}
catch(Exception ex)
{
ErrorMessage = ex.Message;
}
}
protected void HandleInvalidSubmit()
{
ErrorMessage = "Le formulaire n'est pas valide. Veuillez réessayer.";
}
}
and here's the relevant code in the data service :
public async Task<int> PostContactAsync(string token, Contact.Post model)
{
var response = await PostAuthenticatedAsync<int>(token, Url, model);
return response;
}
public async Task<T> PostAuthenticatedAsync<T>(string token, string url, object model)
{
var jsonBody = model.ToJson();
var request = new HttpRequestMessage()
{
RequestUri = new Uri(HttpClient.BaseAddress.ToString() + url),
Method = HttpMethod.Post,
Content = jsonBody
};
request.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
var response = await HttpClient.SendAsync(request);
return await response.FromJson<T>(Options);
}
...and the extension method that serializes the object into json :
public static StringContent ToJson(this object o)
{
return new StringContent(JsonSerializer.Serialize(o), Encoding.UTF8, "application/json");
}
Here's the object model that I'm passing through :
public class Contact
{
public class Post
{
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(50)]
public string CompanyName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
[MaxLength(20)]
public string PostCode { get; set; }
[MaxLength(60)]
public string Locality { get; set; }
public int CountryId { get; set; }
}
}
And, finally, here's the API method that I'm trying to reach :
[HttpPost]
public async Task<ActionResult> PostContact(Contact.Post model)
{
try
{
var createdId = await _contactRepository.CreateAsync(model);
return Ok(new { Id = createdId });
}
catch (Exception ex)
{
return BadRequest(new { ex.Message });
}
}
Any idea what is happening or what actual exception lies behind this cryptic error message ?
P.S. : I know that there is a question with that exact exception message but it concerns .NET Core while I'm targeting .NET Standard 2.1. I've read it and it visibly doesn't apply to this case.
You are not returning an int (the Id). You're returning an anonymous object with an int property named Id.
Try
return Ok(createdId);

Web api parse FromQuery parameter name

I have strict API routing requirements. This must be a Get request and the routing cannot be changed. The user can search for people by name, but he can also search for them by part of the name:
api/People?name=Tom - "Tom" can be in any part of the name, ignore case
api/People?name:contains=Tom - "Tom" must be at the beginning of the name, ignore case
api/People?name:exact=Tom - "Tom" must be case sensitive
How it should be implemented in the controller?
There is an option with 3 parameters, can this be improved?
public async Task<IActionResult> GetByName(
[FromQuery(Name = "name")] string name,
[FromQuery(Name = "name:contains")] string beginName,
[FromQuery(Name = "name:exact")] string exactName)
You can use custom model binding,here is a demo:
NameModel:
public class NameModel {
public string name { get; set; }
public string beginName { get; set; }
public string exactName { get; set; }
}
Controller:
[Route("/GetByName")]
public async Task<IActionResult> GetByName([ModelBinder(typeof(NameBinder))]NameModel nameModel)
{
return Ok();
}
NameBinder:
public class NameBinder:IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var model1 = new NameModel();
var name=bindingContext.ValueProvider.GetValue("name").FirstValue;
var beginName = bindingContext.ValueProvider.GetValue("name:contains").FirstValue;
var exactName = bindingContext.ValueProvider.GetValue("name:exact").FirstValue;
if (name.ToLower().Contains("tom")) {
model1.name = name;
}
if (beginName.ToLower().StartsWith("tom")) {
model1.beginName = beginName;
}
if (exactName.Contains("Tom")) {
model1.exactName = exactName;
}
bindingContext.Result = ModelBindingResult.Success(model1);
return Task.CompletedTask;
}
}
result:

Parse FHIR Patient response to json

I'm new to .Net and FHIR. I followed a few tutorial to understand how FHIR API works. I need to create an application that use only GET request to get data from server. Below, I am trying to make a request to retrieve a patient by ID in PatientRepository class. However, when I test it with Postman, it doesn't return any response. How should I change my code? Many thanks
Model:
public class Patient : Hl7.Fhir.Model.Patient
{
public string name { get; set; }
public string birthday { get; set; }
}
public class PatientList
{
public List<Patient> Patients { get; set; }
}
Controller:
public class PatientController : Controller
{
private readonly IPatientRepository _patientRepository;
public PatientController(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
}
[HttpGet]
[Route("api/GetPatientById/{id}")]
public IActionResult getPatientById(long id)
{
var model = _patientRepository.GetPatientById(id);
if (model == null)
return NotFound();
return Ok(model);
}
}
}
PatientRepository:
public class PatientRepository : IPatientRepository
{
public async Task<Patient> GetPatientById(long id)
{
var client = new FhirClient("https://fhir.****.***/hapi-fhir-jpaserver/fhir/");
client.Timeout = (60 * 100);
client.PreferredFormat = ResourceFormat.Json;
var pat = client.Read<Patient>("Patient/1");
var parser = new FhirJsonParser();
return new Patient
{
birthday = pat.birthday,
}
}
}

Azure Mobile Service for Windows Phone 8.1 - Insert to existing DB

I am writing an app that has an Azure database. I've never did nything connected with Azure, so I am new to all the stuff. I've found on the internet and at microsoft documentation some tutorials, but I must have got sth wrong, cause it doesn't work. So I have a table at my database called Week, I've created a model in my code:
[DataContract]
public class Week
{
//[JsonProperty(PropertyName = "Id")]
//[DataMember]
public int Id { get; set; }
[JsonProperty(PropertyName = "Book")]
[DataMember]
public Book CurrentBook { get; set; }
[JsonProperty(PropertyName = "Is_Read")]
[DataMember]
public Boolean IsRead { get; set; }
[JsonProperty(PropertyName = "Pages_Read")]
[DataMember]
public int PagesRead { get; set; }
[JsonProperty(PropertyName = "Start_Date")]
[DataMember]
public DateTime StartDate { get; set; }
[JsonProperty(PropertyName = "User")]
[DataMember]
public User Reader { get; set; }
[JsonProperty(PropertyName = "Week_Number")]
[DataMember]
public int WeekNumber { get; set; }
public Week(Book currentBook, Boolean isRead, int pagesRead, DateTime startDate, User reader, int weekNumber)
{
CurrentBook = currentBook;
IsRead = isRead;
PagesRead = pagesRead;
StartDate = startDate;
Reader = reader;
WeekNumber = weekNumber;
}
public Week()
{
}
public int GetMonth()
{
//TODO: Implement the method.
return 0;
}
}
Then I created the WeekRepository for CRUD operations:
public class WeekRepository : BaseRepository<Week>
{
private IMobileServiceTable<Week> weekTable;
public string errorMesage = string.Empty;
public WeekRepository()
{
weekTable = MobileService.GetTable<Week>();
}
public async override Task<int> Save(Week entity)
{
try
{
await weekTable.InsertAsync(entity);
// .ContinueWith(t =>
//{
// if (t.IsFaulted)
// {
// errorMesage = "Insert failed";
// }
// else
// {
// errorMesage = "Inserted a new item with id " + entity.Id;
// }
//});
}
catch (WebException ex)
{
errorMesage = ex.Message;
}
return entity.Id;
}
public override void Update(Week entity)
{
return;
}
public override Week Load(int bookId)
{
var week = weekTable.Where(w => w.IsRead == false).ToListAsync();
return week.Result.Single();
}
public override List<Week> LoadByUserId(int userId)
{
return new List<Week>();
}
public Week LoadCurrentWeek(int userId)
{
return new Week();
}
}
To test if it works, I wrote a simple test:
[TestMethod]
public void ShouldSaveWeekToTheDB()
{
//ARANGE
Week weekTestEntity = new Week(null, false, 10, new DateTime(), null, 1);
//ACT
int id = weekRepository.Save(weekTestEntity).Result;
//ASSERT
var savedItem = weekRepository.Load(1);
Assert.AreEqual(false, savedItem.IsRead);
}
However, InsertAsync() throws an exception - Not Found. I've no idea what I am doing wrong, cause it seems a simple thing as far as I can see from the material on the Internet.
If You could help me, I would be really grateful!
Thank You in advance!
Best Regards,
Roman.

Categories

Resources