update for latest python-telegram-bot API

This commit is contained in:
Davide Alberani 2025-01-02 10:59:01 +01:00
parent b9aea57c10
commit e2cff58a84

View file

@ -4,13 +4,14 @@
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-2021 Davide Alberani <da@mimante.net> Apache 2.0 license Copyright 2018-2025 Davide Alberani <da@mimante.net> Apache 2.0 license
""" """
import os import os
import logging import logging
import subprocess import subprocess
from telegram.ext import Updater, CommandHandler from telegram import Update
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)
@ -29,7 +30,7 @@ def getSpam(section):
try: try:
stdout = stdout.strip() stdout = stdout.strip()
stdout = stdout.decode('utf8') stdout = stdout.decode('utf8')
except: except Exception:
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' % (
@ -39,10 +40,10 @@ def getSpam(section):
return stdout return stdout
def serve(section, update, context): async 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))
update.message.reply_text(spam) await update.message.reply_text(spam)
def en(update, context): def en(update, context):
@ -53,18 +54,17 @@ def it(update, context):
return serve('spam-ita-o', update, context) return serve('spam-ita-o', update, context)
def about(update, context): async def about(update, context):
logging.info('%s required more info' % update.effective_user.username) logging.info('%s required more info' % update.effective_user.username)
update.message.reply_text('See https://github.com/alberanid/fortunes-spam') await 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')
updater = Updater(os.environ['SPAMBOT_TOKEN']) application = Application.builder().token(os.environ['SPAMBOT_TOKEN']).build()
updater.dispatcher.add_handler(CommandHandler('en', en)) application.add_handler(CommandHandler('en', en))
updater.dispatcher.add_handler(CommandHandler('it', it)) application.add_handler(CommandHandler('it', it))
updater.dispatcher.add_handler(CommandHandler('about', about)) application.add_handler(CommandHandler('about', about))
updater.start_polling() application.run_polling(allowed_updates=Update.ALL_TYPES)
updater.idle()