-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (55 loc) · 1.91 KB
/
index.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
const fetch = require('node-fetch');
require('dotenv').config();
const qs = require("querystring")
const accountSid = process.env["TWILIO_ACCOUNT_SID"];
const authToken = process.env["TWILIO_AUTH_TOKEN"];
const TwilioNumber = process.env["TWILIO_NUMBER"];
const MobileNumberToSendTranslation = '';
const translationBaseURL = 'https://api.funtranslations.com/translate/yoda?text=';
const client = require('twilio')(accountSid, authToken);
module.exports = async function (context, req) {
// context.log(accountSid);
// context.log(authToken);
try {
context.log('JavaScript HTTP trigger function processed a request.');
const formValues = qs.parse(req.body);
//context.log("formValues: " + formValues);
const textToTransLate = formValues.Body;
context.log("Text to Translate: " + textToTransLate);
var responseMessage = "Input Text: " + textToTransLate;
await fetch(translationBaseURL + textToTransLate)
.then(response => response.json())
.then(jsonResponse => {
context.log(jsonResponse);
responseMessage = jsonResponse;
});
try {
if (responseMessage.error != null)
{
textMessage = responseMessage.error.message;
}
else
{
textMessage = responseMessage.contents.translation;
}
} catch (error) {
context.log(error);
}
context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage
};
// Uncomment the following code to additionally send translations to any mobile number
/*
client.messages
.create({
body: textMessage,
from: TwilioNumber,
to: MobileNumberToSendTranslation,
})
.then(message => context.log(message.sid));
*/
} catch (error) {
context.log(error);
}
}