prima aggiunta
This commit is contained in:
parent
e6e8944516
commit
bf7cf01392
5 changed files with 99 additions and 0 deletions
14
.env.sample
Normal file
14
.env.sample
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Mastodon Instance Base URL
|
||||
INSTANCE_BASE="https://yourmastodon.instance"
|
||||
# Values from Development Application
|
||||
CLIENT_ID="xxxxxx"
|
||||
CLIENT_SECRET="xxxxxx"
|
||||
ACCESS_TOKEN="xxxxxx"
|
||||
# Welcome message format string.
|
||||
# {username} will put the user's name.
|
||||
# {useracct} will use the user's acct value, which on local instances should be identical to the {username}.
|
||||
WELCOME_MESSAGE="Welcome, @{username}, to this instance!"
|
||||
# Welcome message visibility: 'direct', 'private', 'unlisted', 'public'
|
||||
MESSAGE_VISIBILITY="unlisted"
|
||||
# How long in seconds to wait between checks
|
||||
WAIT_TIME=300
|
15
Dockerfile
Normal file
15
Dockerfile
Normal file
|
@ -0,0 +1,15 @@
|
|||
FROM python:3
|
||||
|
||||
RUN mkdir /data
|
||||
|
||||
WORKDIR "/usr/src/app"
|
||||
ENV IS_DOCKER 1
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY . .
|
||||
|
||||
CMD [ "python", "./bot.py" ]
|
||||
|
||||
VOLUME /data
|
1
last_id.txt
Normal file
1
last_id.txt
Normal file
|
@ -0,0 +1 @@
|
|||
0
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
Mastodon.py==1.3.1
|
||||
python-dotenv==0.9.1
|
67
welcomebot.py
Normal file
67
welcomebot.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
from mastodon import Mastodon
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path
|
||||
import os
|
||||
import time
|
||||
|
||||
load_dotenv()
|
||||
|
||||
INSTANCE_BASE = os.getenv('INSTANCE_BASE')
|
||||
CLIENT_ID = os.getenv('CLIENT_ID')
|
||||
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
|
||||
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
|
||||
WELCOME_MESSAGE = os.getenv('WELCOME_MESSAGE', 'Welcome to the instance, @{username}!').replace("\\n","\n")
|
||||
MESSAGE_VISIBILITY = os.getenv('MESSAGE_VISIBILITY', 'unlisted')
|
||||
WAIT_TIME = int(os.getenv('WAIT_TIME', 300))
|
||||
IS_DOCKER = os.getenv('IS_DOCKER', False)
|
||||
|
||||
|
||||
def contains_local_account(accounts):
|
||||
for account in accounts:
|
||||
if account.username == account.acct:
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_local_account(account):
|
||||
return account.username == account.acct
|
||||
|
||||
def send_welcome_message(mastodon, account):
|
||||
formatted_message = WELCOME_MESSAGE.format(username=account.username, useracct=account.acct)
|
||||
mastodon.status_post(formatted_message)
|
||||
|
||||
last_id_file = Path('.') / 'last_id.txt'
|
||||
last_id = 0
|
||||
|
||||
if IS_DOCKER:
|
||||
last_id_file = Path('/data') / 'last_id'
|
||||
if last_id_file.is_file():
|
||||
with open(str(last_id_file)) as f:
|
||||
last_id = int(f.read())
|
||||
|
||||
print('Botvenon is starting...')
|
||||
print('Welcoming any users after id %d' % last_id)
|
||||
|
||||
mastodon = Mastodon(
|
||||
client_id=CLIENT_ID,
|
||||
client_secret=CLIENT_SECRET,
|
||||
access_token=ACCESS_TOKEN,
|
||||
api_base_url=INSTANCE_BASE)
|
||||
|
||||
user = mastodon.account_verify_credentials() # Get the current user
|
||||
|
||||
followers = mastodon.account_followers(user) # Get the latest followers
|
||||
|
||||
|
||||
# print(
|
||||
# f'''Followed by: {account.username}
|
||||
# (Acct value: {account.acct}, id: {account.id})''')
|
||||
|
||||
while True:
|
||||
for account in mastodon.account_followers(user):
|
||||
if is_local_account(account) and account.id > last_id and not account.bot:
|
||||
last_id = account.id
|
||||
with open(str(last_id_file), 'w+') as f:
|
||||
f.write(str(last_id))
|
||||
print('Welcoming %s...' % account.username)
|
||||
send_welcome_message(mastodon, account)
|
||||
time.sleep(WAIT_TIME)
|
Loading…
Reference in a new issue