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

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

Comments

Loading...