-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
169 lines (151 loc) · 3.96 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
import React, { Component } from 'react';
import { StatusBar } from 'expo-status-bar';
import { Keyboard } from 'react-native';
import { StyleSheet, Text, View, Image} from 'react-native';
import { Interactions, Analytics } from 'aws-amplify';
// import awsconfig from './src/aws-exports';
import { GiftedChat , InputToolbar, Composer, ChatHeaderBar, Bubble, Send , Icon, MessageImage, Lightbox } from 'react-native-gifted-chat';
import 'react-native-url-polyfill/auto';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';
import Header from './Header';
// Amplify.configure(awsconfig);
import { Amplify } from 'aws-amplify';
Amplify.configure({
Auth: {
identityPoolId: '[Enter Identity Pool ID here]',
region: '[Enter Region here]'
},
Interactions: {
bots: {
[Enter Bot Name here]: {
name: '[Enter Bot Name here]',
alias: '$LATEST',
region: '[Enter Region here]'
}
}
}
});
const botName = '[Enter Bot Name here]';
const botUser = {
_id: 2,
name: 'Bot',
avatar: 'https://repository-images.githubusercontent.com/259820727/4edc2c80-9871-11ea-811d-342b0d0395f6',
};
let chatId = 1;
const customtInputToolbar = props => {
return (
<InputToolbar
{...props}
containerStyle={{
backgroundColor: "white",
borderTopColor: "#E8E8E8",
borderTopWidth: 1,
marginBottom: 1,
padding: 5,
borderRadius: 20
}}
placeholder = "Type something..."
/>
);
};
export default class App extends React.Component {
state = {
messages: [
{
_id: chatId,
text: 'Hey, How can I help you?',
user: botUser,
createdAt: new Date(),
image : '',
},
],
};
sendMessageToBot = async (userInput) => {
const response = await Interactions.send(botName, userInput);
console.log(response);
this.appendChatMessages([this.formatMessage(response)]);
};
formatMessage = (res) => {
chatId = chatId + 1;
if(res.responseCard != null){
let pics = 0;
while(pics != 2){
return {
_id: chatId,
createdAt: new Date(),
text: res.message,
user: botUser,
image : res.responseCard.genericAttachments[pics].imageUrl,
};
pics++;
}
}else{
return {
_id: chatId,
createdAt: new Date(),
text: res.message,
user: botUser,
image : '',
};
}
};
onSend = (messages) => {
// console.log(messages);
messages.map((msg) => this.sendMessageToBot(msg.text));
this.appendChatMessages(messages);
};
appendChatMessages = (messages) => {
// console.log(messages);
this.setState((previousState) => ({
messages: GiftedChat.append(previousState.messages, messages),
}));
};
render(){
return (
<View style={styles.container}>
<StatusBar style="auto" hidden = {false} backgroundColor = "#F5F5F5" translucent = {true}/>
<Header/>
<GiftedChat
renderInputToolbar={props => customtInputToolbar(props)}
messages={this.state.messages}
onSend={(messages) => this.onSend(messages)}
user={{
_id: 1,
}}
renderBubble = {(props) => {
return <Bubble
{...props}
wrapperStyle={{
right: {
backgroundColor : "blue",
marginBottom: 5,
padding : 3,
},
left: {
backgroundColor : "#EAEAEA",
padding : 3,
marginBottom: 5,
}
}}
/>
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
messageInput: { borderRadius: 30, borderTopColor: '#222', backgroundColor: 'transparent', },
inputToolbar: {
marginLeft: 15,
marginRight: 15,
marginBottom: 10,
borderWidth: 0.5,
borderColor: 'grey',
borderRadius: 25
},
});