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

settings.js 6.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  1. var util = require('util');
  2. var builder = require('botbuilder');
  3. var validators = require('../validators');
  4. var utils = require('../utils');
  5. var addressLibrary = require('./address');
  6. var SettingChoice = {
  7. Email: 'edit_email',
  8. Phone: 'edit_phone',
  9. Addresses: 'edit_addresses',
  10. Cancel: 'cancel'
  11. };
  12. var lib = new builder.Library('settings');
  13. lib.dialog('/', [
  14. // Display options
  15. function (session) {
  16. builder.Prompts.choice(session, 'settings_intro', [
  17. session.gettext(SettingChoice.Email),
  18. session.gettext(SettingChoice.Phone),
  19. session.gettext(SettingChoice.Addresses),
  20. session.gettext(SettingChoice.Cancel)
  21. ]);
  22. },
  23. // Trigger option edit
  24. function (session, args, next) {
  25. args = args || {};
  26. var response = args.response || {};
  27. var option = response.entity;
  28. var promptMessage;
  29. switch (option) {
  30. case session.gettext(SettingChoice.Email):
  31. promptMessage = 'type_email_or_return';
  32. if (session.userData.sender && session.userData.sender.email) {
  33. promptMessage = session.gettext('your_current_email', session.userData.sender.email);
  34. }
  35. session.send(promptMessage);
  36. return session.beginDialog('email');
  37. case session.gettext(SettingChoice.Phone):
  38. promptMessage = 'type_phone_or_return';
  39. if (session.userData.sender && session.userData.sender.phoneNumber) {
  40. promptMessage = session.gettext('your_current_phone', session.userData.sender.phoneNumber);
  41. }
  42. session.send(promptMessage);
  43. return session.beginDialog('phone');
  44. case session.gettext(SettingChoice.Addresses):
  45. return session.beginDialog('addresses');
  46. case session.gettext(SettingChoice.Cancel):
  47. return session.endDialog();
  48. }
  49. },
  50. // Setting updated/cancelled
  51. function (session, args) {
  52. args = args || {};
  53. var text = args.updated ? 'setting_updated' : 'setting_not_updated';
  54. session.send(text);
  55. session.replaceDialog('/');
  56. }
  57. ]).reloadAction('restart', null, { matches: /^back|b/i }); // restart menu options when 'B' or 'Back' is received
  58. // Email edit
  59. lib.dialog('email', editOptionDialog(
  60. function (input) { return validators.EmailRegex.test(input); },
  61. 'invalid_email_address',
  62. function (session, email) { saveSenderSetting(session, 'email', email); }));
  63. // Phone Number edit
  64. lib.dialog('phone', editOptionDialog(
  65. function (input) { return validators.PhoneRegex.test(input); },
  66. 'invalid_phone_number',
  67. function (session, phone) { saveSenderSetting(session, 'phoneNumber', phone); }));
  68. // Addresses
  69. var UseSavedInfoChoices = addressLibrary.UseSavedInfoChoices;
  70. lib.dialog('addresses', [
  71. function (session, args, next) {
  72. // Check if an option was selected
  73. var selection = session.message.text;
  74. if (selection.toLowerCase() === session.gettext(UseSavedInfoChoices.Home).toLowerCase() ||
  75. selection.toLowerCase() === session.gettext(UseSavedInfoChoices.Work).toLowerCase()) {
  76. session.dialogData.selection = selection;
  77. return next();
  78. }
  79. // Show saved addresses
  80. session.send('choose_address_to_update');
  81. var saved = session.userData.billingAddresses = session.userData.billingAddresses || {};
  82. var message = new builder.Message(session)
  83. .attachmentLayout(builder.AttachmentLayout.carousel);
  84. var homeAddress = saved[session.gettext(UseSavedInfoChoices.Home)];
  85. var workAddress = saved[session.gettext(UseSavedInfoChoices.Work)];
  86. var notSet = session.gettext('not_set');
  87. message.addAttachment(createAddressCard(session, session.gettext(UseSavedInfoChoices.Home), homeAddress || notSet));
  88. message.addAttachment(createAddressCard(session, session.gettext(UseSavedInfoChoices.Work), workAddress || notSet));
  89. message.addAttachment(new builder.HeroCard(session)
  90. .title('not_this_time')
  91. .subtitle('do_not_change_addresses')
  92. .buttons([
  93. builder.CardAction.imBack(session, session.gettext('back'), session.gettext('back_label'))
  94. ]));
  95. session.send(message);
  96. },
  97. function (session, args, next) {
  98. // Trigger address request dialog
  99. session.beginDialog('address:/', {
  100. promptMessage: session.gettext('specify_new_address', session.dialogData.selection)
  101. });
  102. },
  103. function (session, args, next) {
  104. // Save new address
  105. var selection = session.dialogData.selection;
  106. var newAddress = args.address;
  107. session.userData.billingAddresses = session.userData.billingAddresses || {};
  108. session.userData.billingAddresses[selection] = newAddress;
  109. session.endDialogWithResult({ updated: true });
  110. }
  111. ]);
  112. function saveSenderSetting(session, name, value) {
  113. session.userData.sender = session.userData.sender || {};
  114. session.userData.sender[name] = value;
  115. }
  116. function editOptionDialog(validationFunc, invalidMessage, saveFunc) {
  117. return new builder.SimpleDialog(function (session, args, next) {
  118. // check dialog was just forwarded
  119. if (!session.dialogData.loop) {
  120. session.dialogData.loop = true;
  121. session.sendBatch();
  122. return;
  123. }
  124. if (!validationFunc(session.message.text)) {
  125. // invalid
  126. session.send(invalidMessage);
  127. } else {
  128. // save
  129. saveFunc(session, session.message.text);
  130. session.endDialogWithResult({ updated: true });
  131. }
  132. });
  133. }
  134. function createAddressCard(session, buttonTitle, address) {
  135. return new builder.HeroCard(session)
  136. .title(buttonTitle)
  137. .subtitle(address)
  138. .buttons([
  139. builder.CardAction.imBack(session, buttonTitle, buttonTitle)
  140. ]);
  141. }
  142. // Export createLibrary() function
  143. module.exports.createLibrary = function () {
  144. return lib.clone();
  145. };
Tip!

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

Comments

Loading...