Browse Source

Telegram bot

Davide Alberani 5 years ago
parent
commit
57a2c55b6b
3 changed files with 93 additions and 1 deletions
  1. 27 0
      Dockerfile.telegram
  2. 7 1
      README.md
  3. 59 0
      telegram-bot.py

+ 27 - 0
Dockerfile.telegram

@@ -0,0 +1,27 @@
+FROM debian:stable-slim
+LABEL \
+	maintainer="Davide Alberani <da@erlug.linux.it>"
+
+RUN \
+	apt-get update && \
+	apt-get -y --no-install-recommends install \
+		polygen \
+		python3 \
+		python3-cffi \
+		python3-six \
+		python3-tz \
+		python3-decorator \
+		python3-dateutil \
+		python3-requests \
+		python3-cryptography \
+		python3-setuptools \
+		python3-wheel \
+		python3-pip && \
+	pip3 install python-telegram-bot && \
+	rm -rf /var/lib/apt/lists/*
+
+COPY tap.grm cry-a-lot telegram-bot.py /
+
+ENTRYPOINT ["python3", "/telegram-bot.py"]
+
+

+ 7 - 1
README.md

@@ -16,11 +16,17 @@ This time it will totally work.
 * feel a better person
 
 
+## Telegram bot
+
+A demo bot can be found here: https://t.me/ThoughtsAndPrayersBot
+
+You can use the *Dockerfile.telegram* file to create your own Telegram bot; don't forget to run it specifying the **TAPBOT_TOKEN** environment variable.
+
 ## Mastodon bot
 
 A demo bot can be found at [@thoughts_and_prayers](https://botsin.space/@thoughts_and_prayers)
 
-You can use the content of the *Dockerfile.mastodon* file to create your own Mastodon bot; don't forget to run it specifying the **TAPBOT_TOKEN** environment variable.
+You can use the *Dockerfile.mastodon* file to create your own Mastodon bot; don't forget to run it specifying the **TAPBOT_TOKEN** environment variable.
 
 
 # License

+ 59 - 0
telegram-bot.py

@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+"""Telegram bot for Thoughts & Prayers.
+
+Build it with: docker build -f Dockerfile.telegram -t telegram-tap .
+Run it with something like: docker run -ti --rm -e TAPBOT_TOKEN=your-telegram-token telegram-tap
+
+Copyright 2018 Davide Alberani <da@erlug.linux.it> Apache 2.0 license
+"""
+
+import os
+import sys
+import logging
+import subprocess
+from telegram.ext import Updater, CommandHandler
+
+sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
+logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+                    level=logging.INFO)
+
+logger = logging.getLogger(__name__)
+
+
+def getThoughtsAndPrayers():
+    cmd = ['/cry-a-lot']
+    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 prayer; 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 Thoughts & Prayers bottle was empty; try again'
+    return stdout
+
+
+def tap(bot, update):
+    a_tap = getThoughtsAndPrayers()
+    logging.info('%s wants some Thoughts & Prayers; serving:\n%s' % (update.message.from_user.name, a_tap))
+    update.message.reply_text(a_tap)
+
+
+def about(bot, update):
+    logging.info('%s required more info' % update.message.from_user.name)
+    update.message.reply_text('See https://github.com/alberanid/thoughts-and-prayers')
+
+
+if __name__ == '__main__':
+    if 'TAPBOT_TOKEN' not in os.environ:
+        print("Please specify the Telegram token in the TAPBOT_TOKEN environment variable")
+    logging.info('start serving Thoughts & Prayers')
+    updater = Updater(os.environ['TAPBOT_TOKEN'])
+    updater.dispatcher.add_handler(CommandHandler('tap', tap))
+    updater.dispatcher.add_handler(CommandHandler('about', about))
+    updater.start_polling()
+    updater.idle()