pywws.service.mastodon
Post messages to Mastodon.
Mastodon is a micro-blogging system that at first sight looks a bit like Twitter. In many ways it’s quite different though. This module sends “toots”, with optional image files, typically to report on weather conditions every hour.
Create account: https://joinmastodon.org/
Additional dependency: https://mastodonpy.readthedocs.io/
Example
weather.ini
configuration:[mastodon] handle = kt19weather@botsin.space client_id = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx access_token = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [hourly] text = ['toot.txt', 'tweet_media.txt', '24hrs.txt'] plot = ['tweet.png.xml', '24hrs.png.xml', 'rose_12hrs.png.xml'] services = [('mastodon', 'toot.txt'), ('twitter', 'tweet_media.txt'), ('ftp', '24hrs.png', 'rose_12hrs.png', '24hrs.txt')]
Create an account
Before creating a Mastodon account for your weather reports you need to choose an “instance”. Mastodon is a federated system, running on many interconnected servers, each of which is called an instance. Choose one whose rules allow “bots” (i.e. automated posts) such as botsin.space. After creating an account, edit its profile and make sure the “this is a bot account” box is selected.
The pywws.service.mastodon
module requires you to install an
additional dependency:
sudo pip install mastodon.py
Create a template
A “toot” is a short text of up to 500 characters. It’s up to you what to
put in your “toots” but an example is included to get your started. Copy
the example template toot.txt
to your template directory, then edit
it to suit your preferences. (At least change the hashtags to suit your
location.) You should also check it uses the same text encoding as your
other templates. The example template includes a media
line to send
a graph. Either remove this or copy the example graph template
tweet.png.xml
to your graph templates directory, if you don’t
already have one there.
Now generate a toot file from your template, for example:
python -m pywws.template ~/weather/data ~/weather/templates/toot.txt toot.txt
cat toot.txt
Post your first toot
Now you are ready to run pywws.service.mastodon
:
python -m pywws.service.mastodon ~/weather/data toot.txt
If this works, your new Mastodon account will have posted its first weather report. (You can delete the toot.txt file now.)
Add Mastodon posts to your hourly tasks
Edit the [hourly]
section in weather.ini
. If your toots include
one or more graphs you need to add the graph templates to the plot
list. Note that if you reuse your Twitter graph you only need to
generate it once. Add your toot template to the text
list. Finally,
add mastodon
to the services
list, with an option specifying the
template processing result. For example:
[hourly]
text = ['toot.txt']
plot = ['tweet.png.xml']
services = [('mastodon', 'toot.txt')]
You could use the [logged]
, [12 hourly]
or [daily]
sections
instead, but I think [hourly]
is most appropriate for Mastodon
updates.
Add more images
Mastodon allows up to four images per “toot”, so you could add more
graphs, or a webcam image, or a weather forecast icon. Use one media
line per image at the start of your toot template. You need to give the
full path of any files that are not in your “output” directory (a
subdirectory of your work directory called output
).
Classes
|
Comments or questions? Please subscribe to the pywws mailing list http://groups.google.com/group/pywws and let us know.