Telegram bot to code and run programs in Julia

I would like to share with you a simple program to generate Telegram-bot for code and run the programs.

First, you need to generate a Telegram bot via the @BotFather bot ( send the message /newbot to @BotFather and follow the instructions). Then you need to obtain the token of your new bot. For this, in the @BotFather bot, you send the message /token and follow the instructions.

Once you have your bot, a simple way to code inside telegram is to run the following program (based on Telegram Package):

using Telegram, Telegram.API

tg = TelegramClient(“Your_Token”)

run_bot() do msg
try
x = eval(Meta.parse(msg.message.text))

    if typeof(x) == Plots.Plot{Plots.GRBackend}
        savefig("test.png")
        sendPhoto(photo = open("test.png", "r"), chat_id = msg.message.chat.id)
    else
        sendMessage(text = "$x", chat_id = msg.message.chat.id)
    end
catch
    
end

end

Here the plot works only with Plots package, but you can modify it to make it works width other packages.

Then, you need just open a conversation with the bot. If you send a message that is a Julia code, it will evaluate it on your computer and return the result as a message in telegram.

It is not a perfect program but is really simple and useful (for example for teaching and advising).

Note: If you want to make a group with your new bot such that everybody can make a contribution, you need to give the bot admin privileges.

2 Likes