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.1 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
  1. // This loads the environment variables from the .env file
  2. require('dotenv-extended').load();
  3. var builder = require('botbuilder');
  4. var azure = require('botbuilder-azure');
  5. var restify = require('restify');
  6. // Setup Restify Server
  7. var server = restify.createServer();
  8. server.listen(process.env.port || process.env.PORT || 3978, function () {
  9. console.log('%s listening to %s', server.name, server.url);
  10. });
  11. // Create connector and listen for messages
  12. var connector = new builder.ChatConnector({
  13. appId: process.env.MICROSOFT_APP_ID,
  14. appPassword: process.env.MICROSOFT_APP_PASSWORD
  15. });
  16. server.post('/api/messages', connector.listen());
  17. var HelpMessage = '\n * If you want to know which city I\'m using for my searches type \'current city\'. \n * Want to change the current city? Type \'change city to cityName\'. \n * Want to change it just for your searches? Type \'change my city to cityName\'';
  18. var UserNameKey = 'UserName';
  19. var UserWelcomedKey = 'UserWelcomed';
  20. var CityKey = 'City';
  21. // Setup bot with default dialog
  22. var bot = new builder.UniversalBot(connector, function (session) {
  23. // initialize with default city
  24. if (!session.conversationData[CityKey]) {
  25. session.conversationData[CityKey] = 'Seattle';
  26. session.send('Welcome to the Search City bot. I\'m currently configured to search for things in %s', session.conversationData[CityKey]);
  27. }
  28. // is user's name set?
  29. var userName = session.userData[UserNameKey];
  30. if (!userName) {
  31. return session.beginDialog('greet');
  32. }
  33. // has the user been welcomed to the conversation?
  34. if (!session.privateConversationData[UserWelcomedKey]) {
  35. session.privateConversationData[UserWelcomedKey] = true;
  36. return session.send('Welcome back %s! Remember the rules: %s', userName, HelpMessage);
  37. }
  38. session.beginDialog('search');
  39. });
  40. // Azure DocumentDb State Store
  41. var docDbClient = new azure.DocumentDbClient({
  42. host: process.env.DOCUMENT_DB_HOST,
  43. masterKey: process.env.DOCUMENT_DB_MASTER_KEY,
  44. database: process.env.DOCUMENT_DB_DATABASE,
  45. collection: process.env.DOCUMENT_DB_COLLECTION
  46. });
  47. var botStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);
  48. // Set Custom Store
  49. bot.set('storage', botStorage);
  50. // Enable Conversation Data persistence
  51. bot.set('persistConversationData', true);
  52. // search dialog
  53. bot.dialog('search', function (session, args, next) {
  54. // perform search
  55. var city = session.privateConversationData[CityKey] || session.conversationData[CityKey];
  56. var userName = session.userData[UserNameKey];
  57. var messageText = session.message.text.trim();
  58. session.send('%s, wait a few seconds. Searching for \'%s\' in \'%s\'...', userName, messageText, city);
  59. session.send('https://www.bing.com/search?q=%s', encodeURIComponent(messageText + ' in ' + city));
  60. session.endDialog();
  61. });
  62. // reset bot dialog
  63. bot.dialog('reset', function (session) {
  64. // reset data
  65. delete session.userData[UserNameKey];
  66. delete session.conversationData[CityKey];
  67. delete session.privateConversationData[CityKey];
  68. delete session.privateConversationData[UserWelcomedKey];
  69. session.endDialog('Ups... I\'m suffering from a memory loss...');
  70. }).triggerAction({ matches: /^reset/i });
  71. // print current city dialog
  72. bot.dialog('printCurrentCity', function (session) {
  73. // print city settings
  74. var userName = session.userData[UserNameKey];
  75. var defaultCity = session.conversationData[CityKey];
  76. var userCity = session.privateConversationData[CityKey];
  77. if (userCity) {
  78. session.endDialog(
  79. '%s, you have overridden the city. Your searches are for things in %s. The default conversation city is %s.',
  80. userName, userCity, defaultCity);
  81. } else {
  82. session.endDialog('Hey %s, I\'m currently configured to search for things in %s.', userName, defaultCity);
  83. }
  84. }).triggerAction({ matches: /^current city/i });
  85. // change current city dialog
  86. bot.dialog('changeCurrentCity', function (session, args) {
  87. // change default city
  88. var newCity = args.intent.matched[1].trim();
  89. session.conversationData[CityKey] = newCity;
  90. var userName = session.userData[UserNameKey];
  91. session.endDialog('All set %s. From now on, all my searches will be for things in %s.', userName, newCity);
  92. }).triggerAction({ matches: /^change city to (.*)/i });
  93. // change my current city dialog
  94. bot.dialog('changeMyCurrentCity', function (session, args) {
  95. // change user's city
  96. var newCity = args.intent.matched[1].trim();
  97. session.privateConversationData[CityKey] = newCity;
  98. var userName = session.userData[UserNameKey];
  99. session.endDialog('All set %s. I have overridden the city to %s just for you', userName, newCity);
  100. }).triggerAction({ matches: /^change my city to (.*)/i });
  101. // Greet dialog
  102. bot.dialog('greet', new builder.SimpleDialog(function (session, results) {
  103. if (results && results.response) {
  104. session.userData[UserNameKey] = results.response;
  105. session.privateConversationData[UserWelcomedKey] = true;
  106. return session.endDialog('Welcome %s! %s', results.response, HelpMessage);
  107. }
  108. builder.Prompts.text(session, 'Before get started, please tell me your name?');
  109. }));
Tip!

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

Comments

Loading...