From bf3d6a52cda48d3bc80aceea57288dd538b9d1f5 Mon Sep 17 00:00:00 2001 From: cowtools Date: Fri, 6 Dec 2024 12:43:13 -0500 Subject: [PATCH] update bots.mdx to reflect changes in bluesky-social/cookbook#16 --- docs/starter-templates/bots.mdx | 67 +++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/docs/starter-templates/bots.mdx b/docs/starter-templates/bots.mdx index effdeb642..cfad8f070 100644 --- a/docs/starter-templates/bots.mdx +++ b/docs/starter-templates/bots.mdx @@ -16,58 +16,67 @@ Here's a TypeScript script that creates a bot that posts a smiley emoji on an au Install TypeScript and Node. ``` -npm i -g typescript -npm i -g ts-node +npm i -g typescript ts-node ``` Then, save your bot's username and password to a `.env` file. ``` -BLUESKY_USERNAME= -BLUESKY_PASSWORD= +BLUESKY_USERNAME="username.bsky.social" +BLUESKY_PASSWORD="password1234" ``` ### Create a Script -The below script will post 🙂 every three hours. +The below script will post "Hello, world! 🦋" every time it runs. ```TypeScript -import { BskyAgent } from '@atproto/api'; -import * as dotenv from 'dotenv'; -import { CronJob } from 'cron'; -import * as process from 'process'; - -dotenv.config(); - -// Create a Bluesky Agent -const agent = new BskyAgent({ - service: 'https://bsky.social', - }) +import { AtpAgent } from "@atproto/api"; +import "node:process"; +// Create an AT Protocol Agent +const agent = new AtpAgent({ + service: "https://bsky.social", +}); async function main() { - await agent.login({ identifier: process.env.BLUESKY_USERNAME!, password: process.env.BLUESKY_PASSWORD!}) + try { + // We just need to log in... + await agent.login({ + identifier: process.env.BLUESKY_HANDLE!, + password: process.env.BLUESKY_PASSWORD!, + }); + console.log("Successfully logged in!"); + + // Now we can make our post! await agent.post({ - text: "🙂" + text: "Hello, world! 🦋", }); - console.log("Just posted!") + console.log("Successfully posted!"); + // Et voilà! + } catch (err: any) { + console.error("Uh oh! %s", err.message); + } } main(); +``` +You can run it with: -// Run this on a cron job -const scheduleExpressionMinute = '* * * * *'; // Run once every minute for testing -const scheduleExpression = '0 */3 * * *'; // Run once every three hours in prod - -const job = new CronJob(scheduleExpression, main); // change to scheduleExpressionMinute for testing - -job.start(); +```console +$ npx tsc +$ node --env-file='.env' main.js +Successfully logged in! +Successfully posted! ``` -### Deploying Your Bot +## Deploying Your Bot + +You can deploy a simple bot on your own computer. If you want something more, deploying projects like this can be low cost or even free on a variety of platforms. For example, check out: -You can deploy a simple bot for no or low cost on a variety of platforms, such as [Heroku](https://www.heroku.com/) or [Fly.io](http://fly.io/). +- [Heroku](https://devcenter.heroku.com/articles/github-integration) +- [Fly.io](https://fly.io/docs/reference/fly-launch/) ## Rate Limits & Respecting Other Users -Keep in mind that bots should respect the network's [rate limits](/docs/advanced-guides/rate-limits). Automated bots that post to an account on an regular interval are welcome. If your bot interacts with other users, please only interact (like, repost, reply, etc.) if the user has tagged the bot account. It must be an opt-in interaction, or else your bot may be taken for spam. \ No newline at end of file +Keep in mind that bots should respect the network's [rate limits](/docs/advanced-guides/rate-limits). Automated bots that post to an account on an regular interval are welcome. If your bot interacts with other users, please only interact (like, repost, reply, etc.) if the user has tagged the bot account. It must be an opt-in interaction, or else your bot may be taken for spam.