An open API service indexing awesome lists of open source software.

https://github.com/rondevhub/mastohashbot

MastoHashBot is a lightweight automation bot for Mastodon. It automatically searches for posts (toots) containing specific hashtags and boosts them. The bot ensures that no post is boosted twice by storing processed IDs in a JSON file and also cleans up old entries after a configurable period of time.
https://github.com/rondevhub/mastohashbot

fediverse fediverse-bot mastodon mastodon-bot opensource

Last synced: 2 months ago
JSON representation

MastoHashBot is a lightweight automation bot for Mastodon. It automatically searches for posts (toots) containing specific hashtags and boosts them. The bot ensures that no post is boosted twice by storing processed IDs in a JSON file and also cleans up old entries after a configurable period of time.

Awesome Lists containing this project

README

          

# ๐Ÿค– MastoHashBot

MastoHashBot is a lightweight automation bot for Mastodon.
It automatically searches for posts (toots) containing specific hashtags and boosts them.
The bot ensures that no post is boosted twice by storing processed IDs in a JSON file and also cleans up old entries after a configurable period of time.

[![Buy me a coffee](https://mini-badges.rondevhub.de/icon/cuptogo/Buy_me_a_Coffee-c1d82f-222/social "Buy me a coffee")](https://www.buymeacoffee.com/RonDev)
[![Buy me a coffee](https://mini-badges.rondevhub.de/icon/cuptogo/ko--fi.com-c1d82f-222/social "Buy me a coffee")](https://ko-fi.com/U6U31EV2VS)
[![Sponsor me](https://mini-badges.rondevhub.de/icon/hearts-red/Sponsor_me/social "Sponsor me")](https://github.com/sponsors/RonDevHub)
[![Pizza Power](https://mini-badges.rondevhub.de/icon/pizzaslice/Buy_me_a_pizza/social "Pizza Power")](https://www.paypal.com/paypalme/Depressionist1/4,99)
---

## โœจ Features

- ๐Ÿ”Ž Searches for multiple hashtags across any Mastodon instance
- ๐Ÿš€ Automatically boosts posts (no duplicates)
- ๐Ÿงน Cleans up old entries from the JSON file after `X` days
- ๐Ÿ“‚ Stores boosted IDs locally in `posted_ids.json`
- โš™๏ธ Configurable via environment variables or script variables

---

## ๐Ÿ“‹ Requirements

- PHP **>= 7.4** (for the PHP version of the bot)
- cURL extension enabled in PHP
- Bash + `curl` + `jq` (for the Bash version of the bot)
- A Mastodon **access token** with `write:accounts` and `write:statuses` permissions
- A Mastodon **instance URL** (e.g. `mastodon.social`)

---

## โš™๏ธ Setup

### 1. Get a Mastodon Access Token

1. Log in to your Mastodon instance
2. Go to **Preferences > Development > New Application**
3. Give it a name, e.g. `MastoHashBot`
4. Select required permissions:
- `read:statuses`
- `write:accounts`
- `write:statuses`
5. Copy the generated **Access Token**

---

### 2. Configure the Bot

Edit the script (PHP or Bash) and set:

```php
// PHP Version
$accessToken = 'INSERT_YOUR_ACCESS_TOKEN_HERE';
$instance = 'YOUR_INSTANCE_HERE';
$hashtags = ['hashtag1', 'hashtag2'];
$cleanupDays = 30;
```
```bash
# Bash Version
ACCESS_TOKEN="INSERT_YOUR_ACCESS_TOKEN_HERE"
INSTANCE="YOUR_INSTANCE_HERE"
HASHTAGS="hashtag1 hashtag2 hashtag3"
CLEANUP_DAYS=30
```
---

### 3. Run the Bot

PHP Version:
```
php mastohashbot.php
```
Bash Version:
```
bash mastohashbot.sh
```
You should see log messages like:
```
Starting Mastodon bot run...
There are 0 IDs that have already been boosted.
Searching for posts with hashtag #fediGive...
Post ID 123456 boosted successfully!
Bot run completed.
```
---

### ๐Ÿ—‘ JSON File Cleanup

The bot stores boosted post IDs in a JSON file (`posted_ids.json`).
To prevent the file from growing indefinitely, entries older than `$cleanupDays` are automatically removed.

---

### ๐Ÿค Usage in Production

- Run the bot regularly via cronjob or a systemd timer, for example:
```
# Run every 15 minutes
*/15 * * * * php /path/to/mastohashbot.php >> /path/to/mastohashbot.log 2>&1
```
- Monitor the logs (`mastohashbot.log`) to check the activity.

---

### ๐Ÿ“ฆ Folder Structure
```
.
โ”œโ”€โ”€ mastohashbot.php # PHP version of the bot
โ”œโ”€โ”€ mastohashbot.sh # Bash version of the bot
โ”œโ”€โ”€ posted_ids.json # Stores boosted IDs
โ””โ”€โ”€ README.md # Project documentation
```

---

### ๐Ÿ”’ Security Notes
- Keep your Access Token secret โ€“ it allows actions on your Mastodon account.
- Do not commit your token into version control (e.g. GitHub).
- Consider using environment variables or `.env` files instead of hardcoding tokens.

---

### ๐ŸŒ Example Use Cases
- ๐Ÿ“ข Community hashtags (e.g. `#fediGive`, `#fediHelp`)
- ๐Ÿ›’ Local marketplaces (e.g. `#flohmarkt`)
- ๐Ÿ˜ Automated content boosting for niche topics

---

### ๐Ÿ›  Troubleshooting
- Error: API request error (401 Unauthorized)
โ†’ Check your access token and permissions
- Error: No results found
โ†’ Make sure the hashtag exists and is used
- Error: JSON decode error
โ†’ The Mastodon API returned unexpected data (try again later)

---

### ๐Ÿ“œ License
MIT License โ€“ free to use, modify, and distribute.

---

### ๐Ÿ‘จโ€๐Ÿ’ป Author
Developed with โค๏ธ for the Fediverse.
Feel free to fork, contribute, or open issues!