Skip to content

Commit a6b59f7

Browse files
author
MIDDLEEAST\mmoussa
committed
Refactoring C# and Node code to Zummer
1 parent 477c1ad commit a6b59f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+843
-1244
lines changed
-6.95 KB
Binary file not shown.
Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
2-
using System.Text.RegularExpressions;
32
using System.Threading.Tasks;
43
using Microsoft.Bot.Builder.Dialogs;
54
using Microsoft.Bot.Builder.Internals.Fibers;
65
using Microsoft.Bot.Builder.Luis;
76
using Microsoft.Bot.Builder.Luis.Models;
8-
using Microsoft.Bot.Builder.Scorables;
97
using Microsoft.Bot.Connector;
108
using Zummer.Handlers;
119

@@ -15,69 +13,36 @@ namespace Zummer.Dialogs
1513
/// The top-level natural language dialog for sample.
1614
/// </summary>
1715
[Serializable]
18-
[LuisModel("", "")]
19-
internal sealed class MainDialog : DispatchDialog
16+
internal sealed class MainDialog : LuisDialog<object>
2017
{
2118
private readonly IHandlerFactory handlerFactory;
22-
private readonly ILuisService luis;
23-
19+
2420
public MainDialog(ILuisService luis, IHandlerFactory handlerFactory)
21+
: base(luis)
2522
{
2623
SetField.NotNull(out this.handlerFactory, nameof(handlerFactory), handlerFactory);
27-
SetField.NotNull(out this.luis, nameof(luis), luis);
28-
}
29-
30-
/// <summary>
31-
/// This method process the summarization requests from the client by matching regex pattern on message text
32-
/// this request is sent when the user clicks on "Read Summary" button. A messages is submitted with value
33-
/// contains summary keyword at the beginning and url of article to summarize.
34-
/// Note: HeroCard is created in <see cref="Utilities.CardGenerators.ArticleCardGenerator"/>
35-
/// </summary>
36-
/// <param name="context">Dialog context</param>
37-
/// <param name="activity">Message activity containing the message information such as text</param>
38-
/// <param name="result">The regex Match with the Regex pattern</param>
39-
/// <returns>Async Task</returns>
40-
[RegexPattern("(^summary )(.*)")]
41-
[ScorableGroup(0)]
42-
public async Task SummarizationRegexHandlerAsync(IDialogContext context, IMessageActivity activity, Match result)
43-
{
44-
await this.handlerFactory.CreateRegexHandler(ZummerStrings.SummarizeActionName).Respond(activity, result);
45-
context.Wait(this.ActivityReceivedAsync);
4624
}
4725

4826
[LuisIntent(ZummerStrings.GreetingIntentName)]
49-
[ScorableGroup(1)]
50-
public async Task GreetingIntentHandlerAsync(IDialogContext context, IMessageActivity activity, LuisResult result)
27+
public async Task GreetingIntentHandlerAsync(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
5128
{
5229
await this.handlerFactory.CreateIntentHandler(ZummerStrings.GreetingIntentName).Respond(activity, result);
53-
context.Wait(this.ActivityReceivedAsync);
30+
context.Wait(this.MessageReceived);
5431
}
5532

56-
[LuisIntent(ZummerStrings.ArticlesIntentName)]
57-
[ScorableGroup(1)]
58-
public async Task FindArticlesIntentHandlerAsync(IDialogContext context, IMessageActivity activity, LuisResult result)
33+
[LuisIntent(ZummerStrings.SearchIntentName)]
34+
public async Task FindArticlesIntentHandlerAsync(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
5935
{
60-
await this.handlerFactory.CreateIntentHandler(ZummerStrings.ArticlesIntentName).Respond(activity, result);
61-
context.Wait(this.ActivityReceivedAsync);
36+
await this.handlerFactory.CreateIntentHandler(ZummerStrings.SearchIntentName).Respond(activity, result);
37+
context.Wait(this.MessageReceived);
6238
}
6339

6440
[LuisIntent("")]
6541
[LuisIntent("None")]
66-
[ScorableGroup(1)]
67-
public async Task FallbackIntentHandlerAsync(IDialogContext context, IMessageActivity activity)
42+
public async Task FallbackIntentHandlerAsync(IDialogContext context, LuisResult result)
6843
{
6944
await context.PostAsync(string.Format(Strings.FallbackIntentMessage));
70-
context.Wait(this.ActivityReceivedAsync);
71-
}
72-
73-
protected override ILuisService MakeService(ILuisModel model)
74-
{
75-
return this.luis;
76-
}
77-
78-
protected override Regex MakeRegex(string pattern)
79-
{
80-
return new Regex(pattern, RegexOptions.IgnoreCase);
45+
context.Wait(this.MessageReceived);
8146
}
8247
}
8348
}

CSharp/intelligence-Zummer/Handlers/ArticlesIntentHandler.cs

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
2+
using Microsoft.Bot.Builder.Dialogs;
53
using Microsoft.Bot.Builder.Dialogs.Internals;
64
using Microsoft.Bot.Builder.Internals.Fibers;
75
using Microsoft.Bot.Builder.Luis.Models;
86
using Microsoft.Bot.Connector;
9-
using Zummer.Utilities.CardGenerators;
107

118
namespace Zummer.Handlers
129
{
@@ -19,18 +16,9 @@ public GreetingIntentHandler(IBotToUser botToUser)
1916
SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
2017
}
2118

22-
public async Task Respond(IMessageActivity activity, LuisResult result)
19+
public async Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result)
2320
{
24-
var reply = this.botToUser.MakeMessage();
25-
26-
reply.Attachments.Add(CardGenerator.GetHeroCard(text: string.Format(Strings.GreetOnDemand)));
27-
reply.Attachments.Add(CardGenerator.GetThumbNailCard(
28-
cardActions: new List<CardAction>
29-
{
30-
new CardAction(ActionTypes.OpenUrl, Strings.BingForMore, value: "https://www.bing.com")
31-
}));
32-
33-
await this.botToUser.PostAsync(reply);
21+
await this.botToUser.PostAsync(Strings.GreetOnDemand);
3422
}
3523
}
3624
}

