From 40d46cd6e5cdb3f34a675975d3863c2b650f90c3 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Thu, 18 Jul 2019 21:24:41 +0200 Subject: [PATCH] Telegram bot --- telegram-bot/Dockerfile | 16 +++++++ telegram-bot/telegram-onthisday.py | 73 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 telegram-bot/Dockerfile create mode 100755 telegram-bot/telegram-onthisday.py diff --git a/telegram-bot/Dockerfile b/telegram-bot/Dockerfile new file mode 100644 index 0000000..e893531 --- /dev/null +++ b/telegram-bot/Dockerfile @@ -0,0 +1,16 @@ +FROM alpine +LABEL \ + maintainer="Davide Alberani " + +RUN \ + apk add --no-cache git python3 py3-cffi py3-six py3-requests py3-cryptography && \ + pip3 install python-telegram-bot && \ + pip3 install markovify && \ + cd / && \ + git clone https://github.com/alberanid/onthisday.git +COPY telegram-onthisday.py / + +WORKDIR / + +ENTRYPOINT ["python3", "/telegram-onthisday.py"] + diff --git a/telegram-bot/telegram-onthisday.py b/telegram-bot/telegram-onthisday.py new file mode 100755 index 0000000..9fe0583 --- /dev/null +++ b/telegram-bot/telegram-onthisday.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +"""Telegram bot for onthisday. + +Build it with: docker build -t telegram-onthisday . +Run it with something like: docker run -ti --rm -e EVENTS_TOKEN=your-telegram-token telegram-onthisday + +Copyright 2019 Davide Alberani Apache 2.0 license +""" + +import os +import sys +import time +import logging +import subprocess +from telegram.ext import Updater, CommandHandler + +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) + +logger = logging.getLogger(__name__) + + +def getEvents(day=None): + cmd = ['python3', '/onthisday/onthisday.py'] + if day: + try: + time.strptime(day, '%m/%d') + cmd += ['--date', day] + except Exception: + pass + if len(sys.argv) > 1: + cmd += sys.argv[1:] + 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 events; 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 events list is empty; try again' + return stdout + + +def events(bot, update, args=None): + day = None + if args and args[0]: + day = args[0].strip() + events = getEvents(day) + logging.info('%s wants some news; serving:\n%s' % (update.message.from_user.name, events)) + update.message.reply_text(events) + + +def about(bot, update): + logging.info('%s required more info' % update.message.from_user.name) + update.message.reply_text('See https://github.com/alberanid/onthisday\n\n/today to use the current date\n\n/date 07/30 for July 30') + + +if __name__ == '__main__': + if 'EVENTS_TOKEN' not in os.environ: + print("Please specify the Telegram token in the EVENTS_TOKEN environment variable") + logging.info('start serving very important facts') + updater = Updater(os.environ['EVENTS_TOKEN']) + updater.dispatcher.add_handler(CommandHandler('today', events, pass_args=True)) + updater.dispatcher.add_handler(CommandHandler('start', events, pass_args=True)) + updater.dispatcher.add_handler(CommandHandler('date', events, pass_args=True)) + updater.dispatcher.add_handler(CommandHandler('about', about)) + updater.dispatcher.add_handler(CommandHandler('help', about)) + updater.start_polling() + updater.idle()