Last active
June 12, 2023 15:26
-
-
Save Shelob9/83e699b50b185b8c761151a5c93618e6 to your computer and use it in GitHub Desktop.
Typescript function to send a BluSkky skeet https://ashevat.medium.com/how-to-build-a-bluesky-bot-using-atproto-and-openai-api-77a26a154b https://bsky.app/profile/josh412.com/post/3jxr7xwnp452r
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
import bsky from '@atproto/api'; | |
const { BskyAgent, RichText } = bsky; | |
import * as dotenv from 'dotenv'; | |
import process from 'node:process'; | |
dotenv.config(); | |
const agent = new BskyAgent({ | |
service: 'https://bsky.social', | |
}); | |
await agent.login({ | |
identifier: process.env.BSKY_USERNAME!, | |
password: process.env.BSKY_PASSWORD!, | |
}); | |
async function sendSkeet({ text, agent }: { | |
text: string, agent: bsky.BskyAgent | |
}) { | |
const rt = new RichText({ text }); | |
await rt.detectFacets(agent); | |
const post = { | |
$type: 'app.bsky.feed.post', | |
text: rt.text, | |
facets: rt.facets, | |
createdAt: new Date().toISOString() | |
} | |
const res = await agent.post(post); | |
return res; | |
} |
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
import bsky from '@atproto/api'; | |
const {BskyAgent} = bsky; | |
import * as dotenv from 'dotenv'; | |
import process from 'node:process'; | |
dotenv.config(); | |
const agent = new BskyAgent({ | |
service: 'https://bsky.social', | |
}); | |
await agent.login({ | |
identifier: process.env.BSKY_USERNAME!, | |
password: process.env.BSKY_PASSWORD!, | |
}); | |
//const tl = await agent.getTimeline(); | |
type fondLinks = { uri: string, start: number, end: number }[]; | |
async function sendSkeet({ text, agent }: { | |
text: string, agent: bsky.BskyAgent | |
}) { | |
const urlRegex = /(?:https?|www)[^\s]+/g; | |
let match; | |
const links: fondLinks = []; | |
while ((match = urlRegex.exec(text)) !== null) { | |
const uri = match[0]; | |
const start = match.index; | |
const end = start + uri.length; | |
links.push({ uri, start, end }); | |
} | |
const facets = links.map(({ uri, start, end }) => { | |
if( uri.startsWith('www.') ){ | |
uri = 'https://' + uri; | |
} | |
return { | |
index: { byteStart: start, byteEnd: end }, | |
features: [ | |
{ | |
$type: 'app.bsky.richtext.facet#link', | |
uri, | |
} | |
] | |
}; | |
}); | |
const res = await agent.post({ | |
text, | |
facets, | |
//embed: {}, | |
}); | |
return res; | |
} | |
const text = 'Testing links https://roysivan.com other words www.hiroy.club more words https://hiroy.club Taco emoji 🌮'; | |
const res = await sendSkeet({ text, agent }); | |
console.log({ res }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment