Skip to content

Commit a7a5a3d

Browse files
authored
Feat/april 2024: add recipes (#25)
* feat: add recipe schedules * recipes: ensure preview text has a source link in the event its a recipe or other extremely long content - extract source link from notebody - and add source link to previewBody * update package.json * april-2024: add recipe schedules
1 parent 7402347 commit a7a5a3d

File tree

3 files changed

+129
-11
lines changed

3 files changed

+129
-11
lines changed

events.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,84 @@ dev:
1414
titlePrefix: '🧘🏾‍♀️ #YourWellness 💡'
1515

1616
prod:
17+
- schedule:
18+
rate: cron(0 7 ? * FRI *)
19+
name: 'Brekkie'
20+
description: 'Weekly Breakfast'
21+
input:
22+
onenoteSettings:
23+
notebookName: 'Recipes'
24+
sectionName: 'Brekkie Ideas'
25+
messageSettings:
26+
channelHandle: '@nyammingz'
27+
disablePreview: false
28+
titlePrefix: '#Brekkie'
29+
showEditLink: true
30+
- schedule:
31+
rate: cron(0 7 ? * FRI *)
32+
name: 'coles-dinner'
33+
description: 'Coles Dinner'
34+
input:
35+
onenoteSettings:
36+
notebookName: 'Recipes'
37+
sectionName: 'Coles Dinners'
38+
messageSettings:
39+
channelHandle: '@nyammingz'
40+
disablePreview: false
41+
titlePrefix: '#Dinner'
42+
showEditLink: true
43+
- schedule:
44+
rate: cron(0 7 1-7 * ? *)
45+
name: 'something-different-1'
46+
description: 'Something Different 1'
47+
input:
48+
onenoteSettings:
49+
notebookName: 'Recipes'
50+
sectionName: 'Dinners'
51+
messageSettings:
52+
channelHandle: '@nyammingz'
53+
disablePreview: false
54+
titlePrefix: '#Dinner'
55+
showEditLink: true
56+
- schedule:
57+
rate: cron(0 7 15-21 * ? *)
58+
name: 'something-different-2'
59+
description: 'Something Different 2'
60+
input:
61+
onenoteSettings:
62+
notebookName: 'Recipes'
63+
sectionName: 'Dinners'
64+
messageSettings:
65+
channelHandle: '@nyammingz'
66+
disablePreview: false
67+
titlePrefix: '#Dinner'
68+
showEditLink: true
69+
- schedule:
70+
rate: cron(0 7 8-14 * ? *)
71+
name: 'jamaican-dish-1'
72+
description: 'Jamaican Dish 1'
73+
input:
74+
onenoteSettings:
75+
notebookName: 'Recipes'
76+
sectionName: 'Jamaican'
77+
messageSettings:
78+
channelHandle: '@nyammingz'
79+
disablePreview: false
80+
titlePrefix: '#JamaicanDish'
81+
showEditLink: true
82+
- schedule:
83+
rate: cron(0 7 22-28 * ? *)
84+
name: 'jamaican-dish-2'
85+
description: 'Jamaican Dish 2'
86+
input:
87+
onenoteSettings:
88+
notebookName: 'Recipes'
89+
sectionName: 'Jamaican'
90+
messageSettings:
91+
channelHandle: '@nyammingz'
92+
disablePreview: false
93+
titlePrefix: '#JamaicanDish'
94+
showEditLink: true
1795
- schedule:
1896
rate: cron(0 7 1,15 * ? *)
1997
name: 'stay-in-touch'
@@ -34,6 +112,7 @@ prod:
34112
onenoteSettings:
35113
notebookName: 'P n B'
36114
sectionName: 'PCM Verses'
115+
isSequential: true
37116
messageSettings:
38117
channelHandle: '@notifyer_quotes'
39118
disablePreview: true
@@ -113,6 +192,7 @@ prod:
113192
onenoteSettings:
114193
notebookName: 'P n B'
115194
sectionName: 'PCM Prayers'
195+
isSequential: true
116196
messageSettings:
117197
channelHandle: '@bb_2nd_brain'
118198
disablePreview: true

lib/notify.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,55 @@ async function formatNoteBody(url) {
1818

1919
// remove images
2020
const el = wrapper.querySelector('div')
21+
2122
if (el) {
2223
for (let img of el.querySelectorAll('img')) {
2324
console.log("removing image: ", img.getAttribute("alt"))
2425
img.remove()
2526
}
26-
return htmlToMarkdown(el.toString())
27+
return {
28+
body: htmlToMarkdown(el.toString()),
29+
source: getSourceLink(el)
30+
}
2731
}
28-
return "";
32+
return { body: "", source: "" }
2933
})
3034
.catch(function (err) {
3135
console.error("formatNoteBody()", err)
32-
return ""
36+
return { body: "", source: "" }
3337
})
3438

