-
-
Save FredrikOseberg/9c994790c6dd76f3d1d9dffac59ef2bb to your computer and use it in GitHub Desktop.
class ActionProvider { | |
// The action provider receives createChatBotMessage which you can use to define the bots response, and | |
// the setState function that allows for manipulating the bots internal state. | |
constructor(createChatBotMessage, setStateFunc, createClientMessage) { | |
this.createChatBotMessage = createChatBotMessage; | |
this.setState = setStateFunc; | |
this.createClientMessage = createClientMessage | |
} | |
handleMessageParser = () => { | |
const messages = this.createChatBotMessage( | |
"The message parser controls how the bot reads input and decides which action to invoke.", | |
{ widget: "messageParser", withAvatar: true } | |
); | |
this.addMessageToBotState(messages); | |
}; | |
handleDefault = () => { | |
const message = this.createChatBotMessage("How can I help?", { | |
withAvatar: true, | |
}); | |
this.addMessageToBotState(message) | |
}; | |
addMessageToBotState = (messages) => { | |
if (Array.isArray(messages)) { | |
this.setState((state) => ({ | |
...state, | |
messages: [...state.messages, ...messages], | |
})); | |
} else { | |
this.setState((state) => ({ | |
...state, | |
messages: [...state.messages, messages], | |
})); | |
} | |
}; | |
} | |
export default ActionProvider; |
In the Message Parser,
parse(message){ this.actionProvider.handleUserMessage(message, this.state) }
In the ActionProvider,
handleUserMessage = (chatMessage, state) => { console.log(state) }
The method - handleUserMessage will get invoked immediately when user sends message in the chatbot component
In the Message Parser,
parse(message){ this.actionProvider.handleUserMessage(message, this.state) }
In the ActionProvider,
handleUserMessage = (chatMessage, state) => { console.log(state) }
The method - handleUserMessage will get invoked immediately when user sends message in the chatbot component
Can you try this?
// In the ActionProvider
handleUserMessage = (chatMessage) => {
this.setState(state => {
console.log(state)
return state
})
}
Got it. Now, it works, Thank You
How to handle a validations for suppose i have a emaild and i want to check that it is valid or not , so how can we deal with it
is there a way to handle several updateChatbotState in a function ?........it glitches whenever I try to update after createChatBotMessage.
When the user enters the message and then I try to access the state of the chatbot into the actionPareser using the console.log but I don't see the message being saved their ?