Skip to content

Commit

Permalink
update bots.mdx to reflect changes in bluesky-social/cookbook#16
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtoolz authored Dec 6, 2024
1 parent 3c16809 commit bf3d6a5
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions docs/starter-templates/bots.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.

0 comments on commit bf3d6a5

Please sign in to comment.