this post was submitted on 25 Aug 2024
-15 points (25.8% liked)

Technology

59312 readers
4528 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related content.
  3. Be excellent to each another!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, to ask if your bot can be added please contact us.
  9. Check for duplicates before posting, duplicates may be removed

Approved Bots


founded 1 year ago
MODERATORS
-15
Post-Article Bot (discuss.tchncs.de)
submitted 2 months ago* (last edited 2 months ago) by [email protected] to c/[email protected]
 

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.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 4 points 2 months ago* (last edited 2 months ago)

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 [email protected]

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.