Advertisement
Guest User

Untitled

a guest
May 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.03 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>Chat Bot</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
  7. <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" />
  8. <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  9.  
  10. <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  11. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  12.  
  13. <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
  14. <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  15.  
  16. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  17. <title>Insert title here</title>
  18. <style>
  19. .mytext {
  20. border: 0;
  21. padding: 10px;
  22. background: whitesmoke;
  23. }
  24.  
  25. .text {
  26. width: 75%;
  27. display: flex;
  28. flex-direction: column;
  29. }
  30.  
  31. .text > p:first-of-type {
  32. width: 100%;
  33. margin-top: 0;
  34. margin-bottom: auto;
  35. line-height: 13px;
  36. font-size: 12px;
  37. }
  38.  
  39. .text > p:last-of-type {
  40. width: 100%;
  41. text-align: right;
  42. color: silver;
  43. margin-bottom: -7px;
  44. margin-top: auto;
  45. }
  46.  
  47. .text-l {
  48. float: left;
  49. padding-right: 10px;
  50. }
  51.  
  52. .text-r {
  53. float: right;
  54. padding-left: 10px;
  55. }
  56.  
  57. .avatar {
  58. display: flex;
  59. justify-content: center;
  60. align-items: center;
  61. width: 25%;
  62. float: left;
  63. padding-right: 10px;
  64. }
  65.  
  66. .macro {
  67. margin-top: 5px;
  68. width: 85%;
  69. border-radius: 5px;
  70. padding: 5px;
  71. display: flex;
  72. }
  73.  
  74. .msj-rta {
  75. float: right;
  76. background: whitesmoke;
  77. margin-left: 10px;
  78. margin-bottom: 10px;
  79. }
  80.  
  81. .msj {
  82. float: left;
  83. background: white;
  84. }
  85.  
  86. .frame {
  87. background: #e0e0de;
  88. height: 450px;
  89. overflow: hidden;
  90. padding: 0;
  91. }
  92.  
  93. .frame > div:last-of-type {
  94. position: absolute;
  95. bottom: 0;
  96. width: 100%;
  97. display: flex;
  98. }
  99.  
  100. body > div > div > div:nth-child(2) > span {
  101. background: whitesmoke;
  102. padding: 10px;
  103. font-size: 21px;
  104. border-radius: 50%;
  105. }
  106.  
  107. body > div > div > div.msj-rta.macro {
  108. margin: auto;
  109. margin-left: 1%;
  110. }
  111.  
  112. ul {
  113. width: 97%;
  114. list-style-type: none;
  115. padding: 18px;
  116. position: absolute;
  117. bottom: 60px;
  118. display: flex;
  119. flex-direction: column;
  120. top: 0;
  121. overflow-y: scroll;
  122. }
  123.  
  124. .msj:before {
  125. width: 0;
  126. height: 0;
  127. content: "";
  128. top: -5px;
  129. left: -14px;
  130. position: relative;
  131. border-style: solid;
  132. border-width: 0 13px 13px 0;
  133. border-color: transparent #ffffff transparent transparent;
  134. }
  135.  
  136. .msj-rta:after {
  137. width: 0;
  138. height: 0;
  139. content: "";
  140. top: -5px;
  141. left: 14px;
  142. position: relative;
  143. border-style: solid;
  144. border-width: 13px 13px 0 0;
  145. border-color: whitesmoke transparent transparent transparent;
  146. }
  147.  
  148. input:focus {
  149. outline: none;
  150. }
  151.  
  152. ::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  153. color: #d4d4d4;
  154. }
  155.  
  156. ::-moz-placeholder { /* Firefox 19+ */
  157. color: #d4d4d4;
  158. }
  159.  
  160. :-ms-input-placeholder { /* IE 10+ */
  161. color: #d4d4d4;
  162. }
  163.  
  164. :-moz-placeholder { /* Firefox 18- */
  165. color: #d4d4d4;
  166. }
  167.  
  168. .navbar-inverse {
  169. background-color: #222222;
  170. border-color: #080808;
  171. display: none;
  172. }
  173.  
  174. footer {
  175. display: none;
  176. }
  177.  
  178. hr {
  179. display: none;
  180. }
  181.  
  182. #divChatBotName {
  183. color: white;
  184. font-weight: bold;
  185. vertical-align: sub;
  186. width: 100%;
  187. margin-top: 5px;
  188. margin-left: 15px;
  189. }
  190.  
  191. #divChatHeader {
  192. background-color: rgba(19, 26, 34, 0.6);
  193. height: 40px;
  194. border-radius: 5px 5px 0 0;
  195. display:flex;
  196. }
  197.  
  198. #chat {
  199. position: absolute;
  200. width: 550px;
  201. z-index: 1;
  202. float: right;
  203. }
  204. .spanMessage {
  205. cursor:pointer;
  206. }
  207. .spanLink{
  208. color: #428bca;
  209. cursor:pointer;
  210. }
  211. </style>
  212. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  213. </head>
  214. <script>
  215. var me = {};
  216.  
  217. var you = {};
  218.  
  219. var sessionID = "";
  220. var countMsg = 0;
  221. var globalLink = "";
  222.  
  223. function formatAMPM(date) {
  224. var hours = date.getHours();
  225. var minutes = date.getMinutes();
  226. //var ampm = hours >= 12 ? 'PM' : 'AM';
  227. //hours = hours % 12;
  228. hours = hours ? hours : 12; // the hour '0' should be '12'
  229. minutes = minutes < 10 ? '0' + minutes : minutes;
  230. var strTime = hours + ':' + minutes;// + ' ' + ampm;
  231. return strTime;
  232. }
  233.  
  234. //-- No use time. It is a javaScript effect.
  235. function insertChat(isBoot, text) {
  236.  
  237. var control = "";
  238. var date = formatAMPM(new Date());
  239.  
  240. if (isBoot) {
  241. control = '<li style="width:100%">' +
  242. '<div class="msj macro">' +
  243. '<div class="text text-l">' +
  244. '<p>' + text + '</p>' +
  245. '<p><small>' + date + '</small></p>' +
  246. '</div>' +
  247. '</div>' +
  248. '</li>';
  249. } else {
  250. control = '<li style="width:100%;">' +
  251. '<div class="msj-rta macro">' +
  252. '<div class="text text-r">' +
  253. '<p>' + text + '</p>' +
  254. '<p><small>' + date + '</small></p>' +
  255. '</div>' +
  256. '</li>';
  257. }
  258.  
  259. $("ul").append(control);//.scrollTop($("ul").prop('scrollHeight'));
  260. $("#ulChatBody").scrollTop(10000);
  261.  
  262. }
  263.  
  264. function sendMessageUser() {
  265. var messageUser = $('#txtMessageUser').val();
  266. insertChat(false, messageUser);
  267. $('#txtMessageUser').val("");
  268. $.ajax({
  269. url: "/message/",
  270. type: 'POST',
  271. data:JSON.stringify({
  272. "conversationId":sessionID,
  273. "usersAnswer" :messageUser
  274. }),
  275. headers: {
  276. "Content-Type":"application/json"
  277. },
  278. async: true,
  279. dataType: "json",
  280. success: function (result) {
  281. if (result != null) {
  282. var str = "";
  283. var link = "";
  284. if(result.urls != null){
  285. for(var i=0; i < result.urls.length; i++){
  286. link = link + '<a target="_blank" href="' + result.urls[i].URL
  287. + '">' + result.urls[i].text + '</a><br/>';
  288. }
  289. }
  290. for(var i = 0; i < result.watsonsQuestion.length; i++){
  291. str = str + result.watsonsQuestion[i] + ' ###';
  292. }
  293.  
  294. var message = link + " " + stringParser(str, link);
  295. insertChat(true, message);
  296. //Reload id every time. backend may invalidate it.
  297. sessionID = result.conversationId;
  298. countMsg++;
  299. if(result.ua == true || result.eoc == true){ //Try getting rate until done.
  300. countMsg = 0;
  301. }
  302. if(countMsg > 3 || sessionID === ""){
  303. $('#divMsg').hide();
  304. $('#spanSendMessage').hide();
  305. $('#btnAgain').show();
  306. }
  307. } else {
  308. alert("download data error");
  309. }
  310. },
  311. error: function () {
  312. alert("connection failed");
  313. }
  314. });
  315. }
  316.  
  317. function sendMessageFirst() {
  318. var data = "";
  319. data = JSON.stringify(data)
  320. $.ajax({
  321. url: "/message/",
  322. type: 'POST',
  323. data:JSON.stringify({
  324. "conversationId":"",
  325. "usersAnswer" :""
  326. }),
  327. headers: {
  328. "Content-Type":"application/json"
  329. },
  330. async: true,
  331. dataType: "json",
  332. success: function (result) {
  333. if (result != null) {
  334. insertChat(true, stringParser(result.watsonsQuestion[0]));
  335. sessionID = result.conversationId;
  336. countMsg++;
  337. } else {
  338. alert("download data error");
  339. }
  340. },
  341. error: function () {
  342. alert("connection failed");
  343. }
  344. });
  345. }
  346.  
  347. function resetChat() {
  348. $("ul").empty();
  349. }
  350.  
  351. function startAgain() {
  352. location.reload();
  353. }
  354.  
  355. function clickLink() {
  356. window.open(globalLink);
  357. }
  358.  
  359. function stringParser(question, link){
  360. if(question.indexOf('#isLink=true#') + 1) {
  361. var url = question.replace('#isLink=true#', '');
  362. window.open(url);
  363. return '< span class="spanHref" onclick="sendToTxtMessageUser('+url+')">result link</span>'
  364. }
  365. else{
  366. var res = question.split(" ");
  367. var returnHtml = "";
  368. for (var i = 0; i < res.length; i++) {
  369. if(res[i] != '#link#'){
  370. var span = '<span class="spanMessage" onclick="sendToTxtMessageUser(' +"'" +
  371. res[i].replace('\n', '').replace('"', '').replace('.', '').replace('###', '')
  372. + "'" + '); return false">' + res[i].replace('###', '<br/>') + ' </span>';
  373. returnHtml = returnHtml + span;
  374. }
  375. else{
  376. returnHtml = returnHtml + res[i];
  377. }
  378. }
  379. return returnHtml.replace('#link#', link);
  380. }
  381.  
  382. }
  383.  
  384. function sendToTxtMessageUser(text){
  385. var message = $('#txtMessageUser').val();
  386. if(message.length == 0){
  387. $('#txtMessageUser').val(message + text);
  388. }
  389. else{
  390. $('#txtMessageUser').val(message + " " + text);
  391. }
  392.  
  393. }
  394.  
  395.  
  396. $(document).ready(function () {
  397. //-- Clear Chat
  398. resetChat();
  399. $("#txtMessageUser").keyup(function (event) {
  400. if (event.keyCode == 13) {
  401. sendMessageUser();
  402. }
  403. });
  404. $('#btnAgain').hide();
  405. sendMessageFirst();
  406.  
  407. });
  408. </script>
  409. <body>
  410. <div id="chat" style="position: absolute">
  411. <div class="">
  412. <div id="code">
  413. </div>
  414.  
  415. <div class="col-sm-10 col-sm-offset-1">
  416. <div class="row" id="divChatHeader">
  417. <div id="divChatBotName">Chat Bot Janush</div>
  418. <button type="button" onclick="startAgain();" style="height: 30px; margin-top: 5px; margin-right: 10px;"
  419. class="btn btn-secondary">reload chat</button>
  420. </div>
  421. </div>
  422. <div class="col-sm-10 col-sm-offset-1 frame">
  423. <div id="divChatBody">
  424. <ul id="ulChatBody"></ul>
  425. </div>
  426. <div style="background-color: rgba(35, 47, 62, 0.6); border-radius: 0 0 5px 5px;">
  427. <div class="msj-rta macro" id="divMsg">
  428. <div class="text text-r" style="background: whitesmoke !important">
  429. <input class="mytext" id="txtMessageUser" placeholder="Type a message" />
  430. </div>
  431. </div>
  432. <div class="msj-rta macro" id="btnAgain" style="background:rgba(35, 47, 62, 0.0)">
  433. <button type="button" onclick="startAgain();"
  434. class="btn btn-secondary">start again</button>
  435. </div>
  436. <div style="padding: 10px;">
  437. <span id="spanSendMessage" class="glyphicon glyphicon-share-alt"
  438. onclick="sendMessageUser(); return false" style="cursor: pointer;"></span>
  439. </div>
  440. </div>
  441. </div>
  442. </div>
  443. </div>
  444. </body>
  445. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement