forked from MissionMarsFourthHorizon/operation-max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provider.cs
95 lines (77 loc) · 2.92 KB
/
Provider.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
namespace HelpDeskBot.HandOff
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Bot.Connector;
public enum ConversationState
{
ConnectedToBot,
WaitingForAgent,
ConnectedToAgent
}
public class Conversation
{
public DateTime Timestamp { get; set; }
public ConversationReference User { get; set; }
public ConversationReference Agent { get; set; }
public ConversationState State { get; set; }
}
public class Provider
{
public Provider()
{
this.Conversations = new List<Conversation>();
}
public IList<Conversation> Conversations { get; private set; }
public int Pending()
{
return this.Conversations.Count(c => c.State == ConversationState.WaitingForAgent);
}
public Conversation CreateConversation(ConversationReference conversationReference)
{
var newConversation = new Conversation
{
User = conversationReference,
State = ConversationState.ConnectedToBot,
Timestamp = DateTime.Now
};
this.Conversations.Add(newConversation);
return newConversation;
}
public Conversation FindByConversationId(string userConversationId)
{
return this.Conversations.Where(c => c.User.Conversation.Id.Equals(userConversationId)).FirstOrDefault();
}
public Conversation FindByAgentId(string agentConversationId)
{
return this.Conversations.Where(c => c.Agent != null && c.Agent.Conversation.Id.Equals(agentConversationId)).FirstOrDefault();
}
public Conversation PeekConversation(ConversationReference agentReference)
{
var conversation = this.Conversations
.Where(c => c.State == ConversationState.WaitingForAgent)
.OrderByDescending(c => c.Timestamp).FirstOrDefault();
if (conversation != null)
{
conversation.State = ConversationState.ConnectedToAgent;
conversation.Agent = agentReference;
}
return conversation;
}
public bool QueueMe(ConversationReference conversationReference)
{
var conversation = this.FindByConversationId(conversationReference.Conversation.Id);
if (conversation == null)
{
conversation = this.CreateConversation(conversationReference);
}
if (conversation.State == ConversationState.ConnectedToBot)
{
conversation.State = ConversationState.WaitingForAgent;
return true;
}
return false;
}
}
}