-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatController.cs
More file actions
182 lines (161 loc) · 5.68 KB
/
Copy pathChatController.cs
File metadata and controls
182 lines (161 loc) · 5.68 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.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using Newtonsoft.Json;
using System.Text;
using Microsoft.AspNetCore.Cors;
using Newtonsoft.Json.Linq;
namespace ChatbotApi.Controllers
{
//Set a request for
/*
public class Response
{
public string type;
public string content;
public JsonResult json;
public string request;
public Response(string content)
{
this.content = content;
type = "text";
}
public Response(JsonResult json, string request)
{
type = "json";
this.json = json;
this.request = request;
}
}*/
public class Richiesta
{
public string content;
public string user;
public Richiesta(string content, string user="")
{
this.content = content;
this.user = user;
}
}
public class content
{
public string title;
public string subtitle;
public string img;
public content(string subtitle, string title="", string img="")
{
this.subtitle = subtitle;
this.title = title;
this.img = img;
}
}
public class Risposta
{
public bool status;
public List<content> content= new List<content>();
public Risposta(List<content> content, bool status=true)
{
this.status = status;
this.content = content;
}
public Risposta(string text, bool status = true)
{
this.status = status;
content cont = new content(text);
this.content.Add(cont);
}
public Risposta(string text, string subtext, bool status = true)
{
this.status = status;
content cont = new content(text, subtext);
this.content.Add(cont);
}
}
public class ChatController : Controller
{
[Produces("application/json")]
[Route("api/chat")]
[EnableCors("AllowAll")]
[HttpPost]
public async Task<string> Dialogflow([FromBody]Richiesta request)
{
// url dialogflow
string url = "https://api.api.ai/v1/query?v=20150910";
string apikey = "776c7c6814ee4d5da82a213e673f46a4";
//Richiesta request;
bool status = false;
string dialog_res="";
var response= new HttpResponseMessage();
Risposta resp;
string res_string;
// creazione request
HttpClient req = new HttpClient();
var values = new Dictionary<string, string>
{
{ "query", "\""+request.content+"\"" },
{ "lang", "it" },
{ "sessionId", "somerandomthing"}
};
var content = new StringContent(
JsonConvert.SerializeObject(values, Formatting.Indented),
Encoding.UTF8,
"application/json"
);
req.DefaultRequestHeaders.Add("Authorization", "Bearer " + apikey);
//Response ret= new Response("");
//req.DefaultRequestHeaders.Add("Content-Type", "application/json");
try
{
response = await req.PostAsync(url, content);
string responseString = await response.Content.ReadAsStringAsync();
JObject obj = JObject.Parse(responseString);
dialog_res = obj["result"]["fulfillment"]["speech"].ToString();
string diag_int = obj["result"]["metadata"]["intentName"].ToString();
//ret = new Response(dialog_res);
status = true;
resp = new Risposta(dialog_res, diag_int, status);
res_string = JsonConvert.SerializeObject(resp);
return res_string;
/*
switch (dialog_res)
{
case "news":
NewsController news = new NewsController();
if (request.user == "")
{
JsonResult novita = news.Get();
List<NewsController.News> var = novita.Data();
foreach (object nov in novita)
{
}
}
else
{
JsonResult novita = news.Get(request.user);
}
break;
case "services":
ServiziController serv = new ServiziController();
if (request.user == "")
ret = new Response(serv.Get(), "novità");
else
ret = new Response(serv.GetServiziUser(request.user), "novità");
break;
default:
resp = new Risposta(dialog_res, status);
string res_string = JsonConvert.SerializeObject(resp);
return res_string;
break;
}*/
}
catch
{
status = false;
}
resp = new Risposta("", status);
res_string = JsonConvert.SerializeObject(resp);
return res_string;
}
}
}