-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotService.aspx.cs
More file actions
101 lines (90 loc) · 3.04 KB
/
Copy pathBotService.aspx.cs
File metadata and controls
101 lines (90 loc) · 3.04 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
using System;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using Syn.Bot.Channels.Testing;
using Syn.Bot.Channels.Widget;
using Syn.Bot.Oscova;
namespace UniFinder
{
public partial class BotService : Page
{
private static WidgetChannel WidgetChannel { get; }
private static OscovaBot Bot { get; }
static BotService()
{
Bot = new OscovaBot();
WidgetChannel = new WidgetChannel(Bot);
// Add the pre-built channel test dialog.
Bot.Dialogs.Add(new ChannelTestDialog(Bot));
// Start training.
Bot.Trainer.StartTraining();
var websiteUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
WidgetChannel.ServiceUrl = websiteUrl + "/BotService.aspx";
WidgetChannel.ResourceUrl = websiteUrl + "/BotResources";
WidgetChannel.WidgetTitle = "EduBot!";
WidgetChannel.LaunchButtonText = "Ask";
WidgetChannel.InputPlaceHolder = "Ask your query here...";
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
WidgetChannel.Process(Request, Response);
}
catch (Exception ex)
{
// Log the exception to see if there's an issue
Debug.WriteLine("BotService Error: " + ex.Message);
Response.Write("Error: " + ex.Message);
}
}
[WebMethod]
public static string Chat(string message)
{
try
{
string result = RunPythonScript(message);
return result;
}
catch (Exception ex)
{
return "Error: " + ex.Message;
}
}
private static string RunPythonScript(string message)
{
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "python",
Arguments = $"\"E:\\UniFinder\\Scripts\\AIChatbot.py\" \"{message}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
return result;
}
}
using (StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
return $"Error: {error}";
}
}
}
return "Error: No response from script.";
}
}
}