Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

SurveyTriggerer.cs 2.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  1. namespace CreateNewConversationBot
  2. {
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Autofac;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Builder.Dialogs.Internals;
  9. using Microsoft.Bot.Connector;
  10. using Microsoft.Rest;
  11. public static class SurveyTriggerer
  12. {
  13. public static async Task StartSurvey(ConversationReference conversationReference, CancellationToken token)
  14. {
  15. var container = WebApiApplication.FindContainer();
  16. // the ConversationReference has the "key" necessary to resume the conversation
  17. var message = conversationReference.GetPostToBotMessage();
  18. ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));
  19. try
  20. {
  21. var conversation = await client.Conversations.CreateDirectConversationAsync(message.Recipient, message.From);
  22. message.Conversation.Id = conversation.Id;
  23. }
  24. catch (HttpOperationException ex)
  25. {
  26. var reply = message.CreateReply();
  27. reply.Text = ex.Message;
  28. await client.Conversations.SendToConversationAsync(reply);
  29. return;
  30. }
  31. // we instantiate our dependencies based on an IMessageActivity implementation
  32. using (var scope = DialogModule.BeginLifetimeScope(container, message))
  33. {
  34. // find the bot data interface and load up the conversation dialog state
  35. var botData = scope.Resolve<IBotData>();
  36. await botData.LoadAsync(token);
  37. // resolve the dialog task
  38. IDialogTask task = scope.Resolve<IDialogTask>();
  39. // make a dialog to push on the top of the stack
  40. var child = scope.Resolve<SurveyDialog>();
  41. // wrap it with an additional dialog that will restart the wait for
  42. // messages from the user once the child dialog has finished
  43. var interruption = child.Void<object, IMessageActivity>();
  44. try
  45. {
  46. // put the interrupting dialog on the stack
  47. task.Call(interruption, null);
  48. // start running the interrupting dialog
  49. await task.PollAsync(token);
  50. }
  51. finally
  52. {
  53. // save out the conversation dialog state
  54. await botData.FlushAsync(token);
  55. }
  56. }
  57. }
  58. }
  59. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...