Update for Python-Telegram-Bot v12

This commit is contained in:
Andreas Bielawski 2019-03-18 18:56:04 +01:00
parent cb8c281323
commit 69d9c78370
2 changed files with 36 additions and 31 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ __pycache__/
*.py[cod] *.py[cod]
*$py.class *$py.class
src/ src/
venv/
# IDE # IDE
.idea/ .idea/

66
bot.py
View File

@ -46,10 +46,10 @@ logger = logging.getLogger(__name__)
try: try:
bot_token = config["DEFAULT"]["token"] bot_token = config["DEFAULT"]["token"]
except KeyError: except KeyError:
logger.error("Bot token is missing, check config.ini") logger.error("Bot token is missing, check config.ini.")
sys.exit(1) sys.exit(1)
if not bot_token: if not bot_token:
logger.error("Bot token is missing, check config.ini") logger.error("Bot token is missing, check config.ini.")
sys.exit(1) sys.exit(1)
# Admins # Admins
@ -79,14 +79,14 @@ else:
r = redis.Redis(host=redis_host, port=int(redis_port), db=int(redis_db), decode_responses=True) r = redis.Redis(host=redis_host, port=int(redis_port), db=int(redis_db), decode_responses=True)
if not r.ping(): if not r.ping():
logging.getLogger("Redis").critical("Failed to connect to Redis server") logging.getLogger("Redis").critical("Failed to connect to Redis server.")
sys.exit(1) sys.exit(1)
feed_hash = "pythonbot:rss:{0}" feed_hash = "pythonbot:rss:{0}"
@run_async @run_async
def start(bot, update): def start(update, context):
if not utils.can_use_bot(update): if not utils.can_use_bot(update):
return return
update.message.reply_text( update.message.reply_text(
@ -96,7 +96,7 @@ def start(bot, update):
@run_async @run_async
def help_text(bot, update): def help_text(update, context):
if not utils.can_use_bot(update): if not utils.can_use_bot(update):
return return
update.message.reply_text( update.message.reply_text(
@ -108,13 +108,13 @@ def help_text(bot, update):
@run_async @run_async
def list_feeds(bot, update, args): def list_feeds(update, context):
if not utils.can_use_bot(update): if not utils.can_use_bot(update):
return return
if args: if context.args:
chat_name = args[0] chat_name = context.args[0]
try: try:
resp = bot.getChat(chat_name) resp = context.bot.getChat(chat_name)
except telegram.error.BadRequest: except telegram.error.BadRequest:
update.message.reply_text("❌ This channel does not exist.") update.message.reply_text("❌ This channel does not exist.")
return return
@ -142,34 +142,34 @@ def list_feeds(bot, update, args):
@run_async @run_async
def subscribe(bot, update, args): def subscribe(update, context):
if not utils.can_use_bot(update): if not utils.can_use_bot(update):
return return
if not args: if not context.args:
update.message.reply_text("❌ No feed URL given.") update.message.reply_text("❌ No feed URL given.")
return return
feed_url = args[0] feed_url = context.args[0]
if not re.match("^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$", feed_url): if not re.match("^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&~+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$", feed_url):
update.message.reply_text("❌ That doesn't look like an URL.") update.message.reply_text("❌ That doesn't look like an URL.")
return return
# Get Chat ID from name if given # Get Chat ID from name if given
if len(args) > 1: if len(context.args) > 1:
chat_name = args[1] chat_name = context.args[1]
try: try:
resp = bot.getChat(chat_name) resp = context.bot.getChat(chat_name)
except telegram.error.BadRequest: except telegram.error.BadRequest:
update.message.reply_text("❌ This channel does not exist.") update.message.reply_text("❌ This channel does not exist.")
return return
chat_id = str(resp.id) chat_id = str(resp.id)
resp = bot.getChatMember(chat_id, bot.id) resp = context.bot.getChatMember(chat_id, context.bot.id)
if resp.status != "administrator": if resp.status != "administrator":
update.message.reply_text("❌ You need to add the bot as an administrator to the channel.") update.message.reply_text("❌ You need to add the bot as an administrator to the channel.")
return return
else: else:
chat_id = str(update.message.chat.id) chat_id = str(update.message.chat.id)
bot.sendChatAction(update.message.chat.id, action=telegram.ChatAction.TYPING) context.bot.sendChatAction(update.message.chat.id, action=telegram.ChatAction.TYPING)
data = feedparser.parse(feed_url) data = feedparser.parse(feed_url)
if "link" not in data.feed: if "link" not in data.feed:
update.message.reply_text("❌ Not a valid feed.") update.message.reply_text("❌ Not a valid feed.")
@ -203,18 +203,18 @@ def subscribe(bot, update, args):
@run_async @run_async
def unsubscribe(bot, update, args): def unsubscribe(update, context):
if not utils.can_use_bot(update): if not utils.can_use_bot(update):
return return
if not args: if not context.args:
update.message.reply_text("❌ No number given, check /rss first.") update.message.reply_text("❌ No number given, check /rss first.")
return return
# Get Chat ID from name if given # Get Chat ID from name if given
if len(args) > 1: if len(context.args) > 1:
chat_name = args[1] chat_name = context.args[1]
try: try:
resp = bot.getChat(chat_name) resp = context.bot.getChat(chat_name)
except telegram.error.BadRequest: except telegram.error.BadRequest:
update.message.reply_text("❌ This channel does not exist.") update.message.reply_text("❌ This channel does not exist.")
return return
@ -223,9 +223,9 @@ def unsubscribe(bot, update, args):
chat_id = str(update.message.chat.id) chat_id = str(update.message.chat.id)
try: try:
n = int(args[0]) n = int(context.args[0])
except ValueError: except ValueError:
update.message.reply_text("❌ No number given.".") update.message.reply_text("❌ No number given.")
return return
chat_hash = feed_hash.format(chat_id) chat_hash = feed_hash.format(chat_id)
@ -343,25 +343,29 @@ def check_feed(bot, key):
@run_async @run_async
def run_job(bot, job=None): def run_job(context):
logger.info("================================") logger.info("================================")
keys = r.keys(feed_hash.format("*:subs")) keys = r.keys(feed_hash.format("*:subs"))
for key in keys: for key in keys:
check_feed(bot, key) check_feed(context.bot, key)
def onerror(bot, update, error): def run_job_manually(update, context):
logger.error(error) run_job(context)
def onerror(update, context):
logger.error("Update \"%s\" caused error \"%s\"", update, context.error)
# Main function # Main function
def main(): def main():
# Setup the updater and show bot info # Setup the updater and show bot info
updater = Updater(token=bot_token) updater = Updater(token=bot_token, use_context=True)
try: try:
logger.info("Starting {0}, AKA @{1} ({2})".format(updater.bot.first_name, updater.bot.username, updater.bot.id)) logger.info("Starting {0}, AKA @{1} ({2})".format(updater.bot.first_name, updater.bot.username, updater.bot.id))
except Unauthorized: except Unauthorized:
logger.critical("Logging in failed, check bot token") logger.critical("Logging in failed, check bot token.")
sys.exit(1) sys.exit(1)
# Register Handlers # Register Handlers
@ -371,7 +375,7 @@ def main():
CommandHandler("rss", list_feeds, pass_args=True), CommandHandler("rss", list_feeds, pass_args=True),
CommandHandler("sub", subscribe, pass_args=True), CommandHandler("sub", subscribe, pass_args=True),
CommandHandler("del", unsubscribe, pass_args=True), CommandHandler("del", unsubscribe, pass_args=True),
CommandHandler("sync", run_job) CommandHandler("sync", run_job_manually)
] ]
for handler in handlers: for handler in handlers:
updater.dispatcher.add_handler(handler) updater.dispatcher.add_handler(handler)