This repository was archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (80 loc) · 3.36 KB
/
Program.cs
File metadata and controls
89 lines (80 loc) · 3.36 KB
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
namespace AzureBot.ConsoleConversation
{
using System;
using System.Configuration;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Bot.Connector.DirectLine;
using System.Linq;
internal class Program
{
private static string directLineToken = ConfigurationManager.AppSettings["DirectLineToken"];
private static string microsoftAppId = ConfigurationManager.AppSettings["MicrosoftAppId"];
private static string fromUser = ConfigurationManager.AppSettings["FromUser"];
private static string BotId = ConfigurationManager.AppSettings["BotId"];
private static DirectLineClient client = new DirectLineClient(directLineToken);
private static string conversationId;
internal static void Main(string[] args)
{
StartBotConversation().Wait();
}
internal static async Task StartBotConversation()
{
var conversation = await client.Conversations.StartConversationAsync();
conversationId = conversation.ConversationId;
new System.Threading.Thread(async () => await ReadBotMessagesAsync()).Start();
//After authenticating using this app, then the tests in the Tests project should work
//as long as the FromUser setting is the same between them
Console.Write("Command > ");
while (true)
{
string input = Console.ReadLine().Trim();
if (input.ToLower() == "exit")
{
break;
}
else
{
if (input.Length > 0)
{
Activity userMessage = new Activity
{
Type = ActivityTypes.Message,
From = new ChannelAccount { Id = fromUser },
Text = input
};
await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
}
}
}
}
internal static async Task ReadBotMessagesAsync()
{
string watermark = null;
while (true)
{
var activities = await client.Conversations.GetActivitiesAsync(conversationId, watermark);
watermark = activities?.Watermark;
var activitiesText = from x in activities.Activities
where x.From.Id == BotId
select x;
foreach (Activity activity in activitiesText)
{
if (!string.IsNullOrEmpty(activity.Text.Trim()))
{
Console.WriteLine(activity.Text);
}
else
{
foreach (Attachment attachment in activity.Attachments)
{
Console.WriteLine(attachment.Content);
}
}
Console.Write("Command > ");
}
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}
}
}
}