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

Program.cs 5.0 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  1. namespace DirectLineSampleClient
  2. {
  3. using System;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Microsoft.Bot.Connector.DirectLine;
  9. using Newtonsoft.Json;
  10. using System.Threading;
  11. using System.Text;
  12. using WebSocketSharp;
  13. public class Program
  14. {
  15. private static string directLineSecret = ConfigurationManager.AppSettings["DirectLineSecret"];
  16. private static string botId = ConfigurationManager.AppSettings["BotId"];
  17. // fromUser is the field that identifies which user is sending activities to the Direct Line service.
  18. // Because this value is created and sent within your Direct Line client, your bot should not
  19. // trust the value for any security-sensitive operations. Instead, have the user log in and
  20. // store any sign-in tokens against the Conversation or Private state fields. Those fields
  21. // are secured by the conversation ID, which is protected with a signature.
  22. private static string fromUser = "DirectLineSampleClientUser";
  23. public static void Main(string[] args)
  24. {
  25. StartBotConversation().Wait();
  26. }
  27. private static async Task StartBotConversation()
  28. {
  29. // Obtain a token using the Direct Line secret
  30. var tokenResponse = await new DirectLineClient(directLineSecret).Tokens.GenerateTokenForNewConversationAsync();
  31. // Use token to create conversation
  32. var directLineClient = new DirectLineClient(tokenResponse.Token);
  33. var conversation = await directLineClient.Conversations.StartConversationAsync();
  34. using (var webSocketClient = new WebSocket(conversation.StreamUrl))
  35. {
  36. webSocketClient.OnMessage += WebSocketClient_OnMessage;
  37. webSocketClient.Connect();
  38. while (true)
  39. {
  40. string input = Console.ReadLine().Trim();
  41. if (input.ToLower() == "exit")
  42. {
  43. break;
  44. }
  45. else
  46. {
  47. if (input.Length > 0)
  48. {
  49. Activity userMessage = new Activity
  50. {
  51. From = new ChannelAccount(fromUser),
  52. Text = input,
  53. Type = ActivityTypes.Message
  54. };
  55. await directLineClient.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. private static void WebSocketClient_OnMessage(object sender, MessageEventArgs e)
  62. {
  63. // Occasionally, the Direct Line service sends an empty message as a liveness ping. Ignore these messages.
  64. if (string.IsNullOrWhiteSpace(e.Data))
  65. {
  66. return;
  67. }
  68. var activitySet = JsonConvert.DeserializeObject<ActivitySet>(e.Data);
  69. var activities = from x in activitySet.Activities
  70. where x.From.Id == botId
  71. select x;
  72. foreach (Activity activity in activities)
  73. {
  74. Console.WriteLine(activity.Text);
  75. if (activity.Attachments != null)
  76. {
  77. foreach (Attachment attachment in activity.Attachments)
  78. {
  79. switch (attachment.ContentType)
  80. {
  81. case "application/vnd.microsoft.card.hero":
  82. RenderHeroCard(attachment);
  83. break;
  84. case "image/png":
  85. Console.WriteLine($"Opening the requested image '{attachment.ContentUrl}'");
  86. Process.Start(attachment.ContentUrl);
  87. break;
  88. }
  89. }
  90. }
  91. Console.Write("Command> ");
  92. }
  93. }
  94. private static void RenderHeroCard(Attachment attachment)
  95. {
  96. const int Width = 70;
  97. Func<string, string> contentLine = (content) => string.Format($"{{0, -{Width}}}", string.Format("{0," + ((Width + content.Length) / 2).ToString() + "}", content));
  98. var heroCard = JsonConvert.DeserializeObject<HeroCard>(attachment.Content.ToString());
  99. if (heroCard != null)
  100. {
  101. Console.WriteLine("/{0}", new string('*', Width + 1));
  102. Console.WriteLine("*{0}*", contentLine(heroCard.Title));
  103. Console.WriteLine("*{0}*", new string(' ', Width));
  104. Console.WriteLine("*{0}*", contentLine(heroCard.Text));
  105. Console.WriteLine("{0}/", new string('*', Width + 1));
  106. }
  107. }
  108. }
  109. }
Tip!

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

Comments

Loading...