Skip to content

Instantly share code, notes, and snippets.

@jasonchester
Created March 19, 2019 17:47
Show Gist options
  • Save jasonchester/015b348b359d71dae7201ba0fad49728 to your computer and use it in GitHub Desktop.
Save jasonchester/015b348b359d71dae7201ba0fad49728 to your computer and use it in GitHub Desktop.
function Get-Als () {
Add-Type -AssemblyName System.Web
$query = [System.Web.HttpUtility]::UrlEncode( @'
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?human ?humanLabel ?humanDescription ?image ?linkcount ?date_of_birth ?date_of_death WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?human wdt:P31 wd:Q5.
?human wdt:P735 wd:Q18916867.
?human wdt:P18 ?image.
?human wikibase:sitelinks ?linkcount.
OPTIONAL { ?human wdt:P569 ?date_of_birth. }
OPTIONAL { ?human wdt:P570 ?date_of_death. }
FILTER(?date_of_birth >= "1921-01-01T00:00:00Z"^^xsd:dateTime)
}
ORDER BY DESC(?linkcount)
LIMIT 200
'@
)
# $q2 = 'SELECT%20%3Fhuman%20%3FhumanLabel%20%3FhumanDescription%20%3Fimage%20%3Flinkcount%20%3Fdate_of_birth%20%3Fdate_of_death%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%20%20%3Fhuman%20wdt%3AP31%20wd%3AQ5.%0A%20%20%3Fhuman%20wdt%3AP735%20wd%3AQ18916867.%0A%20%20%3Fhuman%20wdt%3AP18%20%3Fimage.%0A%20%20%3Fhuman%20wikibase%3Asitelinks%20%3Flinkcount.%0A%20%20OPTIONAL%20%7B%20%3Fhuman%20wdt%3AP569%20%3Fdate_of_birth.%20%7D%0A%20%20OPTIONAL%20%7B%20%3Fhuman%20wdt%3AP570%20%3Fdate_of_death.%20%7D%0A%20%20FILTER(%3Fdate_of_birth%20%3E%3D%20%221921-01-01T00%3A00%3A00Z%22%5E%5Exsd%3AdateTime)%0A%20%20OPTIONAL%20%7B%20%20%7D%0A%20%20OPTIONAL%20%7B%20%20%7D%0A%7D%0AORDER%20BY%20DESC(%3Flinkcount)%0ALIMIT%20200'
$als = Invoke-RestMethod -Method Get -Headers @{ Accept = 'application/sparql-results+json'} -Uri "https://query.wikidata.org/sparql?query=$query"
Write-Output ($als.results.bindings | Select-Object -Property @{n = 'humanLink'; e = {$_.human.value}},
@{n = 'humanLabel'; e = {$_.humanLabel.value}},
@{n = 'humanDescription'; e = {$_.humanDescription.value}},
@{n = 'image'; e = {$_.image.value}},
@{n = 'date_of_birth'; e = {$_.date_of_birth.value}},
@{n = 'date_of_death'; e = {$_.date_of_death.value}}
)
}
function GetThumb ([string] $uri) {
[System.Net.HttpWebRequest]::Create($uri + "?width=300px").GetResponse().ResponseUri.AbsoluteUri
}
$randomAl = Get-Als | Get-Random
$adaptiveBody = $randomAl | ForEach-Object{
@"
{
"`$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"size": "large",
"text": "$(if ($_.date_of_death) {'My name was Al'} else {'My name is Al'})"
},
{
"type": "ColumnSet",
"columns": [
{
"width": "auto",
"items": [
{
"type": "Image",
"width": "200px",
"url": "$(GetThumb($_.image))"
}
]
},
{
"width": "stretch",
"spacing": "padding",
"items": [
{
"type": "Container",
"height": "stretch",
"items": [
{
"type": "TextBlock",
"spacing": "none",
"text": "[$($_.humanLabel)]($($_.humanLink))"
},
{
"type": "TextBlock",
"spacing": "none",
"text": "$($_.humanDescription)"
},
{
"type": "TextBlock",
"spacing": "none",
"text": "($($_.date_of_birth | Get-Date -Format 'MMMM dd, yyyy') - $(if($_.date_of_death) {$_.date_of_death | Get-Date -Format 'MMMM dd, yyyy'}))"
}
]
}
]
}
]
}
]
}
"@
}
$messageBody = $randomAl | ForEach-Object{ @"
{
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": "$(if ($_.date_of_death) {'My name was Al'} else {'My name is Al'})",
"themeColor": "0078D7",
"title": "$(if ($_.date_of_death) {'My name was Al'} else {'My name is Al'})",
"sections": [
{
"text": "![$($_.humanLabel)]($(GetThumb($_.image)))"
},
{
"activityTitle": "$($_.humanLabel) ($($_.date_of_birth | Get-Date -Format 'MMMM dd, yyyy') - $(if($_.date_of_death) {$_.date_of_death | Get-Date -Format 'MMMM dd, yyyy'}))",
"activitySubtitle": "$($_.humanDescription)"
}
]
}
"@
}
$roomUri = 'https://outlook.office.com/webhook/.....'
Invoke-RestMethod -uri $roomUri -Method Post -body $messageBody -ContentType 'application/json'
Write-Output $messageBody
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment