-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagesController.cs
129 lines (114 loc) · 4.83 KB
/
MessagesController.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
namespace LuisBot
{
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Web.Http;
using Dialogs;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Net.Http.Headers;
using System.Xml.Linq;
[BotAuthentication]
public class MessagesController : ApiController
{
string ApiKey = "e2e6074acad9425999d89ba796c85275";
string targetLang = "en";
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
// if (IsSpellCorrectionEnabled)
// {
try
{
// activity.Text = await this.spellService.GetCorrectedTextAsync(activity.Text);
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var input = activity.Text;
Task.Run(async () =>
{
var accessToken = await GetAuthenticationToken(ApiKey);
var output = await TranslateText(input, targetLang, accessToken);
Console.WriteLine(output);
activity.Text = output;
await Conversation.SendAsync(activity, () => new RootLuisDialog());
// Activity reply = activity.CreateReply($"Your text translation to English is => '{output}'");
// await connector.Conversations.ReplyToActivityAsync(reply);
}).Wait();
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
// }
//await Conversation.SendAsync(activity, () => new RootLuisDialog());
}
else
{
this.HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
static async Task<string> TranslateText(string inputText, string language, string accessToken)
{
string url = "http://api.microsofttranslator.com/v2/Http.svc/Translate";
string query = $"?text={System.Net.WebUtility.UrlEncode(inputText)}&to={language}&contentType=text/plain";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await client.GetAsync(url + query);
var result = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
return "Hata: " + result;
var translatedText = XElement.Parse(result).Value;
return translatedText;
}
}
static async Task<string> GetAuthenticationToken(string key)
{
string endpoint = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);
var response = await client.PostAsync(endpoint, null);
var token = await response.Content.ReadAsStringAsync();
return token;
}
}
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}