-
Notifications
You must be signed in to change notification settings - Fork 0
/
post2bsky.ps1
113 lines (99 loc) · 2.81 KB
/
post2bsky.ps1
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
param (
[string]$Message
)
try
{
$ErrorActionPreference = 'Stop';
$Error.Clear();
$verbose = $env:VERBOSE
$Identifier = $env:BLUESKY_IDENTIFIER
$ApiKey = $env:BLUESKY_API_KEY
$baseUri = 'https://bsky.social'
$protocol = 'xrpc'
$createSession = 'com.atproto.server.createSession'
$createRecord = 'com.atproto.repo.createRecord'
$CreateSessionUri = "$($baseUri)/$($protocol)/$($createSession)"
$CreateRecordUri = "$($baseUri)/$($protocol)/$($createRecord)"
if ($verbose.ToLower() -eq 'verbose')
{
Write-Host "Post2BlueSkey DEBUG"
Write-Host "CreateSessionUri : $($CreateSessionUri)"
Write-Host "CreateRecordUri : $($CreateRecordUri)"
Write-Host "Message : $($Message)"
}
$AuthBody = @{'identifier' = $Identifier; 'password' = $ApiKey }
$Headers = @{}
$Headers.Add('Content-Type', 'application/json')
$Response = Invoke-RestMethod -Uri $CreateSessionUri -Method Post -Body ($AuthBody | ConvertTo-Json -Compress) -Headers $Headers
$Headers.Add('Authorization', "Bearer $($Response.accessJwt)")
$createdAt = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.ffffffZ"
$MarkdownLinkPattern = '\[([^\]]+)\]\((http[s]?://[^\s,)]+)\)'
$Links = @()
do
{
$result = $Message -match $MarkdownLinkPattern
if ($result)
{
$anchor = $Matches[1]
$url = $Matches[2]
$Message = $Message.Replace($Matches[0], $anchor)
$startIndex = $Message.IndexOf($anchor)
$endIndex = $startIndex + $anchor.Length
$Links += New-Object -TypeName psobject -Property @{
Name = $anchor
Url = $url
"StartIndex" = $startIndex
"EndIndex" = $endIndex
}
}
} until ($result -eq $false)
if ($links)
{
$Facets = @()
foreach ($Link in $Links)
{
$features = @()
$features += New-Object -TypeName psobject -Property @{
'$type' = "app.bsky.richtext.facet#link"
'uri' = $Link.Url
}
$index = New-Object -TypeName psobject -Property @{
"byteStart" = $Link.startIndex
"byteEnd" = $Link.endIndex
}
$Facets += New-Object -TypeName psobject -Property @{
'index' = $index
'features' = $features
}
}
$Record = New-Object -TypeName psobject -Property @{
'$type' = "app.bsky.feed.post"
'text' = $Message
"createdAt" = $createdAt
'facets' = $Facets
}
}
else
{
$Record = New-Object -TypeName psobject -Property @{
'$type' = "app.bsky.feed.post"
'text' = $Message
"createdAt" = $createdAt
}
}
$Post = New-Object -TypeName psobject -Property @{
'repo' = $Identifier
'collection' = 'app.bsky.feed.post'
record = $Record
}
if ($verbose.ToLower() -eq 'verbose')
{
$Post | ConvertTo-Json -Depth 5
}
Invoke-RestMethod -Uri $CreateRecordUri -Method Post -Body ($Post | ConvertTo-Json -Depth 10 -Compress) -Headers $Headers
}
catch
{
$_.InvocationInfo | Out-String;
throw $_.Exception.Message;
}