Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active June 12, 2023 15:26
Show Gist options
  • Save Shelob9/83e699b50b185b8c761151a5c93618e6 to your computer and use it in GitHub Desktop.
Save Shelob9/83e699b50b185b8c761151a5c93618e6 to your computer and use it in GitHub Desktop.
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;
}
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