-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatBot.js
135 lines (113 loc) · 2.86 KB
/
ChatBot.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {Component} from 'react';
import './TextToSpeech';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
Image,
StatusBar,
} from 'react-native';
import {GiftedChat} from 'react-native-gifted-chat';
import {Dialogflow_V2} from 'react-native-dialogflow';
import { dialogflowConfig } from './env';
const BOT_USER = {
_id: 2,
name: 'Virtual Assistant',
avatar: 'https://media-exp1.licdn.com/dms/image/C4E0BAQERwqLK35zG0A/company-logo_200_200/0?e=2159024400&v=beta&t=jt9W4ycT0Y4g38R3gfjuGAtAzKrGfsx25YbszmHik0A',
};
class ChatBot extends Component {
constructor(props) {
super(props);
this.state = {
messages: [
{
_id: 1,
text: `Hi! I am your virtual assistant from Dell Technologies.\n\nHow may I help you today?`,
createdAt: new Date(),
user: BOT_USER,
}
]
};
};
onSend(messages = []) {
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, messages)
}));
let message = messages[0].text;
Dialogflow_V2.requestQuery(
message,
result => this.handleGoogleResponse(result),
error => console.log (error)
)
}
componentDidMount() {
Dialogflow_V2.setConfiguration(
dialogflowConfig.client_email,
dialogflowConfig.private_key,
Dialogflow_V2.LANG_ENGLISH_US,
dialogflowConfig.project_id
);
this.setState({
messages: [
{
_id: 1,
text: `Hi! I am your virtual assistant from Dell Technologies.\n\nHow may I help you today?`,
createdAt: new Date(),
user: BOT_USER,
}
]
})
}
handleGoogleResponse (result) {
let text = result.queryResult.fulfillmentMessages[0].text.text[0];
console.log(text);
this.sendBotResponse (text);
}
sendBotResponse(text) {
let msg = {
_id: this.state.messages.length + 1,
text,
createdAt: new Date(),
user: BOT_USER
};
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, [msg])
}));
this.props.fortextHandle(text)
}
// reverseArray (messages) {
// revMessages= this.state.messages.reverse();
// }
render() {
console.log("n chatbot");
// console.log(this.state)
return (
// <View style={styles.giftedchat}>
<GiftedChat
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
onPressActionButton = {()=>this.props.fortextHandle(this.state.messages)}
user={{
_id: 1
}}
/>
// </View>
);
}
}
styles = StyleSheet.create ({
giftedchat: {
flex: 1,
backgroundColor: '#fff',
}
});
export default ChatBot;