Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

SearchIntentHandler.cs 2.5 KB

You have to be logged in to leave a comment. Sign In
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
  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. namespace Zummer.Handlers
  14. {
  15. internal sealed class SearchIntentHandler : IIntentHandler
  16. {
  17. private readonly ISearchService bingSearchService;
  18. private readonly IBotToUser botToUser;
  19. public SearchIntentHandler(IBotToUser botToUser, ISearchService bingSearchService)
  20. {
  21. SetField.NotNull(out this.bingSearchService, nameof(bingSearchService), bingSearchService);
  22. SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
  23. }
  24. public async Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result)
  25. {
  26. EntityRecommendation entityRecommendation;
  27. var query = result.TryFindEntity(ZummerStrings.ArticlesEntityTopic, out entityRecommendation)
  28. ? entityRecommendation.Entity
  29. : result.Query;
  30. await this.botToUser.PostAsync(string.Format(Strings.SearchTopicTypeMessage));
  31. var bingSearch = await this.bingSearchService.FindArticles(query);
  32. var zummerResult = this.PrepareZummerResult(query, bingSearch.webPages.value[0]);
  33. var summaryText = $"### [{zummerResult.Tile}]({zummerResult.Url})\n{zummerResult.Snippet}\n\n" ;
  34. summaryText +=
  35. $"*{string.Format(Strings.PowerBy, $"[Bing™](https://www.bing.com/search/?q={zummerResult.Query} site:wikipedia.org)")}*";
  36. await this.botToUser.PostAsync(summaryText);
  37. }
  38. private ZummerSearchResult PrepareZummerResult(string query, Value page)
  39. {
  40. string url;
  41. var myUri = new Uri(page.url);
  42. if (myUri.Host == "www.bing.com" && myUri.AbsolutePath == "/cr")
  43. {
  44. url = HttpUtility.ParseQueryString(myUri.Query).Get("r");
  45. }
  46. else
  47. {
  48. url = page.url;
  49. }
  50. var zummerResult = new ZummerSearchResult
  51. {
  52. Url = url,
  53. Query = query,
  54. Tile = page.name,
  55. Snippet = page.snippet
  56. };
  57. return zummerResult;
  58. }
  59. }
  60. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...