Hey there, fellow developer! Ready to dive into the world of Telegram bots? You're in for a treat. In this guide, we'll walk through creating a Telegram bot using Java. It's easier than you might think, and by the end, you'll have a fully functional bot at your fingertips.
Before we jump in, make sure you've got:
Got all that? Great! Let's get coding.
First things first, let's set up our project:
pom.xml
:<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>6.1.0</version> </dependency>
Now, let's breathe life into our bot:
import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.objects.Update; public class MyAmazingBot extends TelegramLongPollingBot { @Override public String getBotUsername() { return "YourBotUsername"; } @Override public String getBotToken() { return "YourBotToken"; } @Override public void onUpdateReceived(Update update) { // We'll fill this in soon! } }
Time to make your bot responsive:
@Override public void onUpdateReceived(Update update) { if (update.hasMessage() && update.getMessage().hasText()) { String messageText = update.getMessage().getText(); long chatId = update.getMessage().getChatId(); // Handle the message here } }
Let's add some commands:
if (messageText.startsWith("/start")) { sendMessage(chatId, "Welcome! I'm your new bot."); } else if (messageText.startsWith("/help")) { sendMessage(chatId, "I can help you with..."); }
Here's how to send messages back to users:
private void sendMessage(long chatId, String text) { SendMessage message = new SendMessage(); message.setChatId(String.valueOf(chatId)); message.setText(text); try { execute(message); } catch (TelegramApiException e) { e.printStackTrace(); } }
Want to spice things up? Try adding inline keyboards:
private void sendInlineKeyboard(long chatId) { InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup(); List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>(); List<InlineKeyboardButton> rowInline = new ArrayList<>(); rowInline.add(new InlineKeyboardButton().setText("Option 1").setCallbackData("option1")); rowInline.add(new InlineKeyboardButton().setText("Option 2").setCallbackData("option2")); rowsInline.add(rowInline); markupInline.setKeyboard(rowsInline); SendMessage message = new SendMessage(); message.setChatId(String.valueOf(chatId)); message.setText("Choose an option:"); message.setReplyMarkup(markupInline); try { execute(message); } catch (TelegramApiException e) { e.printStackTrace(); } }
Don't forget to handle those pesky errors:
try { // Your code here } catch (Exception e) { logger.error("Error occurred: ", e); }
Test locally first, then deploy to a server. Remember, your bot needs to be always online to respond to users!
And there you have it! You've just created your very own Telegram bot. Pretty cool, right? Remember, this is just the beginning. There's so much more you can do with the Telegram Bot API. So go forth and create something awesome!
Need more info? Check out the official Telegram Bot API documentation. Happy coding!