forked from MissionMarsFourthHorizon/operation-max
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardUtil.cs
117 lines (106 loc) · 4.22 KB
/
CardUtil.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
namespace HelpDesk.Util
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using HelpDeskBot.Model;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using AdaptiveCards;
public static class CardUtil
{
public static async Task ShowSearchResults(IDialogContext context, SearchResult searchResult, string notResultsMessage)
{
Activity reply = ((Activity)context.Activity).CreateReply();
await CardUtil.ShowSearchResults(reply, searchResult, notResultsMessage);
}
public static async Task ShowSearchResults(Activity reply, SearchResult searchResult, string notResultsMessage)
{
if (searchResult.Value.Length != 0)
{
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
foreach (SearchResultHit item in searchResult.Value)
{
reply.Attachments.Add(
new Attachment
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = CreateSearchResultCard(item.Title, item.Category, item.SearchScore.ToString(), item.Text)
}
);
}
ConnectorClient connector = new ConnectorClient(new Uri(reply.ServiceUrl));
await connector.Conversations.SendToConversationAsync(reply);
}
else
{
reply.Text = notResultsMessage;
ConnectorClient connector = new ConnectorClient(new Uri(reply.ServiceUrl));
await connector.Conversations.SendToConversationAsync(reply);
}
}
private static AdaptiveCard CreateSearchResultCard(string title, string category, string score, string text)
{
AdaptiveCard card = new AdaptiveCard();
var columnsBlock = new ColumnSet()
{
Separation = SeparationStyle.Strong,
Columns = new List<Column>
{
new Column
{
Size = "1",
Items = new List<CardElement>
{
new TextBlock
{
Text = title,
Size = TextSize.Large,
Weight = TextWeight.Bolder,
Wrap = true
},
new FactSet
{
Separation = SeparationStyle.None,
Facts = new List<AdaptiveCards.Fact>
{
new AdaptiveCards.Fact("Search Score:", score),
new AdaptiveCards.Fact("Category:", category),
}
}
}
},
new Column
{
Size = "auto",
Items = new List<CardElement>
{
new Image
{
Url = "https://bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-7.png",
Size = ImageSize.Medium
}
}
}
}
};
var textBlock = new TextBlock()
{
Text = text,
Wrap = true,
MaxLines = 2,
Size = TextSize.Normal
};
var submitAction = new SubmitAction
{
Title = "More Details",
Data = $"show me the article {title}"
};
// fill the card
card.Body.Add(columnsBlock);
card.Body.Add(textBlock);
card.Actions.Add(submitAction);
return card;
}
}
}