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

index.js 4.3 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
  1. var builder = require('botbuilder');
  2. var siteUrl = require('./site-url');
  3. var connector = new builder.ChatConnector({
  4. appId: process.env.MICROSOFT_APP_ID,
  5. appPassword: process.env.MICROSOFT_APP_PASSWORD
  6. });
  7. // Welcome Dialog
  8. var MainOptions = {
  9. Shop: 'main_options_order_flowers',
  10. Support: 'main_options_talk_to_support'
  11. };
  12. var bot = new builder.UniversalBot(connector, function (session) {
  13. if (localizedRegex(session, [MainOptions.Shop]).test(session.message.text)) {
  14. // Order Flowers
  15. return session.beginDialog('shop:/');
  16. }
  17. var welcomeCard = new builder.HeroCard(session)
  18. .title('welcome_title')
  19. .subtitle('welcome_subtitle')
  20. .images([
  21. new builder.CardImage(session)
  22. .url('https://placeholdit.imgix.net/~text?txtsize=56&txt=Contoso%20Flowers&w=640&h=330')
  23. .alt('contoso_flowers')
  24. ])
  25. .buttons([
  26. builder.CardAction.imBack(session, session.gettext(MainOptions.Shop), MainOptions.Shop),
  27. builder.CardAction.imBack(session, session.gettext(MainOptions.Support), MainOptions.Support)
  28. ]);
  29. session.send(new builder.Message(session)
  30. .addAttachment(welcomeCard));
  31. });
  32. // Enable Conversation Data persistence
  33. bot.set('persistConversationData', true);
  34. // Set default locale
  35. bot.set('localizerSettings', {
  36. botLocalePath: './bot/locale',
  37. defaultLocale: 'en'
  38. });
  39. // Sub-Dialogs
  40. bot.library(require('./dialogs/shop').createLibrary());
  41. bot.library(require('./dialogs/address').createLibrary());
  42. bot.library(require('./dialogs/product-selection').createLibrary());
  43. bot.library(require('./dialogs/delivery').createLibrary());
  44. bot.library(require('./dialogs/details').createLibrary());
  45. bot.library(require('./dialogs/checkout').createLibrary());
  46. bot.library(require('./dialogs/settings').createLibrary());
  47. bot.library(require('./dialogs/help').createLibrary());
  48. // Validators
  49. bot.library(require('./validators').createLibrary());
  50. // Trigger secondary dialogs when 'settings' or 'support' is called
  51. bot.use({
  52. botbuilder: function (session, next) {
  53. var text = session.message.text;
  54. var settingsRegex = localizedRegex(session, ['main_options_settings']);
  55. var supportRegex = localizedRegex(session, ['main_options_talk_to_support', 'help']);
  56. if (settingsRegex.test(text)) {
  57. // interrupt and trigger 'settings' dialog
  58. return session.beginDialog('settings:/');
  59. } else if (supportRegex.test(text)) {
  60. // interrupt and trigger 'help' dialog
  61. return session.beginDialog('help:/');
  62. }
  63. // continue normal flow
  64. next();
  65. }
  66. });
  67. // Send welcome when conversation with bot is started, by initiating the root dialog
  68. bot.on('conversationUpdate', function (message) {
  69. if (message.membersAdded) {
  70. message.membersAdded.forEach(function (identity) {
  71. if (identity.id === message.address.bot.id) {
  72. bot.beginDialog(message.address, '/');
  73. }
  74. });
  75. }
  76. });
  77. // Cache of localized regex to match selection from main options
  78. var LocalizedRegexCache = {};
  79. function localizedRegex(session, localeKeys) {
  80. var locale = session.preferredLocale();
  81. var cacheKey = locale + ":" + localeKeys.join('|');
  82. if (LocalizedRegexCache.hasOwnProperty(cacheKey)) {
  83. return LocalizedRegexCache[cacheKey];
  84. }
  85. var localizedStrings = localeKeys.map(function (key) { return session.localizer.gettext(locale, key); });
  86. var regex = new RegExp('^(' + localizedStrings.join('|') + ')', 'i');
  87. LocalizedRegexCache[cacheKey] = regex;
  88. return regex;
  89. }
  90. // Connector listener wrapper to capture site url
  91. var connectorListener = connector.listen();
  92. function listen() {
  93. return function (req, res) {
  94. // Capture the url for the hosted application
  95. // We'll later need this url to create the checkout link
  96. var url = req.protocol + '://' + req.get('host');
  97. siteUrl.save(url);
  98. connectorListener(req, res);
  99. };
  100. }
  101. // Other wrapper functions
  102. function beginDialog(address, dialogId, dialogArgs) {
  103. bot.beginDialog(address, dialogId, dialogArgs);
  104. }
  105. function sendMessage(message) {
  106. bot.send(message);
  107. }
  108. module.exports = {
  109. listen: listen,
  110. beginDialog: beginDialog,
  111. sendMessage: sendMessage
  112. };
Tip!

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

Comments

Loading...