chatbot.js
6.9 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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";
import CheckString from './Check';
const username = "유저";
function Chatbot() {
const dispatch = useDispatch();
const messagesFromRedux = useSelector(state => state.message.messages)
useEffect(() => {
eventQuery('001_Welcome')
.then(eventQuery('002_Intro'))
}, [])
const textQuery = async (text) => {
// First Need to take care of the message I sent
let conversation = {
who: username,
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] === '@'){
console.log("첫번째 i", inputString);
inputString = CheckString(inputString);
console.log("두번째 i",inputString);
let response;
if(inputString === '최신'){ //@가수명_최신
response = await Axios.post('/api/latest/textQuery', textQueryVariables)
}
else if(inputString === '정확도'){ //@가수명_정확도
response = await Axios.post('/api/related/textQuery', textQueryVariables)
}
else if(inputString === '소식'){ //@가수명_소식
console.log("뉴스들어옴")
response = await Axios.post('/api/news/textQuery', textQueryVariables)
}
// const response = await Axios.post('/api/crawling/textQuery', textQueryVariables)
let conversations = {}
let pushConversations = []
for(var i = 0; i < 3; i++){
conversation = {
who : '소통이',
content : response.data[i]
}
pushConversations.push(conversation)
}
conversations["card"] = pushConversations
console.log("conversations",conversations)
dispatch(saveMessage(conversations))
} 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
}
console.log("conversation: ",conversation)
dispatch(saveMessage(conversation))
}
}
} catch (error) {
conversation = {
who: '소통이',
content: {
text: {
text: "잘못된 입력입니다. 다시 입력해주세요!"
}
}
}
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) => {
cards.map((card,i) => console.log("card.content",card.content))
return cards.map((card,i) => <Card key={i} cardInfo={card.content} />)
}
const renderOneMessage = (message, i) => {
console.log('message', message)
console.log('message.card',message.card)
// 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.card) {
const AvatarSrc = message.who === '소통이' ? <Icon type="robot" /> : <Icon type="smile" />
console.log("message content",message.content)
return <div>
<List.Item style={{ padding: '1rem' }}>
<List.Item.Meta
avatar={<Avatar icon={AvatarSrc} />}
title={message.who}
description={renderCards(message.card)}
/>
</List.Item>
</div>
}
}
const renderMessage = (returnedMessages) => {
if (returnedMessages) {
return returnedMessages.map((message, i) => {
return renderOneMessage(message, i);
})
} else {
return null;
}
}
return (
<div style={{
height: 650, width: 700,
border: '3px solid black', borderRadius: '7px'
}}>
<div style={{ height: 594, width: '100%', overflow: 'auto'}}>
{renderMessage(messagesFromRedux)}
</div>
<input
style={{
margin: 0, width: '100%', height: 50,
borderRadius: '4px', padding: '5px', fontSize: '1rem', fontFamily: 'KyoboHand'
}}
placeholder="Send a message..."
onKeyPress={keyPressHanlder}
type="text"
/>
</div>
)
}
export default Chatbot;