Lesson 2. PhotoBot
Our today's mission - create a "photo" bot, that will send user a photo. It is just an example so there there will be no photos from online, no group chat support. Just local pics. But there is a good thing: we will learn how to create custom keyboards, how to send photos and create commands.
Let's respect Telegram's servers
Okey, for a start, let's prepare our pictures. Lets download 5 completely unknown photos. Just look: we will send same files to users many many times, so lets coast our traffic and disk space on Telegram Servers. It is amazing that we can upload our files at their server once and then just send files (photos, audio files, documents, voice messages and etc.) by their unique file_id. Okey then. Now lets know photo's file_id when we will send it to our bot. As always, create a new project in IntelliJ Idea and create two files within the src directory: Main.java and PhotoBot.java. Open up first file and type next:
Dont forget to install TelegramBots library
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.exceptions.TelegramApiException;
public class Main {
public static void main(String[] args) {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
try {
botsApi.registerBot(new PhotoBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
System.out.println("PhotoBot successfully started!");
}
}This code will register our bot print "PhotoBot successfully started!" when it is successfully started. Then, save it and open up PhotoBot.java. Paste the following code from previous lesson:
Dont forget to change
bot usernameandbot tokenif you created another bot.
Now lets update our onUpdateReceived method. We want to send file_id of the picture we send to bot. Lets check if message contains photo object:
We want our bot to send file_id of the photo. Well, lets do this:
Lets take a look:

Amazing! Now we know photo's file_id so we can send them by file_id. Lets make our bot answer with that photo when we send command /pic.
Now bot sends photo like this:

And he can even reply to unknown command!

Now lets take a look at ReplyKeyboardMarkup. We will now create custom keyboard like this:

Well, you already now how to make our bot recognise command. Lets make another if for command /markup.
Remember! When you press the button, it will send to bot the text on this button. For example, if I put "Hello" text on the button, when I press it, it will send "Hello" text for me
Amazing! Now lets teach our bot to react on this buttons:
Now, when user press button with "Row 1 Button 1" text on it, bot will send picture by file_id to user:

And lets add "Hide keyboard" function when user send /hide command to bot. This can be done with ReplyMarkupRemove.
Here is code of our files. You can also find all sources at GitHub repository.
src/Main.java
src/PhotoBot.java
Now you can create and remove custom ReplyMarkup keyboards, create custom commands and send photos by file_id! You are doing very well! Hope to see you soon:)
Last updated
Was this helpful?