-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicBot.cs
More file actions
182 lines (166 loc) · 6.5 KB
/
TopicBot.cs
File metadata and controls
182 lines (166 loc) · 6.5 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Linq;
using System.Collections.Generic;
using NumericOverflow.Bot.Models;
using NumericOverflow.Bot.Data;
namespace NumericOverflow.Bot.Services
{
public class TopicBot : BotBase
{
private ITopicIndexer TopicIndexer { get; set; }
private ITopicRepository TopicRepository { get; set; }
private ITopicParameterRepository TopicParameterRepository { get; set; }
private IDialogTextRepository DialogTextRepository { get; set; }
public TopicBot(ITopicIndexer topicIndexer, ITopicParameterRepository topicParameterRepository, IDialogTextRepository dialogTextRepository, ITopicRepository topicRepository)
{
this.TopicIndexer = topicIndexer;
this.TopicRepository = topicRepository;
this.TopicParameterRepository = topicParameterRepository;
this.DialogTextRepository = dialogTextRepository;
}
public override void PipeIn(BotRequest botRequest, ref bool nextPipe)
{
var stepState = this.GetCurrentState(botRequest.DialogState);
if (stepState == null)
{
throw new InvalidCastException();
}
if (stepState.CurrentStatus == TopicStepState.Status.Initialized)
{
this.GetInitialResponseFor(botRequest);
}
else if (stepState.CurrentStatus == TopicStepState.Status.FindTopic)
{
if (botRequest.SelectedChoice != null)
{
var topic = this.TopicRepository.GetTopic(botRequest.SelectedChoice.Id);
this.GetTopicSelectedResponseFor(botRequest, topic);
}
else
{
var bestTopics = this.TopicIndexer.GetBestScoredTopicsFor(botRequest.InputText);
var bestTopicsCount = bestTopics.Count();
if (bestTopicsCount == 0)
{
this.GetErrorResponseFor(botRequest);
}
else if (bestTopicsCount == 1)
{
this.GetTopicSelectedResponseFor(botRequest, bestTopics.First().Item1);
}
else
{
this.GetTopicChoicesResponseFor(botRequest, bestTopics.Select(t => t.Item1));
}
}
}
}
public override void PipeOut(BotRequest botRequest, ref bool nextPipe)
{
var stepState = this.GetCurrentState(botRequest.DialogState);
if (stepState.CurrentStatus == TopicStepState.Status.FindTopicParameter)
{
var topicParameter = botRequest.Bag as TopicParameter;
if (topicParameter != null)
{
if (! stepState.ResolvedParameters.Select(p => p.Id).Contains(topicParameter.Id) &&
stepState.TopicParameters.Select(p => p.Id).Contains(topicParameter.Id))
{
stepState.ResolvedParameters.Add(topicParameter);
stepState.ErrorCount = 0;
}
this.ResolveParameters(botRequest);
}
else
{
this.AddError(botRequest);
}
}
}
public virtual void GetDefaultResponseFor(BotRequest botRequest)
{
botRequest.DialogState = botRequest.DialogState;
botRequest.OutText = "";
botRequest.Choices = null;
}
public virtual void GetInitialResponseFor(BotRequest botRequest)
{
var currentState = GetCurrentState(botRequest.DialogState);
currentState.CurrentStatus = TopicStepState.Status.FindTopic;
currentState.ErrorCount = 0;
botRequest.OutText = this.DialogTextRepository.GetDialogTextFor("Bot Presentation") + "\r\n" +
this.DialogTextRepository.GetDialogTextFor("Select Topic");
}
public virtual void GetErrorResponseFor(BotRequest botRequest)
{
var currentState = GetCurrentState(botRequest.DialogState);
currentState.ErrorCount++;
botRequest.OutText = this.DialogTextRepository.GetDialogTextFor("Unknown Topic") + "\r\n" +
this.DialogTextRepository.GetDialogTextFor("Select Topic");
}
public virtual void GetTopicSelectedResponseFor(BotRequest botRequest, Topic topic)
{
var currentState = GetCurrentState(botRequest.DialogState);
currentState.ErrorCount = 0;
currentState.CurrentTopicId = topic.Id;
currentState.CurrentTopicInput = botRequest.InputText;
currentState.CurrentStatus = TopicStepState.Status.FindTopicParameter;
currentState.TopicParameters = this.GetParametersFor(currentState.CurrentTopicId).ToList();
currentState.ResolvedParameters = new List<TopicParameter>();
// botRequest.OutText = string.Format(this.DialogTextRepository.GetDialogTextFor("Topic Selected"), topic.Title);
botRequest.OutText = this.DialogTextRepository.GetDialogTextFor("Topic Selected" + topic.Title);
this.ResolveParameters(botRequest);
}
public virtual void GetTopicChoicesResponseFor(BotRequest botRequest, IEnumerable<Topic> topics)
{
var currentState = GetCurrentState(botRequest.DialogState);
currentState.ErrorCount = 0;
currentState.CurrentStatus = TopicStepState.Status.FindTopic;
botRequest.OutText = this.DialogTextRepository.GetDialogTextFor("Select Choice");
botRequest.Choices = this.GetChoicesFor(topics).ToList();
}
public virtual TopicStepState GetCurrentState(DialogState dialogState)
{
return dialogState.CurrentState as TopicStepState;
}
public virtual IEnumerable<ChoiceItem> GetChoicesFor(IEnumerable<Topic> topics)
{
return topics.Select(t => new ChoiceItem(t.Id, t.Title));
}
public virtual IEnumerable<TopicParameter> GetParametersFor(string topicId)
{
return this.TopicParameterRepository.GetTopicParameters(topicId);
}
public virtual void ResolveParameters(BotRequest botRequest)
{
var currentState = GetCurrentState(botRequest.DialogState);
var parameterToResolve = currentState.TopicParameters.Where(p => !currentState.ResolvedParameters.Select(r => r.Id).Contains(p.Id)).FirstOrDefault();
if (parameterToResolve == null)
{
// We have all the parameters => Run response
botRequest.OutText = this.GetCompletedText(botRequest);
currentState.CurrentStatus = TopicStepState.Status.Initialized;
this.OnFinalize(botRequest);
}
else
{
this.RedirectToParameter(parameterToResolve, botRequest);
}
}
public virtual void RedirectToParameter(TopicParameter parameterToResolve, BotRequest botRequest)
{
if (parameterToResolve.TypeName == typeof(DateTime).Name)
{
var currentState = GetCurrentState(botRequest.DialogState);
this.OnRedirect(new DateParameterState() { Id = parameterToResolve.Id, Context = currentState.CurrentTopicInput }, botRequest);
botRequest.OutText = botRequest.OutText + ". Now give me a date";
}
}
public virtual string GetCompletedText(BotRequest botRequest)
{
var currentState = GetCurrentState(botRequest.DialogState);
string parameters = string.Join("\r\n", currentState.ResolvedParameters.Select(p => p.Id + "=" + p.Value.ToString()));
return string.Format(this.DialogTextRepository.GetDialogTextFor("Topic Completed {0} {1}"), currentState.CurrentTopicId, parameters);
}
}
}