-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwilio-function.js
126 lines (111 loc) · 4.04 KB
/
twilio-function.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
exports.handler = async function(context, event, callback) {
// Getting authenticated Twilio Client
const twilioClient = context.getTwilioClient();
//Setting up Appwrite Client
const appwriteSdk = require('node-appwrite');
const appwriteClient = new appwriteSdk.Client();
appwriteClient
.setEndpoint(context.APPWRITE_ENDPOINT)
.setProject(context.APPWRITE_PROJECT_ID)
.setKey('<APPWRITE_API_KEY>'); // Was unable to use the Twilio Functions's Environment Variables for this one because of the set max length of 255 characters
const db = new appwriteSdk.Databases(appwriteClient);
const dbId = context.APPWRITE_DB_ID;
const collectionId = context.APPWRITE_COLLECTION_ID;
// Setting up OpenAI Client
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: context.OPENAI_API_KEY
});
const twiml = new Twilio.twiml.MessagingResponse();
let incomingMessage = event.Body.trim();
let currentPhoneNumber = event.From.substring(9);
// Setting currentUser to help retain state of conversation
let currentUser = await db.listDocuments(
dbId,
collectionId,
[
appwriteSdk.Query.equal('phoneNumber', currentPhoneNumber)
]
);
if(currentUser.total == 1) {
currentUser = currentUser.documents[0];
}
else {
currentUser = await db.createDocument(
dbId,
collectionId,
appwriteSdk.ID.unique(),
{
phoneNumber: currentPhoneNumber,
lang: null,
text: null
}
);
}
if(!currentUser.text && !currentUser.lang) {
if(incomingMessage == 'Hello Twilio' || incomingMessage == 'yes') {
// Introductory message
twiml.message('Hi! Welcome to the Language Translator.\n\nPlease enter the language you want to translate to.');
return callback(null, twiml);
}
// Save target language
currentUser = await db.updateDocument(
dbId,
collectionId,
currentUser.$id,
{
phoneNumber: currentPhoneNumber,
lang: incomingMessage,
text: null
}
)
twiml.message('Please enter the text you want to translate.');
return callback(null, twiml);
}
else {
// Save text to translate
currentUser = await db.updateDocument(
dbId,
collectionId,
currentUser.$id,
{
phoneNumber: currentPhoneNumber,
lang: currentUser.lang,
text: incomingMessage
}
)
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: "system",
content: "You are a language translation assistant.",
},
{
role: "user",
content: `Translate the following text to ${currentUser.lang}: ${incomingMessage}\n\nReturn only the translated text in the response with no additional content surrounding it.`
}
]
});
const translatedText = response.choices[0].message.content.trim();
// Send translated text to user
await twilioClient.messages.create({
body: 'Here is the translated text',
from: 'whatsapp:+14155238886', // Your Twilio Sandbox number
to: event.From,
});
await twilioClient.messages.create({
body: translatedText,
from: 'whatsapp:+14155238886', // Your Twilio Sandbox number
to: event.From,
});
// Clear user state from Appwrite
await db.deleteDocument(
dbId,
collectionId,
currentUser.$id
);
twiml.message('If you would like to translate another text, please enter: yes');
return callback(null, twiml);
}
};