-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { BskyAgent, RichText } from '@atproto/api'; | ||
|
||
export const publish = async (caption, file) => { | ||
try { | ||
const start_time = Date.now(); | ||
const agent = new BskyAgent({ service: 'https://bsky.social' }); | ||
await agent.login({ | ||
identifier: process.env.BSKY_USERNAME, | ||
password: process.env.BSKY_PASSWORD, | ||
}); | ||
const attachment = | ||
file && | ||
(await agent.uploadBlob(file, { | ||
encoding: 'image/jpeg', | ||
})); | ||
const richText = new RichText({ text: caption }); | ||
// automatically detects mentions and links | ||
await richText.detectFacets(agent); | ||
const postRecord = { | ||
$type: 'app.bsky.feed.post', | ||
text: richText.text, | ||
facets: richText.facets, | ||
createdAt: new Date().toISOString(), | ||
...(attachment && { | ||
embed: { | ||
$type: 'app.bsky.embed.images', | ||
images: [ | ||
{ | ||
image: attachment.data.blob, | ||
alt: '', | ||
}, | ||
], | ||
}, | ||
}), | ||
}; | ||
const result = await agent.post(postRecord); | ||
const duration = (Date.now() - start_time) / 1000; | ||
return { | ||
...result, | ||
duration, | ||
}; | ||
} catch (error) { | ||
/* console.error( | ||
'Unable to publish to bsky', | ||
JSON.stringify({ error: error.message }) | ||
); */ | ||
// unhandled error | ||
throw error; | ||
} | ||
}; |