chatbot.js
5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useEffect } from 'react';
import Axios from 'axios';
import { useDispatch, useSelector } from 'react-redux';
import { saveMessage } from '../_actions/message_actions';
import Message from './Sections/Message';
import { List, Icon, Avatar } from 'antd';
import Card from "./Sections/Card";
function Chatbot() {
const dispatch = useDispatch();
const messagesFromRedux = useSelector(state => state.message.messages)
useEffect(() => {
eventQuery('001_Welcome')
eventQuery('002_Intro')
}, [])
const textQuery = async (text) => {
// First Need to take care of the message I sent
let conversation = {
who: 'user',
content: {
text: {
text: text
}
}
}
dispatch(saveMessage(conversation))
// console.log('text I sent', conversation)
var inputString = conversation.content.text.text
// We need to take care of the message Chatbot sent
const textQueryVariables = {
text
}
try {
if(inputString[0] === '@'){
await Axios.post('/api/crawling/textQuery', textQueryVariables)
} else {
//I will send request to the textQuery ROUTE
const response = await Axios.post('/api/dialogflow/textQuery', textQueryVariables)
for (let content of response.data.fulfillmentMessages) {
conversation = {
who: '소통이',
content: content
}
dispatch(saveMessage(conversation))
}
}
} catch (error) {
conversation = {
who: '소통이',
content: {
text: {
text: " Error just occured, please check the problem"
}
}
}
dispatch(saveMessage(conversation))
}
}
const eventQuery = async (event) => {
// We need to take care of the message Chatbot sent
const eventQueryVariables = {
event
}
try {
//I will send request to the textQuery ROUTE
const response = await Axios.post('/api/dialogflow/eventQuery', eventQueryVariables)
for (let content of response.data.fulfillmentMessages) {
let conversation = {
who: '소통이',
content: content
}
dispatch(saveMessage(conversation))
}
} catch (error) {
let conversation = {
who: '소통이',
content: {
text: {
text: " Error just occured, please check the problem"
}
}
}
dispatch(saveMessage(conversation))
}
}
const keyPressHanlder = (e) => {
if (e.key === "Enter") {
if (!e.target.value) {
return alert('you need to type something first')
}
//we will send request to text query route
textQuery(e.target.value)
e.target.value = "";
}
}
const renderCards = (cards) => {
return cards.map((card,i) => <Card key={i} cardInfo={card.structValue} />)
}
const renderOneMessage = (message, i) => {
console.log('message', message)
// we need to give some condition here to separate message kinds
// template for normal text
if (message.content && message.content.text && message.content.text.text) {
return <Message key={i} who={message.who} text={message.content.text.text} />
} else if (message.content && message.content.payload.fields.card) {
const AvatarSrc = message.who === '소통이' ? <Icon type="robot" /> : <Icon type="smile" />
return <div>
<List.Item style={{ padding: '1rem' }}>
<List.Item.Meta
avatar={<Avatar icon={AvatarSrc} />}
title={message.who}
description={renderCards(message.content.payload.fields.card.listValue.values)}
/>
</List.Item>
</div>
}
// template for card message
}
const renderMessage = (returnedMessages) => {
if (returnedMessages) {
return returnedMessages.map((message, i) => {
return renderOneMessage(message, i);
})
} else {
return null;
}
}
return (
<div style={{
height: 620, width: 700,
border: '3px solid black', borderRadius: '7px'
}}>
<div style={{ height: 564, width: '100%', overflow: 'auto' }}>
{renderMessage(messagesFromRedux)}
</div>
<input
style={{
margin: 0, width: '100%', height: 50,
borderRadius: '4px', padding: '5px', fontSize: '1rem'
}}
placeholder="Send a message..."
onKeyPress={keyPressHanlder}
type="text"
/>
</div>
)
}
export default Chatbot;