Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active July 26, 2023 05:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aetos382/839ef2fc5e5e6e09a9c2ee934d3eb7b6 to your computer and use it in GitHub Desktop.
Save aetos382/839ef2fc5e5e6e09a9c2ee934d3eb7b6 to your computer and use it in GitHub Desktop.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', 'AppPassword')]
param(
[Parameter(Mandatory)]
[string] $AccountHandle,
[Parameter(Mandatory)]
[string] $AppPassword)
function New-HashTag {
param(
[Parameter(Mandatory)]
[string] $Text)
$hashedText = '#{0}' -f $Text
$params = @{
Text = $hashedText
Url = 'https://bsky.app/search?q={0}' -f ([System.Net.WebUtility]::UrlEncode($Text))
}
return New-Link @params
}
function New-Link {
param(
[Parameter(Mandatory)]
[string] $Text,
[Parameter(Mandatory)]
[uri] $Url)
$result = [PSCustomObject] @{
Text = $Text
Url = $Url
}
$result | Add-Member -TypeName 'Link'
return $result
}
function New-Record {
param(
[Parameter(Mandatory)]
[PSObject[]] $Components,
[datetime] $CreatedAt = (Get-Date -AsUTC))
$texts = @()
$facets = @()
$offset = 0
$encoding = [System.Text.Encoding]::UTF8;
foreach ($component in $Components) {
if ($component -is [string]) {
$texts += $component
$offset += $encoding.GetByteCount($component)
}
elseif ($component -is [PSCustomObject] -and $component.PSTypeNames -contains 'Link') {
$length = $encoding.GetByteCount($component.Text)
$facets += [PSCustomObject] @{
'$type' = 'app.bsky.richtext.facet'
index = @{
byteStart = $offset
byteEnd = $offset + $length
}
features = @(
@{
'$type' = 'app.bsky.richtext.facet#link'
uri = $component.Url
}
)
}
$texts += $component.Text
$offset += $length
}
}
$record = [PSCustomObject] @{
'$type' = 'app.bsky.feed.post'
text = $texts -join ''
createdAt = (Get-Date -Date $CreatedAt -AsUTC -UFormat '%FT%T')
facets = $facets
}
return $record
}
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$createSessionBody = @{
identifier = $AccountHandle
password = $AppPassword
} | ConvertTo-Json -Depth 100
$createSessionResponse = Invoke-RestMethod `
-Uri 'https://bsky.social/xrpc/com.atproto.server.createSession' `
-Method Post `
-ContentType 'application/json' `
-Body $createSessionBody
$createSessionResponse
$did = $createSessionResponse.did
$secureToken = ($createSessionResponse.accessJwt | ConvertTo-SecureString -AsPlainText -Force)
$veryLongText = 'Blueskyの文字数制限を突破するようなすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごくすごく長い文字列'
$charCountUrl = 'https://aetos382.github.io/char-counter/?w={0}' -f ([System.Net.WebUtility]::UrlEncode($veryLongText))
$record = New-Record -Components @(
'ひょっとしてAPIを使えば',
(New-HashTag -Text 'ハッシュタグ'),
'とか',
(New-Link -Text 'すごく長いURLへのリンク' -Url $charCountUrl),
'とかが使えるのでは?'
)
$createRecordBody = @{
did = $did
repo = $AccountHandle
collection = 'app.bsky.feed.post'
record = $record
} | ConvertTo-Json -Depth 100 -EscapeHandling EscapeNonAscii
$createRecordBody
$createRecordResponse = Invoke-RestMethod `
-Uri 'https://bsky.social/xrpc/com.atproto.repo.createRecord' `
-Method Post `
-ContentType 'application/json' `
-Authentication Bearer `
-Token $secureToken `
-Body $createRecordBody
$createRecordResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment