Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. namespace Bot.Dialogs
  2. {
  3. using Microsoft.Bot.Builder;
  4. using Microsoft.Bot.Builder.Dialogs;
  5. using Microsoft.Bot.Schema;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11.  
  12. public class HTMLPromptResult
  13. {
  14. public string Format { get; set; }
  15. public string Text { get; set; }
  16. }
  17.  
  18. public class HTMLPrompt : Prompt<HTMLPromptResult>
  19. {
  20. public HTMLPrompt(string dialogId, PromptValidator<HTMLPromptResult> validator = null) : base(dialogId, validator)
  21. {
  22. }
  23.  
  24. protected override async Task OnPromptAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, bool isRetry, CancellationToken cancellationToken = default)
  25. {
  26. if (turnContext == null)
  27. {
  28. throw new ArgumentNullException(nameof(turnContext));
  29. }
  30.  
  31. if (options == null)
  32. {
  33. throw new ArgumentNullException(nameof(options));
  34. }
  35.  
  36. if (isRetry && options.RetryPrompt != null)
  37. {
  38. await turnContext.SendActivityAsync(options.RetryPrompt, cancellationToken).ConfigureAwait(false);
  39. }
  40. else if (options.Prompt != null)
  41. {
  42. await turnContext.SendActivityAsync(options.Prompt, cancellationToken).ConfigureAwait(false);
  43. }
  44. }
  45.  
  46. protected override Task<PromptRecognizerResult<HTMLPromptResult>> OnRecognizeAsync(ITurnContext turnContext, IDictionary<string, object> state, PromptOptions options, CancellationToken cancellationToken = default)
  47. {
  48. if (turnContext == null)
  49. {
  50. throw new ArgumentNullException(nameof(turnContext));
  51. }
  52.  
  53. var result = new PromptRecognizerResult<HTMLPromptResult>();
  54. HTMLPromptResult htmlResult = new HTMLPromptResult();
  55.  
  56. if (turnContext.Activity.Type == ActivityTypes.Message)
  57. {
  58. var message = turnContext.Activity.AsMessageActivity();
  59. var attachment = message.Attachments?.Where(a => a.ContentType.Equals("text/html"))?.FirstOrDefault();
  60. if (attachment?.Content != null)
  61. {
  62. htmlResult.Format = attachment.ContentType;
  63. htmlResult.Text = attachment.Content.ToString();
  64. result.Succeeded = true;
  65. result.Value = htmlResult;
  66. }
  67. else if (message.Text != null)
  68. {
  69. htmlResult.Format = message.TextFormat;
  70. htmlResult.Text = message.Text;
  71. result.Succeeded = true;
  72. result.Value = htmlResult;
  73. }
  74. }
  75.  
  76. return Task.FromResult(result);
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement