lateximgbot

Latex image Telegram bot
Log | Files | Refs | README | LICENSE

commit c54b74d7b4c6368096977317a824b72867ce94f1
parent 01b579dd25a21f46ff588de547a40906338ac49d
Author: Byron Torres <b@torresjrjr.com>
Date:   Wed,  2 Jun 2021 21:56:05 +0100

Add VERSION and /about

Diffstat:
Mbot.py | 62+++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 43 insertions(+), 19 deletions(-)

diff --git a/bot.py b/bot.py @@ -17,13 +17,14 @@ import random logging.basicConfig( filename="log", filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', - datefmt="%H:%M:%S", level=logging.INFO, ) # CONSTANTS +VERSION = "0.0.1" + try: with open('token') as TokenFile: TOKEN = TokenFile.read().splitlines()[0] @@ -55,7 +56,7 @@ def logger_dec(old_cb_func): def new_cb_func(upd, ctx): msg = upd.message.text name = upd.message.from_user.name - log_msg = f"{dt_now()} :: {name}\t:: {msg}" + log_msg = f"{name}\t:: {msg}" logging.info(log_msg) print(log_msg) @@ -75,7 +76,6 @@ def get_random_example(): else: total_examples = int( line.split()[1] ) example_number = random.randint(1, total_examples) - print("Example number", example_number) for line in File: if line.startswith(f"%BEGIN {example_number}"): @@ -97,25 +97,20 @@ def get_random_example(): @logger_dec def cb_start(upd, ctx): - first_name = upd.message.from_user.first_name - out_msg = f"""\ -Hello, {first_name} -Type some _LaTeX_ and this bot will return a rendered image of it. - -Use /link _(with some LaTeX)_ to also return an image link. - -This bot aims to support inline queries in the future. - -Try the following: + bot_username = ctx.bot.get_me().username.replace('_', '\\_') -``` -{EXAMPLE_LATEX} -``` + # outgoing text + out_msg = f"""\ +@{bot_username} renders _LaTeX_ text and formulae. -Or use /random for examples. +Try: `\\texttt{{Hello, world!}}` -Author: t.me/torresjrjr +See /random for examples +See /help for more +See /about """ + + # send message upd.message.reply_text( out_msg, parse_mode=telegram.ParseMode.MARKDOWN, @@ -127,6 +122,34 @@ def cb_help(upd, ctx): @logger_dec +def cb_about(upd, ctx): + """ + /about callback + """ + bot_username = ctx.bot.get_me().username.replace('_', '\\_') + + # Outgoing text + out_msg = f"""\ +@{bot_username} (@LatexImgBot\_updates) + +Version + {VERSION} +Source code + https://sr.ht/~torresjrjr/linkchanbot +Maintainer + @torresjrjr <b@torresjrjr.com> +License + GNU Affero General Public License +""" + + # Send message + upd.message.reply_text( + out_msg, + parse_mode=telegram.ParseMode.MARKDOWN, + ) + + +@logger_dec def cb_random(upd, ctx): latex = get_random_example() @@ -171,7 +194,7 @@ cb_handler = lambda upd, ctx: handler(upd, ctx) def cb_admin(upd, ctx): msg = upd.message.text name = upd.message.from_user.name - log_msg = f"{dt_now()} :: {name}\t:: {msg}" + log_msg = f"{name}\t:: {msg}" print(log_msg) username = upd.message.from_user.username @@ -211,6 +234,7 @@ def main(): dp.add_error_handler(cb_error) dp.add_handler(CommandHandler('start' , cb_start)) dp.add_handler(CommandHandler('help' , cb_help)) + dp.add_handler(CommandHandler('about' , cb_about)) dp.add_handler(CommandHandler('link' , cb_link)) dp.add_handler(CommandHandler('random' , cb_random)) dp.add_handler(CommandHandler('admin' , cb_admin))