-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
55 lines (43 loc) · 1.26 KB
/
handler.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
'use strict';
const { AWSTranslateJSON } = require('aws-translate-json');
const rp = require('request-promise');
const TELEGRAM_TOKEN = process.env.TELEGRAM_TOKEN;
const awsConfig = {
accessKeyId: process.env.AWS_TRANSLATE_ID,
secretAccessKey: process.env.AWS_TRANSLATE_SECRET,
region: process.env.AWS_TRANSLATE_REGION,
}
const source = "fa";
const target = ["en"];
const { translateJSON } = new AWSTranslateJSON(awsConfig, source, target);
module.exports.handleTranslatorRequest = async event => {
const body = JSON.parse(event.body);
const {chat, text} = body.message;
console.log(text);
if (text) {
let message = '';
try {
const result = await translateJSON({key1: text});
console.log(result);
message = result.en.key1;
} catch (error) {
message = `Input: ${text}, \nError: ${error.message}`;
}
console.log("Sending to user: " + message)
await sendToUser(chat.id, message);
} else {
await sendToUser(chat.id, 'Text message is expected.');
}
return { statusCode: 200 };
};
async function sendToUser(chat_id, text) {
const options = {
method: 'GET',
uri: `https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`,
qs: {
chat_id,
text
}
};
return rp(options);
}