Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active June 8, 2023 21:35
Show Gist options
  • Save Shelob9/eef8786e62bd4995035bc7f52b0263b6 to your computer and use it in GitHub Desktop.
Save Shelob9/eef8786e62bd4995035bc7f52b0263b6 to your computer and use it in GitHub Desktop.
import { BskyAgent, AtpSessionEvent, AtpSessionData } from '@atproto/api'
//https://github.com/bluesky-social/atproto/tree/main/packages/api
const bsAgent = async () => {
const agent = new BskyAgent({
service: 'https://bsky.app/',
persistSession: (evt: AtpSessionEvent, sess?: AtpSessionData) => {
// store the session-data for reuse
}
})
await agent.login({ identifier: 'josh412.bsky.social', password: process.env.BSKY_PASSWORD as string })
return agent
}
const mastodonInstance = 'https://mastodon.social'
const accessToken = process.env.MASTODON_ACCESS_TOKEN;
async function main(args) {
let name = args.name || 'stranger'
let greeting = 'Hello ' + name + '!'
console.log(1)
return await sendToot({status: 'Tacos time now'}).then(
(toot) => {
console.log(33)
return {"body": {toot} }
}
).catch( error => {
console.log( 'Mastodon error: ', error );
return {"body": {error} }
})
}
async function sendToot({status}){
const body = new URLSearchParams({
status: status,
});
const headers = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
return fetch(`${mastodonInstance}/api/v1/statuses`, {
method: 'POST',
headers: headers,
body: body,
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Failed to create new post: ${response.status} ${response.statusText}`);
}
})
.then((data) => {
console.log(`New post created: ${data.content}`);
return data;
})
.catch((error) => {
console.error(error);
throw error;
});
}
async function foo(){
console.log(2);
return 'FOO';
}
const mastodonInstance = 'https://mastodon.social'
const accessToken = process.env.MASTODON_ACCESS_TOKEN;
const sendToot = async ({ status }: {
status: string,
}) => {
const body = new URLSearchParams({
status: status,
});
const headers = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
fetch(`${mastodonInstance}/api/v1/statuses`, {
method: 'POST',
headers: headers,
body: body,
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error(`Failed to create new post: ${response.status} ${response.statusText}`);
}
})
.then((data) => {
console.log(`New post created: ${data.content}`);
})
.catch((error) => {
console.error(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment