Created
October 18, 2019 17:57
-
-
Save rob-derosa/4ee25f4166a8b0e6c63f20061727b6c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft Corporation. All rights reserved. | |
// Licensed under the MIT License. | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Bot.Builder; | |
using Microsoft.Bot.Builder.Dialogs; | |
using Microsoft.Bot.Builder.Dialogs.Choices; | |
using RocketInsurance.Bot.Models; | |
using RocketInsurance.Bot.Responses.FileClaim; | |
using RocketInsurance.Bot.Services; | |
namespace RocketInsurance.Bot.Dialogs | |
{ | |
public class FileClaimDialog : ComponentDialog | |
{ | |
private static FileClaimResponses _responder = new FileClaimResponses(); | |
private IStatePropertyAccessor<FileClaimState> _accessor; | |
private FileClaimState _state; | |
public FileClaimDialog( | |
BotServices botServices, | |
UserState userState, | |
IBotTelemetryClient telemetryClient) | |
: base(nameof(FileClaimDialog)) | |
{ | |
_accessor = userState.CreateProperty<FileClaimState>(nameof(FileClaimState)); | |
var waterfall = new WaterfallStep[] | |
{ | |
PromptPolicyNumber, | |
PromptVehicle, | |
PromptDescription, | |
PromptIntersection, | |
ConfirmClaim, | |
ShowClaimInfo, | |
}; | |
// To capture built-in waterfall dialog telemetry, set the telemetry client | |
// to the new waterfall dialog and add it to the component dialog | |
TelemetryClient = telemetryClient; | |
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfall) { TelemetryClient = telemetryClient } ); | |
AddDialog(new TextPrompt(nameof(TextPrompt))); | |
AddDialog(new AttachmentPrompt(nameof(AttachmentPrompt))); | |
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)) { Style = ListStyle.SuggestedAction }); | |
AddDialog(new NumberPrompt<long>(nameof(NumberPrompt<long>))); | |
InitialDialogId = nameof(WaterfallDialog); | |
} | |
public async Task<DialogTurnResult> PromptPolicyNumber(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
if (!string.IsNullOrEmpty(_state.PolicyNumber)) | |
{ | |
return await sc.NextAsync(_state.PolicyNumber); | |
} | |
else | |
{ | |
return await sc.PromptAsync(nameof(TextPrompt), new PromptOptions() | |
{ | |
Prompt = await _responder.RenderTemplate(sc.Context, sc.Context.Activity.Locale, FileClaimResponses.ResponseIds.PromptPolicyNumber), | |
}); | |
} | |
} | |
public async Task<DialogTurnResult> PromptVehicle(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
_state.PolicyNumber = sc.Result.ToString(); | |
if (!string.IsNullOrEmpty(_state.VehicleMakeModel)) | |
{ | |
return await sc.NextAsync(_state.VehicleMakeModel); | |
} | |
else | |
{ | |
var choices = new List<Choice> | |
{ | |
new Choice { Value = "2018 Audi A4", Synonyms = new List<string> { "22222" } }, | |
new Choice { Value = "2016 Nissan Sentra", Synonyms = new List<string> { "33333" } }, | |
new Choice { Value = "2014 Acura TL", Synonyms = new List<string> { "44444" } } | |
}; | |
return await sc.PromptAsync(nameof(ChoicePrompt), new PromptOptions() | |
{ | |
Prompt = await _responder.RenderTemplate(sc.Context, sc.Context.Activity.Locale, FileClaimResponses.ResponseIds.PromptVehicle), | |
Choices = choices, | |
Style = ListStyle.Auto, | |
}); | |
} | |
} | |
public async Task<DialogTurnResult> PromptDescription(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
if(sc.Result is FoundChoice) | |
_state.VehicleMakeModel = ((FoundChoice)sc.Result).Value; | |
if (!string.IsNullOrEmpty(_state.Description)) | |
{ | |
return await sc.NextAsync(_state.Description); | |
} | |
else | |
{ | |
return await sc.PromptAsync(nameof(TextPrompt), new PromptOptions() | |
{ | |
Prompt = await _responder.RenderTemplate(sc.Context, sc.Context.Activity.Locale, FileClaimResponses.ResponseIds.PromptDescription), | |
}); | |
} | |
} | |
public async Task<DialogTurnResult> PromptIntersection(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
_state.Description = sc.Result.ToString(); | |
if (!string.IsNullOrEmpty(_state.Intersection)) | |
{ | |
return await sc.NextAsync(_state.Intersection); | |
} | |
else | |
{ | |
return await sc.PromptAsync(nameof(TextPrompt), new PromptOptions() | |
{ | |
Prompt = await _responder.RenderTemplate(sc.Context, sc.Context.Activity.Locale, FileClaimResponses.ResponseIds.PromptIntersection), | |
}); | |
} | |
} | |
public async Task<DialogTurnResult> ConfirmClaim(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
_state.Intersection = sc.Result.ToString(); | |
await _responder.ReplyWith(sc.Context, FileClaimResponses.ResponseIds.ConfirmClaim); | |
await Task.Delay(4000); | |
return await sc.NextAsync(); | |
} | |
public async Task<DialogTurnResult> ShowClaimInfo(WaterfallStepContext sc, CancellationToken cancellationToken) | |
{ | |
_state = await _accessor.GetAsync(sc.Context, () => new FileClaimState()); | |
_state.ClaimNumber = "RIC874644"; | |
_state.VehicleImageUrl = "https://rocketinsurancebotywjqid.blob.core.windows.net/assets/audi_a4.png"; | |
_state.Status = "Under review"; | |
await _responder.ReplyWith(sc.Context, FileClaimResponses.ResponseIds.ShowClaim); | |
await Task.Delay(1000); | |
await _responder.ReplyWith(sc.Context, FileClaimResponses.ResponseIds.AutoClaimCard, new { _state } ); | |
return await sc.EndDialogAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment