Message translate on edit

This commit is contained in:
SillyLossy
2023-05-28 22:43:31 +03:00
parent 73fa174165
commit b74939bf65
3 changed files with 59 additions and 57 deletions

View File

@ -3034,7 +3034,6 @@ app.post('/horde_generateimage', jsonParser, async (request, response) => {
app.post('/google_translate', jsonParser, async (request, response) => {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const https = require('https');
const text = request.body.text;
const lang = request.body.lang;
@ -3045,24 +3044,21 @@ app.post('/google_translate', jsonParser, async (request, response) => {
console.log('Input text: ' + text);
const url = generateRequestUrl(text, { to: lang });
try {
const url = generateRequestUrl(text, { to: lang });
const resp = await fetch(url);
https.get(url, (resp) => {
let data = '';
if (!resp.ok) {
throw new Error(resp.statusText);
}
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
const result = normaliseResponse(JSON.parse(data));
console.log('Translated text: ' + result.text);
return response.send(result.text);
});
}).on("error", (err) => {
const result = normaliseResponse(await resp.json());
console.log('Translated text: ' + result.text);
return response.send(result.text);
} catch (err) {
console.log("Translation error: " + err.message);
return response.sendStatus(500);
});
}
});
function writeSecret(key, value) {