diff --git a/Node/send-notification.Node/README.md b/Node/send-notification.Node/README.md new file mode 100644 index 0000000000000000000000000000000000000000..558b19f3f7c16b869a50521ac27b719bbf342bcc --- /dev/null +++ b/Node/send-notification.Node/README.md @@ -0,0 +1,4 @@ +# send-notification Python script + +- Fill ```channelId``` and ```notifications_bearer``` from Notification service information + diff --git a/Node/send-notification.Node/send-notifications.js b/Node/send-notification.Node/send-notifications.js new file mode 100644 index 0000000000000000000000000000000000000000..fcb0ac85df7c5c6698a046f9b3b92b25cb83e9f1 --- /dev/null +++ b/Node/send-notification.Node/send-notifications.js @@ -0,0 +1,53 @@ +const https = require('https'); + +// Target Notification channel +const notifications_sendapi = "https://api-notifications.app.cern.ch/notifications"; +const notifications_bearer = "fill-me with Channel API Key"; +const channelId = "fill-me with ChannelID"; + +// Notification json sample +// { +// "notification": { +// "target": "...", +// "summary": "Notification title", +// "priority": "LOW|NORMAL|IMPORTANT", +// "body": "Notification content", +// "link": if any, +// "imgurl": if any, +// } +// } + +// Build json and post to notifications service +const message = JSON.stringify({ + "notification": { + "body": "<p>Message content</p>", + "summary": "This is a test notification", + "target": channelId, + "priority": 'NORMAL', + "link": "any alternate url you might want to provide", + } +}); + +const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': message.length, + 'Authorization': 'Bearer ' + notifications_bearer, + } +}; + +const req = https.request(notifications_sendapi, options, res => { + console.log(`statusCode: ${res.statusCode}`); + + res.on('data', d => { + process.stdout.write(d); + }) +}) + +req.on('error', error => { + console.error(error); +}) + +req.write(message); +req.end(); \ No newline at end of file diff --git a/Powershell/rss-to-notifications.Powershell/README.md b/Powershell/rss-to-notifications.Powershell/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f0a43176322a0b2d49208ea39f62df867af5976d --- /dev/null +++ b/Powershell/rss-to-notifications.Powershell/README.md @@ -0,0 +1,7 @@ +# rss-to-notification Powershell script + +- Edit ```$FeedList```, an array of RSS sources. +- Edit ```$datafile``` path +- Fill ```channelId``` and ```notifications_bearer``` from Notification service information +- In the loop, an old SSO automatic auth with kerberos is provided (commented out by default) + diff --git a/Powershell/rss-to-notifications.Powershell/rss-to-notifications.ps1 b/Powershell/rss-to-notifications.Powershell/rss-to-notifications.ps1 new file mode 100644 index 0000000000000000000000000000000000000000..e74124f33c44c868c1c7a1b0fa03bfffed3c0e83 --- /dev/null +++ b/Powershell/rss-to-notifications.Powershell/rss-to-notifications.ps1 @@ -0,0 +1,88 @@ +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +# Array of source RSS Feeds URIs and their sender name (optional) +$FeedList = @( + [PSCustomObject]@{Sender="CERN Home News";Uri="https://home.cern/api/news/feed.rss";} +) + +# Datafile used to remember which posts were already synced +# Full path is needed +$datafile = "C:\Program Files\CERN\RSS-To-Notifications\rss-to-notifications.dat" + +# Target Notification channel +$notifications_sendapi = "https://api-notifications.app.cern.ch/notifications" +$notifications_bearer = "fill-me with Channel API Key" +$channelId = "fill-me with ChannelID" + +# Processing all the source RSS +Foreach ($OneFeed in $FeedList) +{ + # Anonymous access + # use read below to deal with accents, ok with Drupal + [net.httpwebrequest]$httpwebrequest = [net.webrequest]::create($OneFeed.Uri) + [net.httpWebResponse]$httpwebresponse = $httpwebrequest.getResponse() + $reader = new-object IO.StreamReader($httpwebresponse.getResponseStream()) + [xml]$Content = $reader.ReadToEnd() + $reader.Close() + + # Authenticate with OLD SSO (ADFS) using current kerberos token + # $r = Invoke-Webrequest -uri $OneFeed.Uri -UserAgent "curl-sso-kerberos/CERN-SSO-Cookie-Powershell" -SessionVariable bidule -UseDefaultCredentials + # try { + # $wppage = Invoke-Webrequest -uri $r.Forms[0].Action -Body $r.Forms[0] -Method 'POST' -SessionVariable bidule2 -ErrorAction Ignore | out-null + # } catch {} + # $cookies = $bidule2.Cookies.GetCookies($r.Forms[0].Action) + # $wppage = Invoke-Webrequest -uri $OneFeed.Uri -WebSession $bidule2 + + $Feed = $Content.rss.channel + + # No sorting, process latest first + #ForEach ($msg in $Feed.Item) + # Sort by pubDate, works for example on Drupal feeds + $tmparray = $Feed.Item | Sort-Object { $_.pubDate -as [datetime] } + ForEach ($msg in $tmparray) + { + # Extract item guid to check if alreayd synced + $guid = $msg.guid.'#text' + if ($guid -eq $null) { + $guid = $msg.guid + } + + $alreadyposted = $null + $alreadyposted = Select-String $guid -path $datafile + + if ($alreadyposted -eq $null) + { + "Posting message $guid" + # Notification json sample + # { + # "notification": { + # "target": "...", + # "summary": "Notification title", + # "priority": "LOW|NORMAL|IMPORTANT", + # "body": "Notification content", + # "link": if any, + # "imgurl": if any, + # } + # } + + # One can try to extract a feature image and fill it's url in imgurl property + # But the image must be accessible anonymously to render fine in the notification list + + # Build json and post to notifications service + # body comes from description field, on some RSS like Wordpress description is a summary, fully body is body=$msg.encoded.InnerText + $Notification = @{ target=$channelId; summary=$msg.Title; priority='NORMAL'; body=$msg.description; link=$msg.link; } + $Payload = @{ notification=$Notification} + $headers = @{Authorization = "Bearer $notifications_bearer"} + Invoke-RestMethod -Uri $notifications_sendapi -Method Post -ContentType 'application/json;charset=utf-8' -Headers $headers -Body (ConvertTo-Json $Payload) + + # Saves which entries are posted + Add-Content $datafile $guid + } + else + { + "Already posted." + } + } + +} + diff --git a/Python/rss-to-notifications.Python/README.md b/Python/rss-to-notifications.Python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..36c34684b39bfd9801988e55cd36bd9762754b7a --- /dev/null +++ b/Python/rss-to-notifications.Python/README.md @@ -0,0 +1,8 @@ +# rss-to-notification Python script + +- install pre-requisites + - ```pip install feedparser``` +- Edit ```$FeedList```, an array of RSS sources. +- Edit ```$datafile``` path +- Fill ```channelId``` and ```notifications_bearer``` from Notification service information + diff --git a/Python/rss-to-notifications.Python/rss-to-notifications.py b/Python/rss-to-notifications.Python/rss-to-notifications.py new file mode 100644 index 0000000000000000000000000000000000000000..5213f9ea4b64556af58b50065b0deeb26762e620 --- /dev/null +++ b/Python/rss-to-notifications.Python/rss-to-notifications.py @@ -0,0 +1,74 @@ +import feedparser +import requests +import os.path + +# Array of source RSS Feeds URIs +FeedList = ["https://home.cern/api/news/feed.rss"] + +# Datafile used to remember which posts were already synced +datafile = "rss-to-notifications.dat" + +# Target Notification channel +notifications_sendapi = "https://api-notifications.app.cern.ch/notifications" +notifications_bearer = "fill-me with Channel API Key" +channelId = "fill-me with ChannelID" + +header = {'Authorization': 'Bearer ' + notifications_bearer} + +for OneFeed in FeedList: + Feed = feedparser.parse(OneFeed) + #entry = Feed.entries[1] + + for msg in Feed.entries: + + # Extract item guid to check if already synced + guid = msg.guid + if not guid: + print("Missing GUID skipping item or it will sync forever") + continue + + alreadyposted = False + if os.path.isfile(datafile): + with open(datafile) as file: + if any(line.rstrip('\n') == guid for line in file): + alreadyposted = True + + if not alreadyposted: + print("Posting message ", guid) + # Notification json sample + # { + # "notification": { + # "target": "...", + # "summary": "Notification title", + # "priority": "LOW|NORMAL|IMPORTANT", + # "body": "Notification content", + # "link": if any, + # "imgurl": if any, + # } + # } + + # One can try to extract a feature image and fill it's url in imgurl property + # But the image must be accessible anonymously to render fine in the notification list + + # Build json and post to notifications service + message = { + "notification": { + "body": msg.description, + "summary": msg.title, + "target": channelId, + "priority": 'NORMAL', + "link": msg.link, + } + } + #print(message) + response = requests.post(notifications_sendapi, json=message, headers=header) + #print(response) + #print(response.json()) + + # Saves which entries are posted + file_object = open(datafile, 'a') + file_object.write(guid + '\n') + file_object.close() + else: + print("Already posted.") + diff --git a/Python/send-notification.Python/README.md b/Python/send-notification.Python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..558b19f3f7c16b869a50521ac27b719bbf342bcc --- /dev/null +++ b/Python/send-notification.Python/README.md @@ -0,0 +1,4 @@ +# send-notification Python script + +- Fill ```channelId``` and ```notifications_bearer``` from Notification service information + diff --git a/Python/send-notification.Python/send-notifications.py b/Python/send-notification.Python/send-notifications.py new file mode 100644 index 0000000000000000000000000000000000000000..23d1518c311b00e68da859785eeaa9d0073d8703 --- /dev/null +++ b/Python/send-notification.Python/send-notifications.py @@ -0,0 +1,35 @@ +import requests + +# Target Notification channel +notifications_sendapi = "https://api-notifications.app.cern.ch/notifications" +notifications_bearer = "fill-me with Channel API Key" +channelId = "fill-me with ChannelID" + +header = {'Authorization': 'Bearer ' + notifications_bearer} + +# Notification json sample +# { +# "notification": { +# "target": "...", +# "summary": "Notification title", +# "priority": "LOW|NORMAL|IMPORTANT", +# "body": "Notification content", +# "link": if any, +# "imgurl": if any, +# } +# } + +# Build json and post to notifications service +message = { + "notification": { + "body": "<p>Message content</p>", + "summary": "This is a test notification", + "target": channelId, + "priority": 'NORMAL', + "link": "any alternate url you might want to provide", + } +} +#print(message) +response = requests.post(notifications_sendapi, json=message, headers=header) +#print(response) +#print(response.json()) diff --git a/README.md b/README.md index de53e5ccb4628c9775f338c5425856d0311bce8f..9b86b39611e7424c2ad51beadee6dfdf822bd20c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ -# notifications-samples +# Script examples to integrate with CERN Notifications service +A list of different scripts to interact with the CERN Notifications service can be found here. +Disclaimer: these scripts are provided as a kick-start and helper + +### Examples + +- send-notification: sending a notification via the REST API +- rss-to-notifications: sync RSS feed(s) to a Notification Channel via the API. + +### Contribute + +If you have worked on other integration scripts or implemented the current examples in different languages please share them with us via a Merge Request. diff --git a/curl/send-notification.script/README.md b/curl/send-notification.script/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e504115632b04224db4ef76c651a837bc21f15fa --- /dev/null +++ b/curl/send-notification.script/README.md @@ -0,0 +1,5 @@ +# send-notification shell script + +- Fill ```channelId``` in ```notification.json``` with the Channel ID +- Fill ```bearer``` in ```sendnotification.sh``` with the Channel API Key + diff --git a/curl/send-notification.script/notification.json b/curl/send-notification.script/notification.json new file mode 100644 index 0000000000000000000000000000000000000000..cef9afec1a1bb46d650a300178f85a4bc2ac490d --- /dev/null +++ b/curl/send-notification.script/notification.json @@ -0,0 +1,3 @@ +{ +"notification": {"target":"fill-me-with-channelID","summary":"Notification title","priority":"NORMAL","body":"<p>This is a notification sent by curl script</p>\n"} +} diff --git a/curl/send-notification.script/sendnotification.sh b/curl/send-notification.script/sendnotification.sh new file mode 100644 index 0000000000000000000000000000000000000000..30e060ef63708d82e33362201d1504ab51daaaf4 --- /dev/null +++ b/curl/send-notification.script/sendnotification.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +curl -X POST https://api-notifications.app.cern.ch/notifications/notifications/apisend \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer fill-me-with-APIKey" \ + --data '@notification.json' + +