-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewsletter_draft.sh
More file actions
executable file
·154 lines (130 loc) · 4.83 KB
/
newsletter_draft.sh
File metadata and controls
executable file
·154 lines (130 loc) · 4.83 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
#
# Generate newsletter draft and write it to Confluence
#
# Required environment variables:
#
# - ARTICLE_LINKS (optional, auto-fetched by default)
# - NEWSLETTER_LINKS (optional, auto-fetched by default)
# - OPENROUTER_API_KEY
# - OPENROUTER_MODEL
# - OPENROUTER_PROMPT
# - CONFLUENCE_SPACE_KEY
# - CONFLUENCE_ANCESTOR_ID
# - CONFLUENCE_HOST
# - CONFLUENCE_PAT
set -euo pipefail
# Get relevant Blog post links and append them to `OPENROUTER_PROMPT`
ARTICLE_LINKS="${ARTICLE_LINKS:-$(
(
echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel>'
page=1
found=0
while [ $found -eq 0 ]; do
content=$(curl -s -L -A "Mozilla/5.0" "https://www.digitale-gesellschaft.ch/feed/?paged=$page")
[[ "$content" != *"<item>"* ]] && break
echo "$content" | sed -n '/<item>/,/<\/item>/p'
[[ "$content" =~ category.*Newsletter.*category ]] && found=1
((page++))
[ $page -gt 10 ] && break
done
echo '</channel></rss>'
) | \
xee xpath "string-join(//item[category='Newsletter'][1]/preceding-sibling::item[not(category='Zmittag')]/link, ' ')" | \
xargs -d '"' echo | \
xargs -n1
)}"
NEWSLETTER_LINKS="${NEWSLETTER_LINKS:-$(
curl -s -L -A "Mozilla/5.0" https://www.digitale-gesellschaft.ch/feed/?tag=newsletter | \
xee xpath "string-join(//item/link, ' ')" | \
xargs -d '"' echo | \
xargs -n1
)}"
OPENROUTER_PROMPT+=$'\n\n# Artikellinks\n\n'"$ARTICLE_LINKS"$'\n\n# Letzte 10 Newsletter\n\n'"$NEWSLETTER_LINKS"
# Fetch result from OpenRouter
echo "Fetching response from OpenRouter..."
OPENROUTER_PAYLOAD=$(jq -n \
--arg model "$OPENROUTER_MODEL" \
--arg prompt "$OPENROUTER_PROMPT" \
'{
model: $model,
messages: [{role: "user", content: $prompt}]
}')
OPENROUTER_RESPONSE=$(curl -s -X POST "https://openrouter.ai/api/v1/chat/completions" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d "$OPENROUTER_PAYLOAD")
CONTENT=$(echo "$OPENROUTER_RESPONSE" | jq -r '.choices[0].message.content')
if [ -z "$CONTENT" ] || [ "$CONTENT" == "null" ]; then
echo "Error: Failed to get content from OpenRouter"
echo "Response: $OPENROUTER_RESPONSE"
exit 1
fi
echo "Successfully received content from OpenRouter."
# Normalize Confluence host trailing slash
CONFLUENCE_HOST="${CONFLUENCE_HOST%/}"
# Prepare Confluence page title
## Calculate the 3rd Wednesday of the current month (always between the 15th and 21st)
YEAR_MONTH=$(date +"%Y-%m")
for d in {15..21}; do
if [ "$(date -d "$YEAR_MONTH-$d" +%u)" -eq 3 ]; then
DATE="$YEAR_MONTH-$d"
break
fi
done
BASE_TITLE="«Update» $DATE"
TITLE="$BASE_TITLE"
COUNTER=1
## Add model ID to title if manually triggered run
if [[ $GITHUB_EVENT_NAME == "workflow_dispatch" ]] ; then
TITLE+=" | $OPENROUTER_MODEL"
fi
## Add model ID with serial nr to title if it already exists
echo "Checking if page with title '$TITLE' already exists..."
while true; do
CHECK_RESPONSE=$(curl -s -L -X GET "${CONFLUENCE_HOST}/rest/api/content?title=$(echo -n "$TITLE" | jq -sRr @uri)&spaceKey=$CONFLUENCE_SPACE_KEY" \
-H "Authorization: Bearer $CONFLUENCE_PAT")
if [[ $(echo "$CHECK_RESPONSE" | jq '.size') -gt 0 ]]; then
TITLE="$BASE_TITLE | $OPENROUTER_MODEL ($COUNTER)"
echo "Title already exists. Trying '$TITLE'..."
COUNTER=$((COUNTER + 1))
else
break
fi
done
# Convert result to HTML and write to new Confluence page
BODY_HTML=$(echo "$CONTENT" | pandoc --from=markdown --to=html --wrap=none)
echo "Creating new Confluence wiki page: $TITLE"
CONFLUENCE_PAYLOAD=$(jq -n \
--arg title "$TITLE" \
--arg space_key "$CONFLUENCE_SPACE_KEY" \
--arg ancestor_id "$CONFLUENCE_ANCESTOR_ID" \
--arg val "$BODY_HTML" \
'{
type: "page",
title: $title,
space: {
key: $space_key
},
ancestors: [{id: $ancestor_id}],
body: {
storage: {
value: $val,
representation: "storage"
}
}
}')
CONFLUENCE_RESPONSE=$(curl -s -L -X POST "${CONFLUENCE_HOST}/rest/api/content" \
-H "Authorization: Bearer $CONFLUENCE_PAT" \
-H "Content-Type: application/json" \
-d "$CONFLUENCE_PAYLOAD")
CONFLUENCE_PAGE_LINK=$(echo "$CONFLUENCE_RESPONSE" | jq -r '._links.base + ._links.webui')
if [[ "$CONFLUENCE_PAGE_LINK" == *"null"* ]]; then
echo "Error: Failed to create Confluence page"
echo "Response: $CONFLUENCE_RESPONSE"
exit 1
fi
echo "Confluence wiki page created successfully: $CONFLUENCE_PAGE_LINK"
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
echo "Erstellte Wiki-Seite: [**$TITLE**]($CONFLUENCE_PAGE_LINK)" >> "$GITHUB_STEP_SUMMARY"
fi