-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (79 loc) · 2.22 KB
/
server.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
import express from 'express';
import { config } from 'dotenv';
import OpenAI from 'openai';
// Load environment variables
config();
// Create a web server
const app = express();
const port = process.env.PORT || 3034;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
// Add middleware to parse JSON bodies
app.use(express.json());
// Initialize OpenAI API
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
app.get('/webhooks', (req, res) => {
if (
req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === '1234'
) {
res.send(req.query['hub.challenge']);
} else {
res.sendStatus(400);
}
});
app.post('/webhooks', async (req, res) => {
const body = req.body.entry[0].changes[0];
if (body.field !== 'messages') {
// not from the messages webhook so dont process
return res.sendStatus(200);
}
if (!body.value.messages) {
return res.sendStatus(200);
}
const text = body.value.messages
.map((message) => message.text.body)
.join('\n\n');
console.log(text);
const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: text }],
});
// Extract the response from the OpenAI completion
const aiResponse = completion.choices[0].message.content;
// Extract the phone number from the incoming message
const phoneNumber = body.value.messages[0].from;
// Prepare the message payload
const messagePayload = {
messaging_product: 'whatsapp',
to: phoneNumber,
type: 'text',
text: {
body: aiResponse,
},
};
// Send the response back to WhatsApp
try {
const response = await fetch(
`https://graph.facebook.com/v20.0/${process.env.PHONE_NUMBER_ID}/messages`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SYSTEM_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(messagePayload),
},
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log('Message sent successfully');
} catch (error) {
console.error('Failed to send message:', error);
}
res.sendStatus(200);
});