Wolframalpha Calculator in Telegram Dialog

Idea



In telegram dialogs, I very often use the telegram bot: inlatexbot . It allows you to insert Latex directly into the telegram dialog - this is convenient: the mathematical notation can be sent with a clear picture so that

4∗2x∗13=1024

, not so sqrt (4) * 2 ^ x * 1/3 = 1024 .

But no less often, I need the WolframAlpha functionality in a telegram : calculate a complex bill for a party with friends, various percentages, solve an equation, and so on - the full functionality of WolframAlpha, but at the same time not leave the telegram dialogue.



Wolfram Connection



I found Wolfram Alpha API for python (https://pypi.org/project/wolframalpha/). The documentation is very clear and you can do everything that is available in the web version. True, there is a limitation of the free Wolfram API at 2,000 requests per month. The code that works with tungsten is extremely simple and came out in 4 lines for me.



import wolframalpha
client = wolframalpha.Client('TOKEN')
res = client.query(query)
print(next(res.results).text)


In the first two lines, I import the wolframalpha module, create a client with my APP ID - you must first register with wolfram and get it, then you can send requests to tungsten and just print the result of what Wolfram Alpha received. Everything works - now you can start integrating with Telegram.



Integration into Telegram bot



It remains to connect everything together and send requests received from Telegram to Wolfram and give an answer to the user. I create a bot in @BotFather, turn on inline mode in the settings, so that the bot works right in the Telegram dialog. I use an example of an inline bot from GitHub for Telegram, and send a response to the user. This is what the most important part of the code looks like.



def inlinequery(update, context):
    """Handle the inline query."""
    query = update.inline_query.query
    print(query)
    res = client.query(query)
    results = [
        InlineQueryResultArticle(
            id=uuid4(),
            title="Magic answer is here!",
            input_message_content=InputTextMessageContent(
                "{} => *{}*".format(query, next(res.results).text),
                parse_mode=ParseMode.MARKDOWN))]
    update.inline_query.answer(results)


How it works?



It's very simple - right in the dialog I enter @calcherebot 12 + 15 and it gives me the answer 27 . A bot in a telegram simplifies not only the process of calculating something, but also the process of various proofs, for example, you can find out the population of Moscow or the average weight of a person and the bot in a sense changes communication: screenshots of calculators, Wikipedia or other sources are no longer needed - now everything can be accessed in dialogue.







What's next?



So far, the bot works locally and only supports 2,000 requests per month, which is very small for public use. I plan to revise it for mass use by everyone who needs it: students, scientists, just people who need to calculate or prove something right in the telegram. If you have ideas or suggestions for improvement (most likely they are, since the bot was written in a couple of hours) write to me by mail: rk-helper@yandex.ru



All Articles