Skip to content
This repository was archived by the owner on May 2, 2021. It is now read-only.

Commit 8b25d55

Browse files
committed
new event notification
1 parent 7529a38 commit 8b25d55

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed
Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
1-
// const axios = require('axios');
2-
3-
// const { ONE_SIGNAL_API_KEY, ONE_SIGNAL_APP_ID } = process.env;
4-
5-
exports.handler = function (event) {
6-
console.log(event);
7-
// axios.post(
8-
// 'https://onesignal.com/api/v1/notifications',
9-
// {
10-
// included_segments: ['Active Users'],
11-
// app_id: ONE_SIGNAL_APP_ID,
12-
// headings: {
13-
// en: 'Pubblicato un nuovo evento!',
14-
// },
15-
// contents: {
16-
// en: "Titolo dell'evento",
17-
// },
18-
// },
19-
// {
20-
// headers: { Authorization: `Basic ${ONE_SIGNAL_API_KEY}` },
21-
// },
22-
// );
23-
return {
1+
const axios = require('axios');
2+
3+
const { ONE_SIGNAL_API_KEY, ONE_SIGNAL_APP_ID } = process.env;
4+
5+
exports.handler = async function ({ body = '{}' }) {
6+
const { payload = {} } = JSON.parse(body);
7+
8+
const ret = {
249
statusCode: 200,
2510
};
11+
12+
if (payload.branch !== 'master') {
13+
return ret;
14+
}
15+
16+
const { data } = await axios.get(
17+
`https://api.github.com/repos/fevrcoding/fevrcoding.github.io/commits/${payload.commit_ref}`,
18+
);
19+
20+
const [, eventPath] =
21+
data.commit.message.match(/fevrcoding\/cms\/event\/(.+?)\n/) || [];
22+
23+
if (!eventPath) {
24+
return ret;
25+
}
26+
27+
const eventFile = data.files.find(
28+
({ filename, status }) =>
29+
status === 'added' && filename.includes(eventPath),
30+
);
31+
32+
if (!eventFile) {
33+
return ret;
34+
}
35+
36+
const { data: eventTxt } = await axios.get(eventFile.raw_url, {
37+
responseType: 'text',
38+
});
39+
40+
const [, eventTitle] = eventTxt.match(/title:(.+?)\n/) || [];
41+
42+
if (!eventTitle) {
43+
return ret;
44+
}
45+
46+
axios.post(
47+
'https://onesignal.com/api/v1/notifications',
48+
{
49+
included_segments: ['Active Users'],
50+
app_id: ONE_SIGNAL_APP_ID,
51+
headings: {
52+
en: 'Nuovo evento!',
53+
},
54+
contents: {
55+
en: eventTitle,
56+
},
57+
web_url: 'https://www.fevr.it/',
58+
},
59+
{
60+
headers: { Authorization: `Basic ${ONE_SIGNAL_API_KEY}` },
61+
},
62+
);
63+
return ret;
2664
};

0 commit comments

Comments
 (0)