Last active
June 26, 2018 23:18
-
-
Save Leocardoso94/9f2de09ed9e16220ff0175d9b35526a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const textInput = document.getElementById('textInput'); | |
| const chat = document.getElementById('chat'); | |
| let context = {}; | |
| const templateChatMessage = (message, from) => ` | |
| <div class="from-${from}"> | |
| <div class="message-inner"> | |
| <p>${message}</p> | |
| </div> | |
| </div> | |
| `; | |
| const InsertTemplateInTheChat = (template) => { | |
| const div = document.createElement('div'); | |
| div.innerHTML = template; | |
| chat.appendChild(div); | |
| }; | |
| const getWatsonMessageAndInsertTemplate = async (text = '') => { | |
| const uri = 'http://localhost:3000/conversation/'; | |
| const response = await (await fetch(uri, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| text, | |
| context, | |
| }), | |
| })).json(); | |
| context = response.context; | |
| const template = templateChatMessage(response.output.text, 'watson'); | |
| InsertTemplateInTheChat(template); | |
| }; | |
| textInput.addEventListener('keydown', (event) => { | |
| if (event.keyCode === 13 && textInput.value) { | |
| getWatsonMessageAndInsertTemplate(textInput.value); | |
| const template = templateChatMessage(textInput.value, 'user'); | |
| InsertTemplateInTheChat(template); | |
| textInput.value = ''; | |
| } | |
| }); | |
| getWatsonMessageAndInsertTemplate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment