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
import axios from "npm:axios@1.7.7";
import * as cheerio from "npm:cheerio@1.0.0";
const onesignalToken = Deno.env.get("ONESIGNAL_TOKEN");
const segmentAppID = Deno.env.get("SEGMENT_APPID");
const defaultSegment = [Deno.env.get("DEFAULT_ONESIGNAL_SEGMENT")];
const onesignalURL = "https://onesignal.com/api/v1/notifications";
/**
* Send a notification to OneSignal
*
* @param heading - Main heading of the notification
* @param message - Notification body text
* @param url - Optional url to attach to the notifications
*/
export default async (heading: string, message, url = "", segment = defaultSegment) => {
const body = {
app_id: segmentAppID,
included_segments: segment,
headings: { en: heading },
contents: { en: message },
web_url: url,
};
const auth = {
headers: {
Authorization: `Basic ${onesignalToken}`,
},
};
try {
await axios.post(onesignalURL, body, auth);
} catch (error) {
console.error(error.response["data"]["errors"]);
throw error;
}
};