forked from wamikamalik/eupheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
199 lines (181 loc) · 5.23 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
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {Component} from 'react';
import { StyleSheet, Text, View, Image, Button, TouchableOpacity } 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: 'PERIOD Bot',
avatar: 'https://img1.picmix.com/output/stamp/normal/8/2/8/1/1271828_5c823.png'
};
class App extends Component {
_isMounted = false;
componentDidMount() {
this._isMounted = true;
Dialogflow_V2.setConfiguration(
dialogflowConfig.client_email,
dialogflowConfig.private_key,
Dialogflow_V2.LANG_ENGLISH_US,
dialogflowConfig.project_id
);
}
componentWillUnmount() {
this._isMounted = false;
}
state = {
image: null,
hasImage: false,
buttons: [],
card: false,
id: null,
messages: [
{
_id: 1,
text: `Hi, I am the PERIOD bot🩸!`,
createdAt: new Date(),
user: BOT_USER,
image: null,
buttons:[]
}
]
};
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)
);
}
executeButton(messages = []) {
let message = messages[0].text;
Dialogflow_V2.requestQuery(
message,
result => this.handleGoogleResponse(result),
error => console.log(error)
);
}
async handleGoogleResponse(result) {
var text
var buttons=[], image
var button = []
this.setState({buttons:[]})
console.log(result.queryResult.fulfillmentMessages[0])
if (result.queryResult.fulfillmentMessages[0].card != undefined) {
text = await result.queryResult.fulfillmentMessages[0].card.title+'\n'+result.queryResult.fulfillmentMessages[0].card.subtitle
if(result.queryResult.fulfillmentMessages[0].card.buttons != undefined) {
button = result.queryResult.fulfillmentMessages[0].card.buttons
button.map(b=>{
console.log(b)
buttons.push({message: [{ _id: this.state.messages.length + 1,
text: b.postback,
createdAt: new Date(),
user: {
_id: 1,
name: 'User',
avatar: 'https://frodsham.gov.uk/wp-content/uploads/2019/05/profile-photo-placeholder.jpg'
}
}], title: b.text})
})
this.setState({buttons: buttons, id: this.state.messages.length+1})
}
if(result.queryResult.fulfillmentMessages[0].card.imageUri != undefined) {
image = await result.queryResult.fulfillmentMessages[0].card.imageUri
this.setState({image: image, card: true, hasImage: true})
}
this.sendBotResponse(text);
}
else {
text = await result.queryResult.fulfillmentMessages[0].text.text[0]
this.sendBotResponse(text);
}
console.log(text)
}
sendBotResponse(text) {
let msg = {
_id: this.state.messages.length + 1,
text,
createdAt: new Date(),
user: BOT_USER,
image: this.state.card&&this.state.hasImage?this.state.image:"",
buttons: this.state.buttons
};
//console.log(this.card)
this.setState({card:false, hasImage: false})
this.setState(previousState => ({
messages: GiftedChat.append(previousState.messages, [msg])
}));
}
render() {
let i = 0;
return (
<View style={{ flex: 1, backgroundColor: "#ff6060" }}>
<GiftedChat
showUserAvatar={true}
imageProps={styles.image}
renderCustomView = {(currentMessage)=>
{
return currentMessage.currentMessage.buttons&¤tMessage.currentMessage.buttons.map(button=>{
return (
<View>
{currentMessage.currentMessage._id==this.state.id&&<TouchableOpacity style={styles.button} onPress={() => {this.executeButton(button.message)}}><Text style={styles.text}>{button.title}</Text></TouchableOpacity>}
</View>
)
})
}}
isCustomViewBottom={true}
messages={this.state.messages}
onSend={messages => this.onSend(messages)}
user={{
_id: 1,
name: 'User',
avatar: 'https://frodsham.gov.uk/wp-content/uploads/2019/05/profile-photo-placeholder.jpg'
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
button: {
alignItems: 'center',
marginBottom: 20,
marginTop:20,
elevation: 20,
justifyContent: 'center',
alignSelf:'center',
alignContent: 'center',
borderWidth: 5,
borderColor: '#FF0000',
borderRadius: 20,
width: 250,
height: 60,
color: 'white',
backgroundColor: '#8B0000'
},
text: {
fontSize: 14,
color: "#fff",
fontWeight: "bold",
alignSelf: "center",
},
image: {
width: 300,
alignSelf:'center',
height: 300,
}
})
export default App;