Hello and welcome!

I have developed a bot that automatically posts to Mastodon at scheduled intervals. In my case, it is used to share studies on gender-based violence online to help raise awareness of the issue. However, the bot’s posts are fully customisable, so you could basically use it to post anything.

If you want to implement my bot, here’s the code I used.

Step 1: This is your .json File. Enter the account details for the Mastodon account you want the bot to post from. Next, customize the article content, which will become your Mastodon message. Finally, set the interval in seconds to determine how often the bot will post.


{
    "access_token": "add access token of your mastodon account",
    "api_base_url": "add base url of your mastodon server",
    "articles": [
        "Article 1 content",
        "Article 2 content",
        "Article 3 content"
    ],
    "interval": 60 - choose time interval 
}

**Step 2: This is your Python file and the actual bot. Please enter the information as explained above the code brackets. Make sure to delete the descriptions text in quotation marks before running the bot. Otherwise, the code may not work. **


Load configuration 
with open('config.json') as config_file:
    config = json.load(config_file)

# "Code identifies the Mastodon data (Acess-Token & Server Domain)"
Authenticate to Mastodon 
mastodon = Mastodon(
    access_token=config['access_token'],
    api_base_url=config['api_base_url']
)
# "This paragraph posts the articles"
def post_article(article):
    mastodon.toot(article)
    print(f"Posted article: {article}")

# "Verifies time intervals"
# Post articles at regular intervals 
articles = config['articles']
interval = config['interval']

for article in articles:
    post_article(article)
    time.sleep(interval)

For all German speaking people: If you’re interested in my original university project, or need help adding the bot to your Mastodon account, you can watch this video: https://tube.spdns.org/w/6tWr68ihyiPQRuZjsoP3zT

(skip to 7:10 for the bot explanation)


Bot Licensing: AGPL-3.0 (Affero General Public License 3.0) allows for the use and modification of the code but requires that any derived works and versions made available over a network must also be released under the AGPL.

    • JohnEdwa
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 months ago

      Lemmy at least has an account option to hide posts made by bot accounts if you don’t want to see them.