I am trying to set up a chat page that the user can interact with a chatbot that I have already created. The users messages pop up on the right side of the page but when the message is sent to the server and a response is returned, I am unsure on how to display it on the left side.
const [messages, setMessages] = useState([
{
_id: 0,
text: 'Hello! How may I assist you?',
createdAt: new Date().getTime(),
user: {
_id: 2,
name: 'System',
avatar: require('./assets/splash.png')
}
},
{
_id: 1,
text: 'New chat started.',
createdAt: new Date().getTime(),
system: true
}
]);
function systemResponse(answer){
<GiftedChat
/>
}
function handleSend(newMessage = []){
setMessages(GiftedChat.append(messages, newMessage));
console.log(newMessage);
var userText = newMessage[0].text;
console.log(userText);
fetch('http://ec2-3-23-33-73.us-east-2.compute.amazonaws.com:5000/chatbot',
{
method: 'POST',
body: JSON.stringify({
textString: userText,
})
})
.then((response) => response.json())
.then((json) => {<GiftedChat
user={{_id: 2, name:"System", avatar: require('./assets/splash.png')}}
text = 'hello'
/>})
}
return(
<GiftedChat
onSend={newMessage => handleSend(newMessage)}
messages={messages}
user={{ _id: 1, name: 'Luke' }}
placeholder="Ask your quesiton here..."
showUserAvatar
alwaysShowSend
scrollToBottom
/>
);