/
EchoDialog.cs
164 lines (146 loc) · 6.08 KB
/
EchoDialog.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.ConnectorEx;
using Newtonsoft.Json;
using System.Timers;
using Cafex.LiveAssist.Bot;
using System.Net.Http;
namespace Microsoft.Bot.Sample.SimpleEchoBot
{
[Serializable]
public class EchoDialog : IDialog<object>
{
protected int count = 1;
private static Sdk sdk;
private static ChatContext chatContext;
private static string conversationRef;
private static Timer timer;
public async Task StartAsync(IDialogContext context)
{
sdk = sdk ?? new Sdk(new SdkConfiguration()
{
AccountNumber = "__CHANGE_ME__"
});
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var activity = await argument as Activity;
if (chatContext != null)
{
// As chatContext is not null we already have an escalated chat.
// Post the incoming message line to the escalated chat
await sdk.PostLine(activity.Text, chatContext);
}else if (activity.Text == "reset")
{
PromptDialog.Confirm(
context,
AfterResetAsync,
"Are you sure you want to reset the count?",
"Didn't get that!",
promptStyle: PromptStyle.Auto);
}
else if (activity.Text.Contains("help"))
{
// "help" within the message is our escalation trigger.
await context.PostAsync("Escalating to agent");
await Escalate(activity); // Implemented in next step.
}
else
{
await context.PostAsync($"{this.count++}: You said {activity.Text}");
context.Wait(MessageReceivedAsync);
}
}
private async Task Escalate(Activity activity)
{
// This is our reference to the upstream conversation
conversationRef = JsonConvert.SerializeObject(activity.ToConversationReference());
var chatSpec = new ChatSpec()
{
// Set Agent skill to target
Skill = "__CHANGE_ME__",
VisitorName = activity.From.Name
};
// Start timer to poll for Live Assist chat events
if (timer == null)
{
timer = timer ?? new Timer(5000);
// OnTimedEvent is implemented in the next step
timer.Elapsed += (sender, e) => OnTimedEvent(sender, e);
timer.Start();
}
// Request a chat via the Sdk
chatContext = await sdk.RequestChat(chatSpec);
}
public async Task AfterResetAsync(IDialogContext context, IAwaitable<bool> argument)
{
var confirm = await argument;
if (confirm)
{
this.count = 1;
await context.PostAsync("Reset count.");
}
else
{
await context.PostAsync("Did not reset count.");
}
context.Wait(MessageReceivedAsync);
}
async void OnTimedEvent(Object source, ElapsedEventArgs eea)
{
if (chatContext != null)
{
// Create an upstream reply
var reply = JsonConvert.DeserializeObject<ConversationReference>(conversationRef)
.GetPostToBotMessage().CreateReply();
// Create upstream connection on which to send reply
var client = new ConnectorClient(new Uri(reply.ServiceUrl));
// Poll Live Assist for events
var chatInfo = await sdk.Poll(chatContext);
if (chatInfo != null)
{
// ChatInfo.ChatEvents will contain events since last call to poll.
if (chatInfo.ChatEvents != null && chatInfo.ChatEvents.Count > 0)
{
foreach (ChatEvent e in chatInfo.ChatEvents)
{
switch (e.Type)
{
// type is either "state" or "line".
case "line":
// Source is either: "system", "agent" or "visitor"
if (e.Source.Equals("system"))
{
reply.From.Name = "system";
}
else if (e.Source.Equals("agent"))
{
reply.From.Name = chatInfo.AgentName;
}
else
{
break;
}
reply.Type = "message";
reply.Text = e.Text;
client.Conversations.ReplyToActivity(reply);
break;
case "state":
// State changes
// Valid values: "waiting", "chatting", "ended"
if (chatInfo.State.Equals("ended"))
{
chatContext = null;
}
break;
}
}
}
}
}
}
}
}