-
-
Save FredrikOseberg/8b3cfadeb1980bdb7b52cd4138d64542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import Chatbot from "react-chatbot-kit"; | |
import config from "./config"; | |
import actionProvider from "./actionProvider.js"; | |
import messageParser from "./messageParser.js"; | |
function App() { | |
const [showBot, toggleBot] = useState(false); | |
const saveMessages = (messages) => { | |
localStorage.setItem("chat_messages", JSON.stringify(messages)); | |
}; | |
const loadMessages = () => { | |
const messages = JSON.parse(localStorage.getItem("chat_messages")); | |
return messages; | |
}; | |
return ( | |
<div className="App"> | |
{showBot && ( | |
<Chatbot | |
config={config} | |
actionProvider={actionProvider} | |
messageHistory={loadMessages()} | |
messageParser={messageParser} | |
saveMessages={saveMessages} | |
/> | |
)} | |
<button onClick={() => toggleBot((prev) => !prev)}>Bot</button> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you could save messages in action provider using an array of objects to save ques and ans like this -
import db from "../firebase";
var chat = [];
async function sendData() {
await db
.collection("bot")
.add({ chatBot: chat })
.then(() => {
console.log("success");
chat = [];
})
.catch((error) => {
console.log(error);
});
}
class ActionProvider {
constructor(createChatBotMessage, setStateFunc, createClientMessage) {
this.createChatBotMessage = createChatBotMessage;
this.setState = setStateFunc;
this.createClientMessage = createClientMessage;
}
handleOptionOne = () => {
const message = this.createChatBotMessage("Whats Your Business?", {
widget: "optionOne",
});
};
handleJavascriptQuiz = (ans) => {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
};
handleOptionTwo = (ans) => {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
const message = this.createChatBotMessage("Whats your budget?", {
widget: "optionThree",
});
};
handleOptionThree = (ans) => {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
const message = this.createChatBotMessage(
"Are You looking for Online Collaboration?",
{
widget: "optionFour",
}
);
};
handleOptionFour = (ans) => {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
const message = this.createChatBotMessage(
"Thanks for the details,Please drop your contact number",
{
widget: "",
}
);
};
details = (ans) => {
if (ans) {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
} else chat.pop();
};
greet = (ans) => {
if (ans) {
chat[chat.length - 1] = { ...chat[chat.length - 1], ans: ans };
} else chat.pop();
};
phonenumber = (inputtxt) => {
var phoneno = /^\d{10}$/;
};
addMessageToState = (message) => {
chat.push({ ques: message.message });
};
}
export default ActionProvider;