@cmnd-ai/chatbot-react 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +121 -0
- package/package.json +1 -1
package/Readme.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
sidebar_position: 1
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Documentation
|
|
6
|
+
|
|
7
|
+
## cmnd-chat-bot-package
|
|
8
|
+
|
|
9
|
+
This npm package provides a customizable chatbot component for use in web applications, designed specifically for Node.js environments.
|
|
10
|
+
|
|
11
|
+
### Requirements
|
|
12
|
+
|
|
13
|
+
Before using this package, ensure you have the following prerequisites installed on your computer:
|
|
14
|
+
|
|
15
|
+
- Node.js
|
|
16
|
+
|
|
17
|
+
### Installation
|
|
18
|
+
|
|
19
|
+
You can install the `cmnd-chat-bot-package` from npm using npm or yarn:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install cmnd-chat-bot-package
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### With yarn
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
yarn add cmnd-chat-bot-package
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Example implementation
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
import { ChatProvider, CmndChatBot } from "test-cmnd-chatbot";
|
|
35
|
+
import { useEffect } from "react";
|
|
36
|
+
|
|
37
|
+
const components = {
|
|
38
|
+
messages({ props }) {
|
|
39
|
+
const messages = props.chats.data?.messages || [];
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
const comp = document.querySelector(`#lastItemId`);
|
|
43
|
+
if (!comp) return;
|
|
44
|
+
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
comp.scrollIntoView({
|
|
47
|
+
behavior: "smooth",
|
|
48
|
+
block: "end", // Scroll to the bottom of the container
|
|
49
|
+
});
|
|
50
|
+
}, 500);
|
|
51
|
+
}, [messages.length]);
|
|
52
|
+
|
|
53
|
+
const getchild = () => {
|
|
54
|
+
if (props.chats.error) {
|
|
55
|
+
return <h1>Error</h1>;
|
|
56
|
+
}
|
|
57
|
+
if (!props.chats.data?.messages?.length)
|
|
58
|
+
return <h1>Ask me a question</h1>;
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div>
|
|
62
|
+
{messages.map((eachMessage, msgIndex) => (
|
|
63
|
+
<div
|
|
64
|
+
key={eachMessage.id}
|
|
65
|
+
id={msgIndex === messages.length - 1 ? "lastItemId" : undefined}
|
|
66
|
+
>
|
|
67
|
+
{eachMessage.message + ""}
|
|
68
|
+
</div>
|
|
69
|
+
))}
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return <div style={{ flexGrow: 1 }}>{getchild()}</div>;
|
|
75
|
+
},
|
|
76
|
+
userInputBox({ props }) {
|
|
77
|
+
return (
|
|
78
|
+
<div
|
|
79
|
+
style={{
|
|
80
|
+
display: "flex",
|
|
81
|
+
gap: "1rem",
|
|
82
|
+
}}
|
|
83
|
+
>
|
|
84
|
+
<input
|
|
85
|
+
style={{
|
|
86
|
+
flexGrow: 1,
|
|
87
|
+
}}
|
|
88
|
+
type="text"
|
|
89
|
+
value={props.userInputData.textValue}
|
|
90
|
+
onChange={(e) => props.userInputData.setTextValue(e.target.value)}
|
|
91
|
+
placeholder="Type here"
|
|
92
|
+
disabled={props.userInputData.isSending}
|
|
93
|
+
/>
|
|
94
|
+
<button
|
|
95
|
+
onClick={props.userInputData.submitMessage}
|
|
96
|
+
disabled={props.userInputData.isSending}
|
|
97
|
+
>
|
|
98
|
+
{props.userInputData.isSending ? "Sending..." : "Send"}
|
|
99
|
+
</button>
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
},
|
|
103
|
+
error({ props }) {
|
|
104
|
+
return <div>Error: {props.chats.error + ""}</div>;
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
function App() {
|
|
109
|
+
return (
|
|
110
|
+
<div>
|
|
111
|
+
<ChatProvider apiKey="xxxxx" chatbotId={1} organizationId={1}>
|
|
112
|
+
{(params) => <CmndChatBot {...params} components={components} />}
|
|
113
|
+
</ChatProvider>
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default App;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
You can find an example implementation from Github [here](https://github.com/CyprusCodes/cmnd-react-chatbot-example.git)
|