telegram-mathbot/mathbot/bot.py

98 lines
3.3 KiB
Python
Raw Normal View History

2018-01-18 09:50:14 +00:00
# Copyright (c) 2018 Daniel Knüttel #
# #
# This file is part of mathbot. #
# #
# mathbot is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# mathbot is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with mathbot. If not, see <http://www.gnu.org/licenses/>. #
# #
2018-01-18 08:09:39 +00:00
from telegram.ext import RegexHandler
from .rendering import get_png
from .static import static_content
2018-01-18 09:47:45 +00:00
import logging
logger = logging.getLogger(__name__)
2018-01-18 08:09:39 +00:00
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"):]
2018-01-18 09:47:45 +00:00
tex = text
2018-01-18 08:09:39 +00:00
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):
2018-01-18 21:03:40 +00:00
text = update.message.text[len("/rcmath"):]
2018-01-18 08:09:39 +00:00
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:]
2018-01-18 09:47:45 +00:00
tex = text
2018-01-18 08:09:39 +00:00
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))
2018-01-18 21:03:40 +00:00
def substitute_render_math(bot, update):
text = update.message.text[len("/rsmath"):]
tex = text.replace("/", "\\")
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(tex))
def substitute_render_math_caption(bot, update):
text = update.message.text[len("/rscmath"):]
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 = text.replace("/", "\\")
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, tex))
2018-01-18 08:09:39 +00:00
def help(bot, update):
update.message.reply_text(static_content.help_text, parse_mode = "Markdown")