-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
44 lines (40 loc) · 1.03 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
const twilio = require('twilio');
const openai = require('openai');
// Set up OpenAI API client
openai.apiKey = "YOUR_OPENAI_API_KEY";
// Set up Twilio client
const client = twilio(
"YOUR_TWILIO_ACCOUNT_SID",
"YOUR_TWILIO_AUTH_TOKEN"
);
// Set up a webhook to listen for incoming messages
app.post('/webhook', (req, res) => {
const message = req.body.Body;
// Use the GPT-3 model to generate a response to the message
openai.completions.create({
engine: "text-davinci-002",
prompt: message,
max_tokens: 2048,
temperature: 0.7,
}, (error, response) => {
if (error) {
console.log(error);
res.sendStatus(500);
} else {
// Send the response back to the user
client.messages
.create({
body: response.choices[0].text,
from: "YOUR_TWILIO_PHONE_NUMBER",
to: req.body.From
})
.then(() => {
res.sendStatus(200);
})
.catch((error) => {
console.log(error);
res.sendStatus(500);
});
}
});
});