Comments (8)
Oh, guess I can't put in code properly in issues
from mastodon.py.
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.
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.
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.
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.
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.
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.
closing as resolved
from mastodon.py.
Related Issues (20)
- Streaming not working HOT 5
- Add `offset` parameter for `trending_statuses`
- MastodonVersionError: Version check failed (Need version 1.1.0) HOT 5
- How to get configured character limit of Mastodon instance HOT 5
- Add Citation for Mastodon.py HOT 3
- Update filter methods to 4.x HOT 2
- [question] Keeping async listener connection active? HOT 2
- media_update return Error 404: Record not found HOT 1
- Event on mentioned? HOT 2
- mastodon.account_search not working correctly? HOT 10
- Deprecate visibility parameter value `private` and use `followers_only` instead HOT 3
- module is installed, but missing library stubs or py.typed marker HOT 5
- scopes = ['read', 'write'] is ret, but the generated url sill contains '+follow+push' HOT 6
- OAuth question HOT 2
- TypeError: exceptions must derive from BaseException HOT 3
- [Question] Is there a way to export lists as CSV? HOT 2
- [Question] scheduling posts and replies HOT 2
- content_type is not ignored on incompatible servers HOT 2
- [question] retrieving most recent status for a user HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mastodon.py.