Recently I'm using Mastodon as my main SNS, so I thought I'd try making something with a bot, so instead of a memorandum
Get an access token to hit the API. From Mastodon's home ** User Settings → Development → New App ** You should get to this page. Enter the name of the app on this page and press ** Send ** at the bottom The name you entered earlier is displayed, so if you press it
** Client key Client secret Access token **
I think that is displayed. All you need after this is an access token. You don't need to remember this page as you can always see it.
You can toot with the following code.
toot.py
import requests
headers = {'Authorization': 'Bearer xxxxxxxxxxxxxxxxx'} #Enter the access token you obtained in xxxxxxxxxxxxxxxxx.
host = "https://example.com/api/v1/statuses" #example.Change com to the domain of your instance.
toot = "" #toot text
content = toot.encode() #Doesn't work without encoding
data = {'status': cotent}
post = requests.post(host, "headers"=headers, "data"=data)
print(post.status_code)
The only library you need is requests, so it's very easy to toot. It is not good for security to write the Access token directly in the code, so please use it properly. It seems that it doesn't work well unless the text toot is encoded when tooting, so I had a hard time. If 200 is output with this, you can safely toot.
Create a sentence you want to toot in a textbook, bring it, and turn it with cron to complete the bot.
Recommended Posts