Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

5 changed files with 25 additions and 25 deletions

View file

@ -41,7 +41,7 @@ You can use the content of the *mastodon-bot* directory to create your own Masto
# License # License
Copyright 2003-2021 Davide Alberani <da@mimante.net> Copyright 2003-2021 Davide Alberani <da@erlug.linux.it>
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View file

@ -1,10 +1,10 @@
FROM alpine FROM alpine
LABEL \ LABEL \
maintainer="Davide Alberani <da@mimante.net>" maintainer="Davide Alberani <da@erlug.linux.it>"
RUN \ RUN \
apk add --no-cache git fortune python3 py3-pip py3-cffi py3-six py3-requests py3-cryptography && \ apk add --no-cache git fortune python3 py3-cffi py3-six py3-requests py3-cryptography && \
pip3 install --break-system-packages Mastodon.py && \ pip3 install Mastodon.py && \
cd / && \ cd / && \
git clone https://github.com/alberanid/fortunes-spam.git git clone https://github.com/alberanid/fortunes-spam.git
COPY fortunes-spam-bot.py /fortunes-spam COPY fortunes-spam-bot.py /fortunes-spam

View file

@ -6,6 +6,9 @@ import sys
import subprocess import subprocess
from mastodon import Mastodon from mastodon import Mastodon
API_URL = 'https://botsin.space/'
def getSpam(sections=('spam-o', 'spam-ita-o')): def getSpam(sections=('spam-o', 'spam-ita-o')):
cmd = ['fortune', '-o', *sections] cmd = ['fortune', '-o', *sections]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@ -23,10 +26,10 @@ def getSpam(sections=('spam-o', 'spam-ita-o')):
return stdout return stdout
def serve(api_url, token): def serve(token):
spam = getSpam() spam = getSpam()
print('serving:\n%s' % spam) print('serving:\n%s' % spam)
mastodon = Mastodon(access_token=token, api_base_url=api_url) mastodon = Mastodon(access_token=token, api_base_url=API_URL)
mastodon.status_post(spam, sensitive=True, spoiler_text='NSFW') mastodon.status_post(spam, sensitive=True, spoiler_text='NSFW')
@ -34,7 +37,4 @@ if __name__ == '__main__':
if 'SPAMBOT_TOKEN' not in os.environ: if 'SPAMBOT_TOKEN' not in os.environ:
print("Please specify the Mastodon token in the SPAMBOT_TOKEN environment variable") print("Please specify the Mastodon token in the SPAMBOT_TOKEN environment variable")
sys.exit(1) sys.exit(1)
if 'API_URL' not in os.environ: serve(token=os.environ['SPAMBOT_TOKEN'])
print("Please specify the Mastodon server in the API_URL environment variable")
sys.exit(1)
serve(api_url=os.environ['API_URL'], token=os.environ['SPAMBOT_TOKEN'])

View file

@ -1,10 +1,10 @@
FROM alpine FROM alpine
LABEL \ LABEL \
maintainer="Davide Alberani <da@mimante.net>" maintainer="Davide Alberani <da@erlug.linux.it>"
RUN \ RUN \
apk add --no-cache git fortune python3 py3-cffi py3-six py3-requests py3-cryptography py3-pip && \ apk add --no-cache git fortune python3 py3-cffi py3-six py3-requests py3-cryptography py3-pip && \
pip3 install --break-system-packages python-telegram-bot && \ pip3 install python-telegram-bot && \
cd / && \ cd / && \
git clone https://github.com/alberanid/fortunes-spam.git git clone https://github.com/alberanid/fortunes-spam.git
COPY fortunes-spam-bot.py /fortunes-spam COPY fortunes-spam-bot.py /fortunes-spam

View file

@ -4,14 +4,13 @@
Build it with: docker build -t fortunes-spam-bot . Build it with: docker build -t fortunes-spam-bot .
Run it with something like: docker run -ti --rm -e SPAMBOT_TOKEN=your-telegram-token fortunes-spam-bot Run it with something like: docker run -ti --rm -e SPAMBOT_TOKEN=your-telegram-token fortunes-spam-bot
Copyright 2018-2025 Davide Alberani <da@mimante.net> Apache 2.0 license Copyright 2018-2021 Davide Alberani <da@erlug.linux.it> Apache 2.0 license
""" """
import os import os
import logging import logging
import subprocess import subprocess
from telegram import Update from telegram.ext import Updater, CommandHandler
from telegram.ext import Application, CommandHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO) level=logging.INFO)
@ -30,7 +29,7 @@ def getSpam(section):
try: try:
stdout = stdout.strip() stdout = stdout.strip()
stdout = stdout.decode('utf8') stdout = stdout.decode('utf8')
except Exception: except:
return 'uh-oh: something was wrong with the encoding of the can\'s label; try again' return 'uh-oh: something was wrong with the encoding of the can\'s label; try again'
if process.returncode != 0: if process.returncode != 0:
return 'something terrible is happening: exit code: %s, stderr: %s' % ( return 'something terrible is happening: exit code: %s, stderr: %s' % (
@ -40,10 +39,10 @@ def getSpam(section):
return stdout return stdout
async def serve(section, update, context): def serve(section, update, context):
spam = getSpam(section) spam = getSpam(section)
logging.info('%s wants some spam; serving:\n%s' % (update.effective_user.username, spam)) logging.info('%s wants some spam; serving:\n%s' % (update.effective_user.username, spam))
await update.message.reply_text(spam) update.message.reply_text(spam)
def en(update, context): def en(update, context):
@ -54,17 +53,18 @@ def it(update, context):
return serve('spam-ita-o', update, context) return serve('spam-ita-o', update, context)
async def about(update, context): def about(update, context):
logging.info('%s required more info' % update.effective_user.username) logging.info('%s required more info' % update.effective_user.username)
await update.message.reply_text('See https://github.com/alberanid/fortunes-spam') update.message.reply_text('See https://github.com/alberanid/fortunes-spam')
if __name__ == '__main__': if __name__ == '__main__':
if 'SPAMBOT_TOKEN' not in os.environ: if 'SPAMBOT_TOKEN' not in os.environ:
print("Please specify the Telegram token in the SPAMBOT_TOKEN environment variable") print("Please specify the Telegram token in the SPAMBOT_TOKEN environment variable")
logging.info('start serving delicious spam') logging.info('start serving delicious spam')
application = Application.builder().token(os.environ['SPAMBOT_TOKEN']).build() updater = Updater(os.environ['SPAMBOT_TOKEN'])
application.add_handler(CommandHandler('en', en)) updater.dispatcher.add_handler(CommandHandler('en', en))
application.add_handler(CommandHandler('it', it)) updater.dispatcher.add_handler(CommandHandler('it', it))
application.add_handler(CommandHandler('about', about)) updater.dispatcher.add_handler(CommandHandler('about', about))
application.run_polling(allowed_updates=Update.ALL_TYPES) updater.start_polling()
updater.idle()