This repository was archived by the owner on Aug 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
64 lines (56 loc) · 1.79 KB
/
worker.js
File metadata and controls
64 lines (56 loc) · 1.79 KB
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
//
// worker.js
//
// Cloudflare worker
//
// Created by Jacob on 6/28/25.
//
eexport default {
//POST check
async fetch(request, env, ctx) {
if (request.method !== 'POST') {
return new Response('Only POST requests are allowed', { status: 405 });
}
// Check API key
const apiKey = request.headers.get("x-api-key");
if (apiKey !== env.API_KEY) {
return new Response('Unauthorized: Invalid API key', { status: 401 });
}
// Parse request body
let phone;
try {
const body = await request.json();
phone = body.phone;
if (!phone || !/^\d{10}$/.test(phone)) {
return new Response('Invalid phone number. Must be 10 digits.', { status: 400 });
}
} catch (err) {
return new Response('Invalid JSON payload.', { status: 400 });
}
// Prepare Twilio SMS
const messageBody = 'Join our beta with this TestFlight link: https://testflight.apple.com/join/abc123';
const payload = new URLSearchParams({
To: `+1${phone}`,
From: env.TWILIO_FROM,
Body: messageBody,
});
const auth = btoa(`${env.TWILIO_SID}:${env.TWILIO_AUTH_TOKEN}`);
// Send SMS via Twilio
const response = await fetch(
`https://api.twilio.com/2010-04-01/Accounts/${env.TWILIO_SID}/Messages.json`,
{
method: 'POST',
headers: {
Authorization: `Basic ${auth}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: payload,
}
);
if (!response.ok) {
const error = await response.text();
return new Response(`Failed to send SMS: ${error}`, { status: 500 });
}
return new Response('SMS sent successfully!', { status: 200 });
},
};