FredrikOseberg / react-chatbot-kit

MIT License
325 stars 156 forks source link

Load history from API #41

Closed filipepimentel closed 3 years ago

filipepimentel commented 3 years ago

Hello, Fredrik. Thanks for the great component that you created.

Is there a possibility to obtain the history from an api, instead of the local storage? In this case I think it could be the initial messages, coming from an api.

I saw your comment on the issue below, but still, I don't know how I could read the history of an api.

https://github.com/FredrikOseberg/react-chatbot-kit/issues/4#issuecomment-683711856

Thank you.

FredrikOseberg commented 3 years ago

Hello, Fredrik. Thanks for the great component that you created.

Is there a possibility to obtain the history from an api, instead of the local storage? In this case I think it could be the initial messages, coming from an api.

I saw your comment on the issue below, but still, I don't know how I could read the history of an api.

#4 (comment)

Thank you.

Hi, thanks for the kind words.

Yeah, you can obtain the message history from an API, there's a few ways you could do this currently.

One way would be to implement it in the loadMessages function:

const loadMessages = async () => {
     const res = await getMessageHistory()
     const data = await res.json()

     return data;
}

<Chatbot ... messageHistory={loadMessages()} />

Another way would be to inject it directly into your config:

// fetch the history from wherever you want in your app and load the config dynamically

const getConfig = (messageHistory) => {
     return {
          initialMessages: [...messageHistory]
     }
}

<Chatbot config={getConfig(messageHistory)} />

Does that work for you?

filipepimentel commented 3 years ago

It definitely works for me, Fredrik. Thank you for your quick response.