telegram-mathbot/mathbot/bot.py
2018-01-18 09:09:39 +01:00

43 lines
1.2 KiB
Python

from telegram.ext import RegexHandler
from .rendering import get_png
from .static import static_content
def handle_exception_in_get_png(bot, update, exception, text):
update.message.reply_text("**Error** while processing '__{}__'.\n\nException was: **{}**.".format(text, exception), parse_mode = "Markdown")
def render_math(bot, update):
text = update.message.text[len("/rmath"):]
tex = "${}$".format(text)
try:
png = get_png(tex)
except Exception as e:
handle_exception_in_get_png(bot, update, e, tex)
return
update.message.reply_photo(png, caption = "{}".format(text))
def render_math_caption(bot, update):
text = update.message.text[len("/rmath"):]
if( not ("<" in text and ">" in text)):
update.message.reply_text("**Error**: missing caption seperators: < and >", parse_mode = "Markdown")
return
caption = text[text.index("<") + 1:text.index(">")]
text = text[text.index(">") + 1:]
tex = "${}$".format(text)
try:
png = get_png(tex)
except Exception as e:
handle_exception_in_get_png(bot, update, e, tex)
return
update.message.reply_photo(png, caption = "{}\n\nLaTeX: {}".format(caption, text))
def help(bot, update):
update.message.reply_text(static_content.help_text, parse_mode = "Markdown")