Exemple #1
0
def listener(messages):
    """
    When new messages arrive TeleBot will call this function.
    """
    for m in messages:
        chatid = m.chat.id
        if m.content_type == 'text':
            text = m.text
            if text[0:7] == "/latex ":
                text = text[7:]
            elif text[0] == "@":
                text = text[13:]
            else:
                break

            tb.send_chat_action(chatid,'upload_document')

            filename = 'resultado' + current_thread().name

            LaTeX2IMG.main(['LaTeX2IMG',text,filename,'webp'])

            with open(filename + '.webp','rb') as equation:
                tb.send_sticker(chatid, equation)
Exemple #2
0
def listener(messages):
    """
    When new messages arrive TeleBot will call this function.
    """
    for m in messages:
        chatid = m.chat.id
        if m.content_type == 'text':
            text = m.text
            if text.startswith("/start "):
                tb.reply_to(m,"¡Bienvenido a Sympybot!\n \
                Tienes varios comandos disponibles:\n \
                /numeric [expresion]\n \
                /symbolic [expresion]\n \
                /plot [expresion]\n \
                /help o /ayuda\
                ")
                break
            elif text.startswith("/help") or text.startswith("/ayuda"):
                tb.reply_to(m,"Aquí tienes los comandos disponibles:\n \
                /numeric [expresion] -> evalúa la expresión\n \
                /symbolic [expresion] -> igual que numeric pero sin evaluar a flotante\n \
                /plot [expresion] -> devolverá una representación gráfica de una expresión\
                ")
                break
            elif text.startswith("/symbolic "):
                text = text.split("/symbolic ")[-1]
                command = "symbolic"
            elif text.startswith("/numeric "):
                text = text.split("/numeric ")[-1]
                command = "numeric"
            elif text.startswith("/plot "):
                text = text.split("/plot ")[-1]
                command = "plot"
            else:
                break

            tb.send_chat_action(chatid,'upload_document')

            filename = 'resultado' + current_thread().name

            ###########
            # Calculus or plot
            if command == "plot":
                try:
                    functions = text.split(" ")
                    p = plot(*functions,show=False,title=text,ylabel="")
                except SyntaxError:
                    tb.reply_to(m,"Sintaxis inválida")
                except:
                    tb.reply_to(m,"Error desconocido " + str(sys.exc_info()))
                else:
                    p.save(filename + '.png')
                    with open(filename + '.png','rb') as image:
                        tb.send_photo(chatid,image)
            else:
                try:
                    if command == "numeric":
                        output = latex(sympify(text).evalf())
                    else:
                        output = latex(sympify(text))
                except ValueError:
                    tb.reply_to(m,"No has escrito bien la expresión")
                except:
                    tb.reply_to(m,"Error desconocido " + str(sys.exc_info()))
                else:
                    LaTeX2IMG.main(['LaTeX2IMG',output,filename,'webp'])

                    with open(filename + '.webp','rb') as result:
                        tb.send_sticker(chatid, result)