Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Bot.Dialogs
- {
- using Microsoft.Bot.Builder;
- using Microsoft.Bot.Builder.Dialogs;
- using Microsoft.Bot.Schema;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- public class HTMLPromptResult
- {
- public string Format { get; set; }
- public string Text { get; set; }
- }
- public class HTMLPrompt : Prompt<HTMLPromptResult>
- {
- public HTMLPrompt(string dialogId, PromptValidator<HTMLPromptResult> validator = null) : base(dialogId, validator)
- {
- }
- protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, bool isRetry, CancellationToken cancellationToken = default)
- {
- if (turnContext == null)
- {
- throw new ArgumentNullException(nameof(turnContext));
- }
- if (options == null)
- {
- throw new ArgumentNullException(nameof(options));
- }
- if (isRetry && options.RetryPrompt != null)
- {
- await turnContext.SendActivityAsync(options.RetryPrompt, cancellationToken).ConfigureAwait(false);
- }
- else if (options.Prompt != null)
- {
- await turnContext.SendActivityAsync(options.Prompt, cancellationToken).ConfigureAwait(false);
- }
- }
- protected override Task<PromptRecognizerResult<HTMLPromptResult>> OnRecognizeAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, CancellationToken cancellationToken = default)
- {
- if (turnContext == null)
- {
- throw new ArgumentNullException(nameof(turnContext));
- }
- var result = new PromptRecognizerResult<HTMLPromptResult>();
- HTMLPromptResult htmlResult = new HTMLPromptResult();
- if (turnContext.Activity.Type == ActivityTypes.Message)
- {
- var message = turnContext.Activity.AsMessageActivity();
- var attachment = message.Attachments?.Where(a => a.ContentType.Equals("text/html"))?.FirstOrDefault();
- if (attachment?.Content != null)
- {
- htmlResult.Format = attachment.ContentType;
- htmlResult.Text = attachment.Content.ToString();
- result.Succeeded = true;
- result.Value = htmlResult;
- }
- else if (message.Text != null)
- {
- htmlResult.Format = message.TextFormat;
- htmlResult.Text = message.Text;
- result.Succeeded = true;
- result.Value = htmlResult;
- }
- }
- return Task.FromResult(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement