Telegram bot
This commit is contained in:
szülő
fee8c1b97f
commit
59f26559a3
3 fájl változott, egészen pontosan 80 új sor hozzáadva és 0 régi sor törölve
|
@ -26,6 +26,10 @@ The charset of both files (spam-o and spam-ita-o) is UTF-8; convert it to suite
|
|||
|
||||
If you add your own fortunes, you can also update the *.dat* files running **make** (it requires the *strfile* command, usually found in the *fortune-mod* package)
|
||||
|
||||
## Telegram bot
|
||||
|
||||
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.
|
||||
|
||||
# License
|
||||
|
||||
Copyright 2003-2018 Davide Alberani <da@erlug.linux.it>
|
||||
|
|
15
telegram-bot/Dockerfile
Általános fájl
15
telegram-bot/Dockerfile
Általános fájl
|
@ -0,0 +1,15 @@
|
|||
FROM alpine
|
||||
LABEL \
|
||||
maintainer="Davide Alberani <da@erlug.linux.it>"
|
||||
|
||||
RUN \
|
||||
apk add --no-cache git fortune python3 && \
|
||||
pip3 install python-telegram-bot && \
|
||||
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"]
|
||||
|
61
telegram-bot/fortunes-spam-bot.py
Futtatható fájl
61
telegram-bot/fortunes-spam-bot.py
Futtatható fájl
|
@ -0,0 +1,61 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Telegram bot for fortunes-spam.
|
||||
|
||||
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
|
||||
|
||||
Copyright 2018 Davide Alberani <da@erlug.linux.it> Apache 2.0 license
|
||||
"""
|
||||
|
||||
import os
|
||||
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 getSpam(section):
|
||||
cmd = ['fortune', '-o', section]
|
||||
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(section, bot, update):
|
||||
spam = getSpam(section)
|
||||
logging.info('%s wants some spam' % update.message.from_user.name)
|
||||
logging.info('served: %s' % spam)
|
||||
update.message.reply_text(spam)
|
||||
|
||||
|
||||
def en(bot, update):
|
||||
return serve('spam-o', bot, update)
|
||||
|
||||
|
||||
def it(bot, update):
|
||||
return serve('spam-ita-o', bot, update)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not 'SPAMBOT_TOKEN' in os.environ:
|
||||
print("Please specify the Telegram token in the SPAMBOT_TOKEN environment variable")
|
||||
logging.info('start serving delicious spam')
|
||||
updater = Updater(os.environ['SPAMBOT_TOKEN'])
|
||||
updater.dispatcher.add_handler(CommandHandler('en', en))
|
||||
updater.dispatcher.add_handler(CommandHandler('it', it))
|
||||
updater.start_polling()
|
||||
updater.idle()
|
Betöltés…
Reference in a new issue