From 472d1f09d9e90d8069adb34f2cff512cef073edf Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 11 May 2019 21:17:05 +0200 Subject: [PATCH] Use HTML parse mode and properly escape stuff --- bot.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index ec88277..68aeb6f 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio +import html from os import environ from collections import namedtuple @@ -19,12 +20,13 @@ logger = logging.getLogger(__name__) Xkcd = namedtuple('Xkcd', ['title', 'link', 'transcript', 'alt', 'number']) URL_FMT_STR = "http://www.ohnorobot.com/index.php?Search=Search&comic=56&s={}" -# FIXME HTML parsemode + escaping -MSG_FMT_STR = "[{number}]({link}): **{title}**\n\n__{alt}__" +MSG_FMT_STR = '{number}: {title}\n\n{alt}' XKCD_JSON_FMT_STR = "https://xkcd.com/{}/info.0.json" MAX_SEARCH_RESULTS = 10 bot = TelegramClient('xkcd', 6, 'eb06d4abfb49dc3eeb1aeb98ae0f581e') +bot.parse_mode = 'html' + session = None # set later # blockquote element -> Xkcd @@ -85,7 +87,12 @@ async def inlinequery(event): result = await asyncio.gather(*(builder.article( title=xkcd.title, url=xkcd.link, - text=MSG_FMT_STR.format(number=xkcd.number, link=xkcd.link, title=xkcd.title, alt=xkcd.alt) + text=MSG_FMT_STR.format( + number=xkcd.number, + link=xkcd.link, + title=html.escape(xkcd.title), + alt=html.escape(xkcd.alt) + ) ) for xkcd in await get_xkcds(event.text))) # FIXME get_xkcds returns duplicates, which lead to the same result ID