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

app.js 3.2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  1. // This loads the environment variables from the .env file
  2. require('dotenv-extended').load();
  3. var builder = require('botbuilder');
  4. var restify = require('restify');
  5. var util = require('util');
  6. var zummerStrings = require('./zummer-strings.js');
  7. var bingSearchService = require('./bing-search-service.js');
  8. var urlObj = require('url');
  9. // Setup Restify Server
  10. var server = restify.createServer();
  11. server.listen(process.env.port || process.env.PORT || 3978, () => {
  12. console.log('%s listening to %s', server.name, server.url);
  13. });
  14. // Create chat bot
  15. var connector = new builder.ChatConnector({
  16. appId: process.env.MICROSOFT_APP_ID,
  17. appPassword: process.env.MICROSOFT_APP_PASSWORD
  18. });
  19. var bot = new builder.UniversalBot(connector);
  20. var serverName;
  21. server.post('/api/messages', (req, res) => {
  22. var listenerFunc = connector.listen();
  23. serverName = req.headers.host;
  24. listenerFunc(req, res);
  25. });
  26. // You can provide your own model by specifing the 'LUIS_MODEL_URL' environment variable
  27. // This Url can be obtained by uploading or creating your model from the LUIS portal: https://www.luis.ai/
  28. const LuisModelUrl = process.env.LUIS_MODEL_URL ||
  29. 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/b550e80a-74ec-4bb4-bcbc-fe35f5b1fce4?subscription-key=a6d628faa2404cd799f2a291245eb135';
  30. // Main dialog with LUIS
  31. var recognizer = new builder.LuisRecognizer(LuisModelUrl);
  32. var intents = new builder.IntentDialog({ recognizers: [recognizer] })
  33. .matches('Greeting', [
  34. (session) => {
  35. session.send(zummerStrings.GreetOnDemand).endDialog();
  36. }
  37. ])
  38. .matches('Search', [
  39. (session, args) => {
  40. var entityRecognized;
  41. var query;
  42. if ((entityRecognized = builder.EntityRecognizer.findEntity(args.entities, 'ArticleTopic'))) {
  43. query = entityRecognized.entity;
  44. } else {
  45. query = session.message.text;
  46. }
  47. bingSearchService.findArticles(query).then((bingSearch) => {
  48. session.send(zummerStrings.SearchTopicTypeMessage);
  49. var zummerResult = prepareZummerResult(query, bingSearch.webPages.value[0]);
  50. var summaryText = util.format("### [%s](%s)\n%s\n\n", zummerResult.title, zummerResult.url, zummerResult.snippet);
  51. summaryText += util.format("*%s*", util.format(zummerStrings.PoweredBy, util.format("[Bing™](https://www.bing.com/search/?q=%s site:wikipedia.org)", zummerResult.query)));
  52. session.send(summaryText).endDialog();
  53. });
  54. }
  55. ])
  56. .onDefault((session) => {
  57. session.send(zummerStrings.FallbackIntentMessage).endDialog();
  58. });
  59. bot.dialog('/', intents);
  60. function prepareZummerResult(query, bingSearchResult) {
  61. var myUrl = urlObj.parse(bingSearchResult.url, true);
  62. var zummerResult = {};
  63. if (myUrl.host == "www.bing.com" && myUrl.pathname == "/cr") {
  64. zummerResult.url = myUrl.query["r"];
  65. } else {
  66. zummerResult.url = bingSearchResult.url;
  67. }
  68. zummerResult.title = bingSearchResult.name;
  69. zummerResult.query = query;
  70. zummerResult.snippet = bingSearchResult.snippet;
  71. return zummerResult;
  72. }
Tip!

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

Comments

Loading...