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 4.2 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
  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 Models;
  10. using Newtonsoft.Json;
  11. public class Program
  12. {
  13. private static string directLineSecret = ConfigurationManager.AppSettings["DirectLineSecret"];
  14. private static string botId = ConfigurationManager.AppSettings["BotId"];
  15. private static string fromUser = "DirectLineSampleClientUser";
  16. public static void Main(string[] args)
  17. {
  18. StartBotConversation().Wait();
  19. }
  20. private static async Task StartBotConversation()
  21. {
  22. DirectLineClient client = new DirectLineClient(directLineSecret);
  23. var conversation = await client.Conversations.StartConversationAsync();
  24. new System.Threading.Thread(async () => await ReadBotMessagesAsync(client, conversation.ConversationId)).Start();
  25. Console.Write("Command> ");
  26. while (true)
  27. {
  28. string input = Console.ReadLine().Trim();
  29. if (input.ToLower() == "exit")
  30. {
  31. break;
  32. }
  33. else
  34. {
  35. if (input.Length > 0)
  36. {
  37. Activity userMessage = new Activity
  38. {
  39. From = new ChannelAccount(fromUser),
  40. Text = input,
  41. Type = ActivityTypes.Message
  42. };
  43. await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
  44. }
  45. }
  46. }
  47. }
  48. private static async Task ReadBotMessagesAsync(DirectLineClient client, string conversationId)
  49. {
  50. string watermark = null;
  51. while (true)
  52. {
  53. var activitySet = await client.Conversations.GetActivitiesAsync(conversationId, watermark);
  54. watermark = activitySet?.Watermark;
  55. var activities = from x in activitySet.Activities
  56. where x.From.Id == botId
  57. select x;
  58. foreach (Activity activity in activities)
  59. {
  60. Console.WriteLine(activity.Text);
  61. if (activity.Attachments != null)
  62. {
  63. foreach (Attachment attachment in activity.Attachments)
  64. {
  65. switch (attachment.ContentType)
  66. {
  67. case "application/vnd.microsoft.card.hero":
  68. RenderHeroCard(attachment);
  69. break;
  70. case "image/png":
  71. Console.WriteLine($"Opening the requested image '{attachment.ContentUrl}'");
  72. Process.Start(attachment.ContentUrl);
  73. break;
  74. }
  75. }
  76. }
  77. Console.Write("Command> ");
  78. }
  79. await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
  80. }
  81. }
  82. private static void RenderHeroCard(Attachment attachment)
  83. {
  84. const int Width = 70;
  85. Func<string, string> contentLine = (content) => string.Format($"{{0, -{Width}}}", string.Format("{0," + ((Width + content.Length) / 2).ToString() + "}", content));
  86. var heroCard = JsonConvert.DeserializeObject<HeroCard>(attachment.Content.ToString());
  87. if (heroCard != null)
  88. {
  89. Console.WriteLine("/{0}", new string('*', Width + 1));
  90. Console.WriteLine("*{0}*", contentLine(heroCard.Title));
  91. Console.WriteLine("*{0}*", new string(' ', Width));
  92. Console.WriteLine("*{0}*", contentLine(heroCard.Text));
  93. Console.WriteLine("{0}/", new string('*', Width + 1));
  94. }
  95. }
  96. }
  97. }
Tip!

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

Comments

Loading...