-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
80 lines (71 loc) · 1.71 KB
/
App.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
import React from "react";
import { View } from "react-native";
import { GiftedChat } from "react-native-gifted-chat";
import { Dialogflow_V2 } from "react-native-dialogflow-text";
const BOT_USER = {
_id: 2,
name: "SmartBot",
avatar: "https://placeimg.com/140/140/any"
};
export default class App extends React.Component {
constructor(props) {
super(props);
let firstMsg = {
_id: 1,
text: "Hello CoderSchool Fan!",
createdAt: new Date(),
user: BOT_USER
};
this.state = {
messages: [firstMsg]
};
}
componentDidMount() {
Dialogflow_V2.setConfiguration(
"CLIENT EMAIL",
"PRICATE KEY",
Dialogflow_V2.LANG_ENGLISH_US,
"PROJECT ID"
);
}
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])
}));
}
handleGoogleResponse(result) {
console.log(result);
let text = result.queryResult.fulfillmentMessages[0].text.text[0];
this.sendBotResponse(text);
}
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)
);
}
render() {
return (
<View style={{flex: 1}}>
<GiftedChat
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: 1
}}
/>
</View>
);
}
}