CSharp/intelligence-Zummer/Handlers/HandlerFactory.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,5 @@ public IIntentHandler CreateIntentHandler(string key)
1616
{
1717
return this.scope.ResolveNamed<IIntentHandler>(key);
1818
}
19-
20-
public IRegexHandler CreateRegexHandler(string key)
21-
{
22-
return this.scope.ResolveNamed<IRegexHandler>(key);
23-
}
2419
}
2520
}

CSharp/intelligence-Zummer/Handlers/IHandlerFactory.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
public interface IHandlerFactory
44
{
55
IIntentHandler CreateIntentHandler(string key);
6-
7-
IRegexHandler CreateRegexHandler(string key);
86
}
97
}

CSharp/intelligence-Zummer/Handlers/IIntentHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace Zummer.Handlers
77
{
88
public interface IIntentHandler
99
{
10-
Task Respond(IMessageActivity activity, LuisResult result);
10+
Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result);
1111
}
1212
}

CSharp/intelligence-Zummer/Handlers/IRegexHandler.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using System.Web;
5+
using Microsoft.Bot.Builder.Dialogs;
6+
using Microsoft.Bot.Builder.Dialogs.Internals;
7+
using Microsoft.Bot.Builder.Internals.Fibers;
8+
using Microsoft.Bot.Builder.Luis;
9+
using Microsoft.Bot.Builder.Luis.Models;
10+
using Microsoft.Bot.Connector;
11+
using Zummer.Models.Search;
12+
using Zummer.Services;
13+
14+
namespace Zummer.Handlers
15+
{
16+
internal sealed class SearchIntentHandler : IIntentHandler
17+
{
18+
private readonly ISearchService bingSearchService;
19+
private readonly ISummarizeService bingSummarizeService;
20+
private readonly IBotToUser botToUser;
21+
22+
public SearchIntentHandler(IBotToUser botToUser, ISearchService bingSearchService, ISummarizeService bingSummarizeService)
23+
{
24+
SetField.NotNull(out this.bingSearchService, nameof(bingSearchService), bingSearchService);
25+
SetField.NotNull(out this.bingSummarizeService, nameof(bingSummarizeService), bingSummarizeService);
26+
SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
27+
}
28+
29+
public async Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result)
30+
{
31+
EntityRecommendation entityRecommendation;
32+
33+
var query = result.TryFindEntity(ZummerStrings.ArticlesEntityTopic, out entityRecommendation)
34+
? entityRecommendation.Entity
35+
: result.Query;
36+
37+
await this.botToUser.PostAsync(string.Format(Strings.SearchTopicTypeMessage));
38+
39+
var bingSearch = await this.bingSearchService.FindArticles(query);
40+
41+
var zummerResult = this.PrepareZummerResult(query, bingSearch.webPages.value[0]);
42+
43+
var bingSummary = await this.bingSummarizeService.GetSummary(zummerResult.Url);
44+
45+
if (bingSummary?.Data != null && bingSummary.Data.Length != 0)
46+
{
47+
var summaryText = bingSummary.Data.Aggregate(
48+
$"### [{zummerResult.Tile}]({zummerResult.Url})"
49+
+ "\n" +
50+
$"**{Strings.SummaryString}**"
51+
+ "\n\n",
52+
(current, datum) => current + (datum.Text + "\n\n"));
53+
54+
summaryText +=
55+
$"*{string.Format(Strings.PowerBy, $"[Bing™](https://www.bing.com/search/?q={zummerResult.Query} site:wikipedia.org)")}*";
56+
57+
await this.botToUser.PostAsync(summaryText);
58+
}
59+
else
60+
{
61+
await this.botToUser.PostAsync(Strings.SummaryErrorMessage);
62+
}
63+
}
64+
65+
private ZummerSearchResult PrepareZummerResult(string query, Value page)
66+
{
67+
string url;
68+
var myUri = new Uri(page.url);
69+
70+
if (myUri.Host == "www.bing.com" && myUri.AbsolutePath == "/cr")
71+
{
72+
url = HttpUtility.ParseQueryString(myUri.Query).Get("r");
73+
}
74+
else
75+
{
76+
url = page.url;
77+
}
78+
79+
var zummerResult = new ZummerSearchResult
80+
{
81+
Url = url,
82+
Query = query,
83+
Tile = page.name
84+
};
85+
86+
return zummerResult;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)