FredrikOseberg / react-chatbot-kit

MIT License
299 stars 139 forks source link

How to display widgets only using createChatBotMessage method? #62

Open VeenaMalali13 opened 3 years ago

VeenaMalali13 commented 3 years ago

When we don't have any message to display and we want to display widgets only, then what should be first parameter to createChatBotMessage method? e.g. createChatBotMessage (what should go here,{widget: widgetName});

vijay-kumar-singh commented 3 years ago

I am also looking solution for this query. As i am doing fetch call inside widget component and have to update chat bot message only when API respond back.

VeenaMalali13 commented 3 years ago

I am also using fetch call, and in response I am getting buttons only(which I am using as widgets in chatbot). But here I want to show widget only and no text message. Please update if you get any solution. Thanks in advance.

FredrikOseberg commented 3 years ago

You can't currently do this. I'm working on expanding this for v2, but right now you can't render widgets alone. They must always be attached to a chatbot message. A hacky solution would be to modify the existing previous message provided that it does not have a widget:

// on fetch response 

this.setState(prev => {
     const lastMessage = prev.messages[prev.messages.length - 1];
     lastMessage.widget = "myWidget"

     return {
          ...prev, messages: [prev.slice(0, prev.messages.length - 1), lastMessage]
     }
})

@vijay-kumar-singh is this the same problem? If you are doing a fetch call in a rendered widget you can use the provided setState method to update the state once you get a response in the widget. All widgets receive the internal setState function as props, which you can access from the widget and update state.

greendinosaur commented 10 months ago

Any update as to whether this has been addressed? The workaround I've used is to call createChatBotMessage with a string with a single space and then style my widget to have the same background colour as the chatbot message. It's a bit of a hack.

sean-cedar commented 10 months ago

The workaround that I've built uses the createCustomMessage, rather than createChatBoxMessage, for certain answer types. This allows you to build a widget type of response via a custom message type, and does not use the standard massage response.