-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.js
205 lines (205 loc) ยท 6.55 KB
/
backup.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
200
201
202
203
204
205
const https = require("https");
const AWS = require("aws-sdk");
AWS.config.region = "ap-northeast-1";
AWS.config.update({region: 'ap-northeast-1'});
const s3 = new AWS.S3({apiVersion: "2006-03-01"});
const docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
const line = require("@line/bot-sdk");
const client = new line.Client({
channelAccessToken: process.env.ACCESSTOKEN
});
const endpoint = "https://takakuureru-bot-imgs.s3-ap-northeast-1.amazonaws.com/";
let rString = randomString();
// ไนฑๆฐใฎsessionใ็ๆใใ
function randomString() {
const length = 32;
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
exports.handler = (event, context) => {
let body = JSON.parse(event.body);
const userId = body.events[0].source.userId;
const message = body.events[0].message;
console.log("body.message---------------------------");
console.log(message);
const replayToken = body.events[0].replyToken
function getSessionData() {
const params = {
TableName: "LinebotUserSession",
Key: {
id: userId + rString
}
};
try {
console.log("console params---------")
console.log(params)
var Item = docClient.get(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
}
}).promise();
return Item;
} catch (e) {
return -1;
}
}
function updateImage(session, images) {
const params = {
TableName: "LinebotUserSession",
Key: {
id: session,
},
ExpressionAttributeNames: {
'#images': 'images',
},
ExpressionAttributeValues: {
':images': images
},
UpdateExpression: 'SET #images = :images'
};
docClient.update(params, function (err, data) {
if (err) {
console.log(err);
} else {
console.log("update succeed")
console.log(data);
}
});
}
function saveSession(imageKey) {
var images = [];
images.push(imageKey);
const item = {
id: userId + rString,
userId: userId,
images: images
};
const params = {
TableName: "LinebotUserSession",
Item: item
};
docClient.put(params, function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
}
function sendMessage(text) {
const message = {
type: "text",
text: text
};
client.replyMessage(replayToken, message).then(response => {
let lambdaResponse = {
statusCode: 200,
headers: {
"X-Line-Status": "OK"
},
body: '{"result":"completed"}'
};
context.succeed(lambdaResponse);
}).catch(err => console.log(err));
}
function confirmMessage(text, label1, label2) {
return client.replyMessage(replayToken, {
"type": "template",
"altText": text,
"template": {
"type": "confirm",
"text": text,
"actions": [{
"type": "message",
"label": label1,
"text": label1
}, {
"type": "message",
"label": label2,
"text": label2,
}]
}
});
}
console.log("message--------------------");
console.log(message);
//ใชใฏใจในใใๅๅพ
// ใใใใฎๅ ดๅ:ใทในใใ ใใใกใใปใผใธ้ไฟกใโโๆงใใๅฉ็จใใใใจใใใใใพใใ๏ผใใใงใฏใใๅ็ฉใ่ฒทๅๅฏ่ฝใใ่ชฟในใใใฆ้ ใใพใใญใๆปๅฎ็ตๆใใๅพ
ใกไธใใใใ
if (message.type !== "image" && message.text !== "ใใใ") {
if (message.text === "ใฏใ") {
sendMessage("ใๅ็ฉใฎ่ฟฝๅ ็ปๅใ้ใไธใใใ");
}
else if (message.text === "ใชใ") {
sendMessage("ใๅฉ็จใใใใจใใใใใพใใ๏ผ");
}
else {
sendMessage("ๆปๅฎใใใใๅ็ฉใใใใฐใ็ปๅใ้ไฟกใใฆใใ ใใ๏ผ(ๆๅคง3ๆใพใง)");
}
}
if (message.type === "image") {
const message_id = message.id;
// Request Headers
const send_options = {
host: "api.line.me",
path: "/v2/bot/message/" + message_id + "/content",
headers: {
"Content-type": "application/json; charset=UTF-8",
Authorization: " Bearer " + process.env.ACCESSTOKEN
},
method: "GET"
};
// APIใชใฏใจในใ
const req = https.request(send_options, function (res) {
const data = [];
res.on("data", function (chunk) {
//image data dividing it in to multiple request
data.push(new Buffer(chunk));
}).on("error", function (err) {
console.log(err);
sendMessage("็ปๅ้ไฟกๅคฑๆใใพใใ๏ผ");
}).on("end", function () {
let imagesSession = randomString();
console.log("data-----------------")
console.log(data);
const params = {
Bucket: process.env.S3_BUCKET_NAME,
Key: rString + imagesSession + ".jpg",
Body: Buffer.concat(data)
};
console.log("params-------------------")
console.log(params);
s3.putObject(params, function (err, data) {
// ็ปๅไฟๅญๅพใฎๅฆ็ ใใผใฟใฎๆดๅฝขใจDBใซไฟๅญใใ
getSessionData().then(res => {
console.log("res---------")
console.log(res);
console.log("this rString-----")
console.log(rString);
console.log("Object.keys(res).length---------------------")
console.log(Object.keys(res).length)
if (Object.keys(res).length) {
const images = res.Item.images
if (Object.keys(images).length < 2) {
images.push(params.Key)
console.log("images------------------")
console.log(images);
updateImage(res.Item.id, images);
confirmMessage("็ปๅใๅ้ ใใพใใ๏ผๅใใๅ็ฉใฎ่ฟฝๅ ็ปๅใฏใใใใพใใงใใใใใ", "ใฏใ", "ใใใ");
} else {
sendMessage("็ปๅใฏๆๅคง3ๆใงใใใใพใใฎใงใใใใใจใใใใใพใใ๏ผ");
}
} else {
saveSession(params.Key);
confirmMessage("็ปๅใๅ้ ใใพใใ๏ผๅใใๅ็ฉใฎ่ฟฝๅ ็ปๅใฏใใใใพใใงใใใใใ", "ใฏใ", "ใใใ");
}
});
});
});
});
req.end();
}
};