forked from manolovni/TuzobotVisualStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainDialog.cs
89 lines (83 loc) · 3.66 KB
/
MainDialog.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
using System;
using Microsoft.Bot.Builder.Dialogs;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using System.Net.Http;
using Microsoft.ProjectOxford.Emotion.Contract;
using System.Linq;
namespace Tuzobot
{
[Serializable]
public class MainDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<Message> argument)
{
var message = await argument;
if (message.Attachments.Count > 0)
{
CV cv = new CV();
TuzobotModelContainer db = new TuzobotModelContainer();
foreach (var a in message.Attachments)
{
Emotion[] emotions = await cv.Detect(a.ContentUrl);
if (emotions != null && emotions.Length > 0)
{
var happyDelta = ((double)emotions[0].Scores.Happiness - 0.5);
var surpriseDelta = ((double)emotions[0].Scores.Surprise - 0.5);
Contest contest = db.ContestSet.SingleOrDefault(c => c.Active);
if (contest == null) contest = db.ContestSet.SingleOrDefault(c => c.Id == 2);
Conv conv = db.ConvSet.SingleOrDefault(c => c.ConversationId == message.ConversationId);
var newSubmit = new Submit();
newSubmit.UserName = message.From.Name;
newSubmit.IsNotAdult = true;
newSubmit.IsWinner = false;
newSubmit.Conv = conv;
newSubmit.Image = message.Attachments[0].ContentUrl;
newSubmit.Contest = contest;
newSubmit.Score = happyDelta * happyDelta + surpriseDelta * surpriseDelta;
db.SubmitSet.Add(newSubmit);
db.SaveChanges();
await context.PostAsync($"Счастье {(100*emotions[0].Scores.Happiness).ToString("N2")}%");
await context.PostAsync($"Удивление {(100 * emotions[0].Scores.Surprise).ToString("N2")}%");
await context.PostAsync($"Испуг {(100 * emotions[0].Scores.Fear).ToString("N2")}%");
}
else
{
await context.PostAsync($"К сожаленю, не смог распознать эмоцию, попробуй еще 😊");
}
}
}
if (message.Text == "reset")
{
PromptDialog.Confirm(
context,
AfterResetAsync,
"Are you sure you want to reset the count?",
"Didn't get that!",
promptStyle: PromptStyle.None);
}
else
{
await context.PostAsync("Я умею распознавать эмоции на фотографиях, пришли мне слефи 😘");
context.Wait(MessageReceivedAsync);
}
}
public async Task AfterResetAsync(IDialogContext context, IAwaitable<bool> argument)
{
var confirm = await argument;
if (confirm)
{
await context.PostAsync("Reset count.");
}
else
{
await context.PostAsync("Did not reset count.");
}
context.Wait(MessageReceivedAsync);
}
}
}