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 5.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  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 Store = require('./store');
  6. var spellService = require('./spell-service');
  7. // Setup Restify Server
  8. var server = restify.createServer();
  9. server.listen(process.env.port || process.env.PORT || 3978, function () {
  10. console.log('%s listening to %s', server.name, server.url);
  11. });
  12. // Create connector and listen for messages
  13. var connector = new builder.ChatConnector({
  14. appId: process.env.MICROSOFT_APP_ID,
  15. appPassword: process.env.MICROSOFT_APP_PASSWORD
  16. });
  17. server.post('/api/messages', connector.listen());
  18. var bot = new builder.UniversalBot(connector, function (session) {
  19. session.send('Sorry, I did not understand \'%s\'. Type \'help\' if you need assistance.', session.message.text);
  20. });
  21. // You can provide your own model by specifing the 'LUIS_MODEL_URL' environment variable
  22. // This Url can be obtained by uploading or creating your model from the LUIS portal: https://www.luis.ai/
  23. var recognizer = new builder.LuisRecognizer(process.env.LUIS_MODEL_URL);
  24. bot.recognizer(recognizer);
  25. bot.dialog('SearchHotels', [
  26. function (session, args, next) {
  27. session.send('Welcome to the Hotels finder! We are analyzing your message: \'%s\'', session.message.text);
  28. // try extracting entities
  29. var cityEntity = builder.EntityRecognizer.findEntity(args.intent.entities, 'builtin.geography.city');
  30. var airportEntity = builder.EntityRecognizer.findEntity(args.intent.entities, 'AirportCode');
  31. if (cityEntity) {
  32. // city entity detected, continue to next step
  33. session.dialogData.searchType = 'city';
  34. next({ response: cityEntity.entity });
  35. } else if (airportEntity) {
  36. // airport entity detected, continue to next step
  37. session.dialogData.searchType = 'airport';
  38. next({ response: airportEntity.entity });
  39. } else {
  40. // no entities detected, ask user for a destination
  41. builder.Prompts.text(session, 'Please enter your destination');
  42. }
  43. },
  44. function (session, results) {
  45. var destination = results.response;
  46. var message = 'Looking for hotels';
  47. if (session.dialogData.searchType === 'airport') {
  48. message += ' near %s airport...';
  49. } else {
  50. message += ' in %s...';
  51. }
  52. session.send(message, destination);
  53. // Async search
  54. Store
  55. .searchHotels(destination)
  56. .then(function (hotels) {
  57. // args
  58. session.send('I found %d hotels:', hotels.length);
  59. var message = new builder.Message()
  60. .attachmentLayout(builder.AttachmentLayout.carousel)
  61. .attachments(hotels.map(hotelAsAttachment));
  62. session.send(message);
  63. // End
  64. session.endDialog();
  65. });
  66. }
  67. ]).triggerAction({
  68. matches: 'SearchHotels',
  69. onInterrupted: function (session) {
  70. session.send('Please provide a destination');
  71. }
  72. });
  73. bot.dialog('ShowHotelsReviews', function (session, args) {
  74. // retrieve hotel name from matched entities
  75. var hotelEntity = builder.EntityRecognizer.findEntity(args.intent.entities, 'Hotel');
  76. if (hotelEntity) {
  77. session.send('Looking for reviews of \'%s\'...', hotelEntity.entity);
  78. Store.searchHotelReviews(hotelEntity.entity)
  79. .then(function (reviews) {
  80. var message = new builder.Message()
  81. .attachmentLayout(builder.AttachmentLayout.carousel)
  82. .attachments(reviews.map(reviewAsAttachment));
  83. session.endDialog(message);
  84. });
  85. }
  86. }).triggerAction({
  87. matches: 'ShowHotelsReviews'
  88. });
  89. bot.dialog('Help', function (session) {
  90. session.endDialog('Hi! Try asking me things like \'search hotels in Seattle\', \'search hotels near LAX airport\' or \'show me the reviews of The Bot Resort\'');
  91. }).triggerAction({
  92. matches: 'Help'
  93. });
  94. // Spell Check
  95. if (process.env.IS_SPELL_CORRECTION_ENABLED === 'true') {
  96. bot.use({
  97. botbuilder: function (session, next) {
  98. spellService
  99. .getCorrectedText(session.message.text)
  100. .then(function (text) {
  101. session.message.text = text;
  102. next();
  103. })
  104. .catch(function (error) {
  105. console.error(error);
  106. next();
  107. });
  108. }
  109. });
  110. }
  111. // Helpers
  112. function hotelAsAttachment(hotel) {
  113. return new builder.HeroCard()
  114. .title(hotel.name)
  115. .subtitle('%d stars. %d reviews. From $%d per night.', hotel.rating, hotel.numberOfReviews, hotel.priceStarting)
  116. .images([new builder.CardImage().url(hotel.image)])
  117. .buttons([
  118. new builder.CardAction()
  119. .title('More details')
  120. .type('openUrl')
  121. .value('https://www.bing.com/search?q=hotels+in+' + encodeURIComponent(hotel.location))
  122. ]);
  123. }
  124. function reviewAsAttachment(review) {
  125. return new builder.ThumbnailCard()
  126. .title(review.title)
  127. .text(review.text)
  128. .images([new builder.CardImage().url(review.image)]);
  129. }
Tip!

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

Comments

Loading...