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 2.6 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
  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 chat bot 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 userStore = [];
  17. var bot = new builder.UniversalBot(connector, function (session) {
  18. // store user's address
  19. var address = session.message.address;
  20. userStore.push(address);
  21. // end current dialog
  22. session.endDialog('You\'ve been invited to a survey! It will start in a few seconds...');
  23. });
  24. // Every 5 seconds, check for new registered users and start a new dialog
  25. setInterval(function () {
  26. var newAddresses = userStore.splice(0);
  27. newAddresses.forEach(function (address) {
  28. console.log('Starting survey for address:', address);
  29. // new conversation address, copy without conversationId
  30. var newConversationAddress = Object.assign({}, address);
  31. delete newConversationAddress.conversation;
  32. // start survey dialog
  33. bot.beginDialog(newConversationAddress, 'survey', null, function (err) {
  34. if (err) {
  35. // error ocurred while starting new conversation. Channel not supported?
  36. bot.send(new builder.Message()
  37. .text('This channel does not support this operation: ' + err.message)
  38. .address(address));
  39. }
  40. });
  41. });
  42. }, 5000);
  43. bot.dialog('survey', [
  44. function (session) {
  45. builder.Prompts.text(session, 'Hello... What\'s your name?');
  46. },
  47. function (session, results) {
  48. session.userData.name = results.response;
  49. builder.Prompts.number(session, 'Hi ' + results.response + ', How many years have you been coding?');
  50. },
  51. function (session, results) {
  52. session.userData.coding = results.response;
  53. builder.Prompts.choice(session, 'What language do you code Node using? ', ['JavaScript', 'CoffeeScript', 'TypeScript']);
  54. },
  55. function (session, results) {
  56. session.userData.language = results.response.entity;
  57. session.endDialog('Got it... ' + session.userData.name +
  58. ' you\'ve been programming for ' + session.userData.coding +
  59. ' years and use ' + session.userData.language + '.');
  60. }
  61. ]);
Tip!

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

Comments

Loading...