Mastodon bot

This commit is contained in:
Davide Alberani 2018-10-23 22:57:06 +02:00
parent 55ce2562d8
commit 16733051d7
3 changed files with 63 additions and 0 deletions

View file

@ -32,6 +32,13 @@ A demo bot can be found here: https://t.me/FortunesSpamBot
You can use the content of the *telegram-bot* directory to create your own Telegram bot; don't forget to run it specifying the **SPAMBOT_TOKEN** environment variable.
## Mastodon bot
A demo bot can be found at [@fortunes_spam@botsin.space](https://botsin.space/@fortunes_spam)
You can use the content of the *mastodon-bot* directory to create your own Mastodon bot; don't forget to run it specifying the **SPAMBOT_TOKEN** environment variable.
# License
Copyright 2003-2018 Davide Alberani <da@erlug.linux.it>

16
mastodon-bot/Dockerfile Normal file
View file

@ -0,0 +1,16 @@
FROM alpine
LABEL \
maintainer="Davide Alberani <da@erlug.linux.it>"
RUN \
apk add --no-cache git fortune python3 && \
pip3 install Mastodon.py && \
cd / && \
git clone https://github.com/alberanid/fortunes-spam.git
COPY fortunes-spam-bot.py /fortunes-spam
WORKDIR /fortunes-spam
ENTRYPOINT ["python3", "fortunes-spam-bot.py"]

View file

@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import subprocess
from mastodon import Mastodon
API_URL = 'https://botsin.space/'
def getSpam(sections=('spam-o', 'spam-ita-o')):
cmd = ['fortune', '-o', *sections]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
try:
stdout = stdout.strip()
stdout = stdout.decode('utf8')
except:
return 'uh-oh: something was wrong with the encoding of the can\'s label; try again'
if process.returncode != 0:
return 'something terrible is happening: exit code: %s, stderr: %s' % (
process.returncode, stderr.decode('utf8'))
if not stdout:
return 'sadness: the spam can was empty; try again'
return stdout
def serve(token):
spam = getSpam()
print('serving:\n%s' % spam)
mastodon = Mastodon(access_token=token, api_base_url=API_URL)
mastodon.status_post(spam)
if __name__ == '__main__':
if 'SPAMBOT_TOKEN' not in os.environ:
print("Please specify the Mastodon token in the SPAMBOT_TOKEN environment variable")
sys.exit(1)
serve(token=os.environ['SPAMBOT_TOKEN'])