complex.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <!DOCTYPE html>
  2. <head>
  3. <title>ChatUI</title>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="../build/chatUI.css">
  6. <meta name="viewport" content="width=device-width">
  7. <style>
  8. html, body{
  9. margin:0;
  10. padding:0;
  11. border:0;
  12. }
  13. #chatbot{
  14. width:100%;
  15. height: 100vh;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="chatbot">
  21. </div>
  22. <script src="https://d3js.org/d3.v4.min.js"></script>
  23. <script src="../build/index-d3.min.js"></script>
  24. <script type="text/javascript">
  25. var chat = chatUI(d3.select('#chatbot'));
  26. var conversation = {},
  27. userName = null;
  28. conversation.init = function(){
  29. chat.addBubble({ type: 'text', value: 'Hi there!', class: 'bot', delay: 1000 }, function () {
  30. //After initial bubble is presented, ask the user for her name
  31. chat.addBubble({ type: 'text', value: 'What is your name?', class: 'bot', delay: 0 });
  32. //Show the input container
  33. chat.showInput(conversation.nameResponse);
  34. });
  35. };
  36. conversation.nameResponse = function(name){
  37. userName = name;
  38. //As input arrives, present the input
  39. chat.addBubble({ type: 'text', value: userName, class: 'human', delay: 0 });
  40. //Hide the input container
  41. chat.hideInput();
  42. //And welcome the user with her name
  43. chat.addBubble({ type: 'text', value: 'Hello '+userName, class: 'bot', delay: 500 }, function(){
  44. chat.addBubble({ type: 'text', value: 'Do you want to know my name?', class: 'bot', delay:500 }, function(){
  45. chat.addBubble({ type: 'select', value:[{label:'Yes'}, {label:'No'}], class: 'human', delay: 0 }, conversation.questResponse);
  46. });
  47. });
  48. };
  49. conversation.questResponse = function(mood){
  50. switch(mood.label){
  51. case 'Yes':
  52. chat.addBubble({ type: 'text', value: 'My name is BOT.', class: 'bot', delay: 500 });
  53. break;
  54. case 'No':
  55. chat.addBubble({ type: 'text', value: 'OK :(', class: 'bot', delay: 500 });
  56. break;
  57. }
  58. };
  59. conversation.init();
  60. </script>
  61. </body>
  62. </html>