There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

Post-Article Bot

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.



<span style="color:#323232;">{
</span><span style="color:#323232;">    "access_token": "add access token of your mastodon account",
</span><span style="color:#323232;">    "api_base_url": "add base url of your mastodon server",
</span><span style="color:#323232;">    "articles": [
</span><span style="color:#323232;">        "Article 1 content",
</span><span style="color:#323232;">        "Article 2 content",
</span><span style="color:#323232;">        "Article 3 content"
</span><span style="color:#323232;">    ],
</span><span style="color:#323232;">    "interval": 60 - choose time interval 
</span><span style="color:#323232;">}
</span>

**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. **



<span style="color:#323232;">Load configuration 
</span><span style="color:#323232;">with open('config.json') as config_file:
</span><span style="color:#323232;">    config = json.load(config_file)
</span><span style="color:#323232;">
</span><span style="color:#323232;"># "Code identifies the Mastodon data (Acess-Token & Server Domain)"
</span><span style="color:#323232;">Authenticate to Mastodon 
</span><span style="color:#323232;">mastodon = Mastodon(
</span><span style="color:#323232;">    access_token=config['access_token'],
</span><span style="color:#323232;">    api_base_url=config['api_base_url']
</span><span style="color:#323232;">)
</span><span style="color:#323232;"># "This paragraph posts the articles"
</span><span style="color:#323232;">def post_article(article):
</span><span style="color:#323232;">    mastodon.toot(article)
</span><span style="color:#323232;">    print(f"Posted article: {article}")
</span><span style="color:#323232;">
</span><span style="color:#323232;"># "Verifies time intervals"
</span><span style="color:#323232;"># Post articles at regular intervals 
</span><span style="color:#323232;">articles = config['articles']
</span><span style="color:#323232;">interval = config['interval']
</span><span style="color:#323232;">
</span><span style="color:#323232;">for article in articles:
</span><span style="color:#323232;">    post_article(article)
</span><span style="color:#323232;">    time.sleep(interval)
</span>

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: 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.

CaptainBasculin ,

Cool script. Would reccomend getting data from RSS feeds as a start instead of hardcoding your news; as that’s what I did on my account bot that posts crucial news to !turkey

I think whatever the Mastodon class is, you’ll need some sort of pre-installing library to use it and import it, and import json too.

Also this is a technology news channel; this post would be more helpful in a programming related community.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • [email protected]
  • random
  • lifeLocal
  • goranko
  • All magazines