3539
return body
3640
}
3741

38-
async function structureMessage(note, titlePrefix, isLogin = false) {
42+
/**
43+
* Get the source link from the note body
44+
* search el for text "Clipped from: " or "Source: "
45+
* if found, select the first href after that location
46+
*
47+
* @param {*} el
48+
* @returns
49+
*/
50+
const getSourceLink = (el) => {
51+
let text = el.innerText;
52+
let clippedFromIndex = text.indexOf('Clipped from: ');
53+
let sourceIndex = text.indexOf('Source: ');
54+
55+
let link;
56+
let links = el.querySelectorAll('a');
57+
for (let i = 0; i < links.length; i++) {
58+
let linkPosition = text.indexOf(links[i].innerText);
59+
if ((clippedFromIndex !== -1 && clippedFromIndex < linkPosition) ||
60+
(sourceIndex !== -1 && sourceIndex < linkPosition)) {
61+
link = links[i].getAttribute('href');
62+
break;
63+
}
64+
}
65+
return link ? `Source: ${htmlToMarkdown(link)}` : "";
66+
}
67+
68+
async function structureMessage(args) {
69+
const { note, titlePrefix, isLogin = false } = args;
3970
if (isLogin) {
4071
return {
4172
title: `Login`,
@@ -53,14 +84,16 @@ async function structureMessage(note, titlePrefix, isLogin = false) {
5384

5485
const imageUrl = links && links.previewImageUrl ? links.previewImageUrl.href : null
5586

56-
const body = await formatNoteBody(url);
87+
const content = await formatNoteBody(url);
5788

58-
const isMsgTooLong = (titlePrefix.length + title.length + body.length) > 4096 + 22
89+
const isMsgTooLong = (titlePrefix.length + title.length + content.body.length) > 4096 + 22
90+
91+
const previewBody = markdownv2.escape(previewText) + markdownv2.escape(`\n\n`) + content.source
5992

6093
return {
6194
prefix: titlePrefix,
6295
title,
63-
body: isMsgTooLong ? previewText : body,
96+
body: isMsgTooLong ? previewBody : content.body,
6497
type: 'link',
6598
url: noteLinks[oneNoteAppUrl].href,
6699
webUrl: noteLinks.oneNoteWebUrl.href,
@@ -76,7 +109,7 @@ async function structureMessage(note, titlePrefix, isLogin = false) {
76109
* @param {*} login - format note as device login dialog
77110
*/
78111
function push(note, icon = '🔒', login = false) {
79-
const msg = structureMessage(note, icon, login)
112+
const msg = structureMessage({note, titlePrefix: icon, isLogin: login})
80113

81114
return new Promise((resolve, reject) => {
82115
fetch
@@ -105,8 +138,13 @@ function push(note, icon = '🔒', login = false) {
105138
* @param {*} login - format note as device login dialog
106139
*/
107140
async function withTelegram(note, messageSettings, login = false) {
108-
const { titlePrefix, channelHandle = process.env.DEFAULT_TELEGRAM_CHANNEL, showEditLink = false, disablePreview = true } = messageSettings
109-
const msg = await structureMessage(note, titlePrefix, login)
141+
const {
142+
titlePrefix,
143+
channelHandle = process.env.DEFAULT_TELEGRAM_CHANNEL,
144+
showEditLink = false,
145+
disablePreview = true,
146+
} = messageSettings
147+
const msg = await structureMessage({ note, titlePrefix, isLogin: login })
110148

111149
let text = markdownv2.bold(markdownv2.escape(msg.prefix))
112150
text += markdownv2.escape(`\n\n`)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"app": "sls invoke local -f app",
88
"deploy": "sls deploy --stage prod",
9-
"_predeploy": "npm run remove",
9+
"predeploy": "npm run remove",
1010
"dev": "sls invoke local -f app --watch --path ./eventData.json",
1111
"offline": "sls offline start --port 4500",
1212
"refresh": "sls invoke local -f refresh",

0 commit comments

Comments
 (0)