Skip to content

Instantly share code, notes, and snippets.

@RustingSword
Last active March 4, 2024 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RustingSword/772f0c5582bb55a50270c339e948fc76 to your computer and use it in GitHub Desktop.
Save RustingSword/772f0c5582bb55a50270c339e948fc76 to your computer and use it in GitHub Desktop.
一个简单的 mastodon bot
#!/usr/bin/env python3
import os
import random
from mastodon import Mastodon
API_BASE_URL = "https://me.ns.ci" # 注册网站,比如 https://me.ns.ci
EMAIL = os.environ.get("MASTODON_EMAIL") # 注册用的邮箱
PASSWORD = os.environ.get("MASTODON_PASSWORD") # 账号密码
CLIENT_CRED_FILE = "pytooter_clientcred.secret"
USER_CRED_FILE = "pytooter_usercred.secret"
def create_client():
if not os.path.exists(CLIENT_CRED_FILE):
Mastodon.create_app(
"my-bot", # 这个可以改成想要的名字,会显示在消息详情里“发自xxx”
api_base_url=API_BASE_URL,
to_file=CLIENT_CRED_FILE,
)
if not os.path.exists(USER_CRED_FILE):
mastodon = Mastodon(
client_id="pytooter_clientcred.secret", api_base_url=API_BASE_URL
)
mastodon.log_in(
EMAIL, PASSWORD, to_file=USER_CRED_FILE
)
client = Mastodon(access_token=USER_CRED_FILE)
return client
client = create_client()
content = random.choice(open("content.txt").read().splitlines()) # 要发送的内容放在当前文件夹下面的 `content.txt` 里,每行一条,每次随机选择一条
client.toot(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment