Git Product home page Git Product logo

Comments (8)

BadUserHater avatar BadUserHater commented on June 2, 2024

Oh, guess I can't put in code properly in issues

from mastodon.py.

codl avatar codl commented on June 2, 2024

I can't put in code properly in issues

You can by surrounding your code with triple backticks. Please fix your original post so your code is readable.

Mastodon.py 1.8.1 does not work since it keeps saying that mastodon = Mastodon() is not callable

Strange. How did you install the library? You should install it with pip or another package manager

Edit: Also, please provide more info about your environment like which version of python are you running on which operating system

from mastodon.py.

BadUserHater avatar BadUserHater commented on June 2, 2024

I am using replit to test the code which means I am using Python 3.8. I used replit's package installer which I assume it installs the package through pip. I used it on Mastodon 1.8.0 and that version works.
Does the code show up properly now so anyone can help me how to host and keep the mastodon bot running 24/7?

from mastodon import Mastodon
import random
import sys
import json
import os
from dotenv import load_dotenv
from webserver import keep_alive

mastodon = Mastodon(
    client_id=os.getenv("CLIENT_ID"),
    client_secret=os.getenv("CLIENT_SECRET"),
    access_token=os.getenv("ACCESS_TOKEN"),
    api_base_url = 'https://mastodon.bot/'
)

following = mastodon.account_following(110974449022845937)

followering_posts = []
for i in following:
    for posts in mastodon.account_statuses(i):
        followering_posts.append(posts.id)

print(followering_posts)

with open("lyrics.txt", "r") as f:
    lyrics = f.readlines()

def respond_to_mentions():
    mentions = mastodon.notifications()
    for mention in mentions:
        if mention['type'] == 'mention':
            status_id = mention['status']['id']
            user_id = mention['account']['id']
            username = mention['account']['acct']
            if '@[email protected]' in mention['status']['content']:
                lyric_message = random.choice(lyrics)
                mastodon.status_post(lyric_message, in_reply_to_id=status_id, visibility="public")

from mastodon.py.

aitorres avatar aitorres commented on June 2, 2024

Hi! If this is the full script you're running it's missing a call to respond_to_mentions, if you want it to run every 5 minutes for example you can try adding a loop in the end that runs respond_to_mentions, something like:

import time

# rest of your code

while True:
    # catching up on mentions
    respond_to_mentions()
    
    # waiting 5 minutes
    time.sleep(5 * 60)

Note that I haven't tried this code but it might be helpful

from mastodon.py.

BadUserHater avatar BadUserHater commented on June 2, 2024

This seems to work but now I need help getting my bot to respond to me.
I am trying to make my bot reply to the toots it is mentioned in only responds if the author followed the bot (I want to lock it behind followers to prevent the rate limits from happening). Sorry if these are basic questions. I am new to making Mastodon bots.
My code now:

from mastodon import Mastodon
import random
import sys
import json
import os
import time
from dotenv import load_dotenv

load_dotenv()

mastodon = Mastodon(
    client_id=os.getenv("CLIENT_ID"),
    client_secret=os.getenv("CLIENT_SECRET"),
    access_token=os.getenv("ACCESS_TOKEN"),
    api_base_url = 'https://mastodon.bot/'
)

following = mastodon.account_following(110974449022845937)

followering_posts = []
for i in following:
    for posts in mastodon.account_statuses(i):
        followering_posts.append(posts.id)

print(followering_posts)

with open("lyrics.txt", "r") as f:
    lyrics = f.readlines()

def respond_to_mentions():
    mentions = mastodon.notifications()
    for mention in mentions:
        if mention['type'] == 'mention':
            status_id = mention['status']['id']
            user_id = mention['account']['id']
            username = mention['account']['acct']
            if '@[email protected]' in mention['status']['content']:
                lyric_message = random.choice(lyrics)
                mastodon.status_post(lyric_message, in_reply_to_id=status_id, visibility="public")

while True:
    respond_to_mentions()
    time.sleep(5 * 60)

from mastodon.py.

codl avatar codl commented on June 2, 2024

You can use account_relationships to check if an account is following the bot, for example:

for mention in mentions:
    relationship = mastodon.account_relationships(mention['account']['id'])
    if not relationship['followed_by']:
        # account doesn't follow us, skip mention
        continue

    # account does follow us, let's reply
    ...

By the way, your code will keep replying to the same mentions over and over every five minutes, which I don't think was your intention?

When respond_to_mentions, which runs every five minutes, calls mastodon.notifications(), it gets the last 40 notifications, including mentions that the bot may have already replied to.

Some ways to deal with this would be

  • Deleting each notification once the bot has processed it, with notifications_dismiss
  • Switching over to use streaming instead. The bot would then receive mentions immediately as they happen and won't receive any past notifications. Unfortunately there's a lack of simple examples of the streaming API.
  • Checking the context of each mention for an existing reply from the bot

from mastodon.py.

halcy avatar halcy commented on June 2, 2024

Hi! If your goal is to write a bot that replies when mentioned, you might also want to look at something more high-level, like https://github.com/chr-1x/ananas - that takes care of a lot of the plumbing for you.

I wonder if there would be a point to having something like a discord server, but generally, I'm also not opposed to having these kinds of questions in the github issues, or on masto itself, I guess.

from mastodon.py.

halcy avatar halcy commented on June 2, 2024

closing as resolved

from mastodon.py.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.