Fig. 01 This is a pizza

How to build a Telegram BOT to suggest you pizza

Giulio Michelon
3 min readMar 22, 2016

Last night I was about to order pizza with my sister, and we had the same, never-ending problem. What pizza should I get?

Didn't I already told you? I'm Italian. This means that pizza here is a ritual. We have so much choice. That's why we still struggle deciding the perfect ingredients match. There's too much offer, moreover we have a very bad memory.

So we had an idea: we should code a Telegram bot that suggest us the more delicious pizza, so we don't struggle anymore!

You know, first world problems.

First step: create a bot

This is a very easy step. Open Telegram and search for the BotFather. Yes, this story is full of stereotypes about Italy.

I'm gonna make you a bot you can't refuse

The BotFater is a bot as well, so it responds to some commands, like /newbot, which starts the creation of a bot. Follow the instruction provided from the BotFather and you will be good to go.

At the end of the questions about you and your bot the BotFather will give you an API key.

Save the key somewhere safe and don't share it with anyone. That's the key to access your bot from any programming language and give him instructions.

Let's give instructions to the bot

Since I'm studying Java for a college exam I decided to use Java as programming language for the bot. You can use anything else you are handy with.

I found this useful library: https://github.com/pengrad/java-telegram-bot-api which provides an handy API in Java for Telegram. Awesome! Add it via Maven to your project and you are good to go.

I'm using NetBeans to do that and you can find the option to add it in the Dependencies folder.

I created a Main java file and I added an instruction for the bot: say "I love Pizza" on a public channel I created.

public class Main {
public static void main(String[] args) {
//create a new Telegram bot object to start talking with Telegram
TelegramBot bot = TelegramBotAdapter.build(“HERE YOUR API KEY”);
bot.sendMessage(“@pizzaciaopizza”, “I love Pizza”);
}
}

This worked. Good start. Thankfully my bot loves pizza.

I wanted to enable my bot to answer a command like "/recommendPizza" and to answer something. This sounds trivial, but it's not, and I'm still looking into a simple server engine to do that. Therefore the tutorial ends here.

Do you have any suggestion? Maybe there's a better language or environment to do what I'm trying to do.

Let me know!

--

--