-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
95 lines (89 loc) · 2.58 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1 class="header">Email Newsletter Collection</h1>
<input
id="email_input"
type="email"
name="email"
placeholder="[email protected]"
/>
<input id="phone_input" type="tel" placeholder="+1 (234) 567-8901" />
<button id="submit_btn" type="button" onclick="submitEmail()">
Sign up
</button>
<a
href="sms:1-234-567-8901?body=Send%20this%20message%20to%20subscribe%20to%20marketing%20messages%20and%20receive%2015%25%20off!%20%5BRef%3A15off%5D"
>Launch Messages App</a
>
<script>
const safeTry = async fn => {
try {
const result = await fn()
return [null, result]
} catch (e) {
if (e instanceof Error) {
console.error(e.message)
} else if (typeof e === 'string') {
console.error(e)
} else {
console.error('An error occurred')
}
return [e, null]
}
}
const OoneSignalAppID = '<APP_ID>'
//Example starts here
const createUserAPI = `https://api.onesignal.com/apps/${OoneSignalAppID}/users`
async function submitPhone() {
const [error, _result] = await safeTry(() =>
fetch(createUserAPI, {
method: 'POST',
mode: 'no-cors',
body: JSON.stringify({
subscriptions: [
{
type: 'SMS',
token: document.getElementById('phone_input').value,
enabled: false, // Needed for double opt-in
},
],
}),
})
)
if (error) {
console.error('Failed to create user')
} else {
console.log('User created successfully')
}
}
async function submitEmail() {
const [error, _result] = await safeTry(() =>
fetch(createUserAPI, {
method: 'POST',
mode: 'no-cors',
body: JSON.stringify({
subscriptions: [
{
type: 'Email',
token: document.getElementById('email_input').value,
enabled: true,
},
],
}),
})
)
if (error) {
console.error('Failed to create user')
} else {
console.log('User created successfully')
}
}
</script>
</body>
</html>