Closed BadUserHater closed 7 months ago
Oh, guess I can't put in code properly in issues
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
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 '@twistedbonnie@mastodon.bot' in mention['status']['content']:
lyric_message = random.choice(lyrics)
mastodon.status_post(lyric_message, in_reply_to_id=status_id, visibility="public")
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
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 '@twistedbonnie@mastodon.bot' 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)
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
notifications_dismiss
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.
closing as resolved
There does not seem to be a way to run and host a mastodon bot with mastodon.py. I want to be able to run my Mastodon bot however when running, it just stops immediately with no error. Here is my code: `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 '@twistedbonnie@mastodon.bot' in mention['status']['content']: lyric_message = random.choice(lyrics) mastodon.status_post(lyric_message, in_reply_to_id=status_id, visibility="public")
my_account = mastodon.account_verify_credentials() print(my_account.id)` I am using Mastodon.py 1.8.0 because Mastodon.py 1.8.1 does not work since it keeps saying that mastodon = Mastodon() is not callable. There should be some sort of support server to get help with the library.