Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bots.mdx to reflect changes in bluesky-social/cookbook#16 #288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.