Skip to content

Instantly share code, notes, and snippets.

@FredrikOseberg
Last active June 22, 2021 15:48
Show Gist options
  • Save FredrikOseberg/9c994790c6dd76f3d1d9dffac59ef2bb to your computer and use it in GitHub Desktop.
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;
@g4rry420
Copy link

g4rry420 commented Jan 3, 2021

Hello @FredrikOseberg Is it possible if I can have an access of state of the chatbot in the actionProvider too ?

@FredrikOseberg
Copy link
Author

Hello @FredrikOseberg Is it possible if I can have an access of state of the chatbot in the actionProvider too ?

Yeah, you can do that in several ways. One way is to pass it from the MessageParser:

this.actionProvider.someMethod(this.state)

Or you can access it from the setState function in the messageParser:

this.setState(state => {
      // Do what you need to do with state here
      
      return state
})

@g4rry420
Copy link

g4rry420 commented Jan 4, 2021

Oh, Thanks

@g4rry420
Copy link

g4rry420 commented Jan 4, 2021

I got one more question, if you can help me with that. When a user enters a message, it parses through the MessageParser but it doesn't get to saved to the state of the chatbot immediately which creates a problem in my version of app that I am creating. Is there any way that when a user enters a message, first it gets to saved to the state of the chatbot and then it renders in the chatbot component ?

@FredrikOseberg
Copy link
Author

FredrikOseberg commented Jan 4, 2021 via email

@g4rry420
Copy link

g4rry420 commented Jan 4, 2021

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 ?

@FredrikOseberg
Copy link
Author

FredrikOseberg commented Jan 4, 2021 via email

@g4rry420
Copy link

g4rry420 commented Jan 4, 2021

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

@FredrikOseberg
Copy link
Author

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
    })
}

@g4rry420
Copy link

g4rry420 commented Jan 4, 2021

Got it. Now, it works, Thank You

@susmitha01
Copy link

susmitha01 commented May 20, 2021

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

@VSevagen
Copy link

is there a way to handle several updateChatbotState in a function ?........it glitches whenever I try to update after createChatBotMessage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment