forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
112 lines (91 loc) · 4.02 KB
/
Program.cs
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
namespace DirectLineSampleClient
{
using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Connector.DirectLine;
using Microsoft.Bot.Connector.DirectLine.Models;
using Models;
using Newtonsoft.Json;
public class Program
{
private static string directLineSecret = ConfigurationManager.AppSettings["DirectLineSecret"];
private static string botId = ConfigurationManager.AppSettings["BotId"];
private static string fromUser = "DirectLineSampleClientUser";
public static void Main(string[] args)
{
StartBotConversation().Wait();
}
private static async Task StartBotConversation()
{
DirectLineClient client = new DirectLineClient(directLineSecret);
var conversation = await client.Conversations.NewConversationAsync();
new System.Threading.Thread(async () => await ReadBotMessagesAsync(client, conversation.ConversationId)).Start();
Console.Write("Command> ");
while (true)
{
string input = Console.ReadLine().Trim();
if (input.ToLower() == "exit")
{
break;
}
else
{
if (input.Length > 0)
{
Message userMessage = new Message
{
FromProperty = fromUser,
Text = input
};
await client.Conversations.PostMessageAsync(conversation.ConversationId, userMessage);
}
}
}
}
private static async Task ReadBotMessagesAsync(DirectLineClient client, string conversationId)
{
string watermark = null;
while (true)
{
var messages = await client.Conversations.GetMessagesAsync(conversationId, watermark);
watermark = messages?.Watermark;
var messagesFromBotText = from x in messages.Messages
where x.FromProperty == botId
select x;
foreach (Message message in messagesFromBotText)
{
Console.WriteLine(message.Text);
if (message.ChannelData != null)
{
var channelData = JsonConvert.DeserializeObject<DirectLineChannelData>(message.ChannelData.ToString());
switch (channelData.ContentType)
{
case "application/vnd.microsoft.card.hero":
RenderHeroCard(channelData);
break;
case "image/png":
Console.WriteLine($"Opening the requested image '{channelData.ContentUrl}'");
Process.Start(channelData.ContentUrl);
break;
}
}
Console.Write("Command> ");
}
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}
}
private static void RenderHeroCard(DirectLineChannelData channelData)
{
const int Width = 70;
Func<string, string> contentLine = (content) => string.Format($"{{0, -{Width}}}", string.Format("{0," + ((Width + content.Length) / 2).ToString() + "}", content));
Console.WriteLine("/{0}", new string('*', Width + 1));
Console.WriteLine("*{0}*", contentLine(channelData.Content.Title));
Console.WriteLine("*{0}*", new string(' ', Width));
Console.WriteLine("*{0}*", contentLine(channelData.Content.Text));
Console.WriteLine("{0}/", new string('*', Width + 1));
}
}
}