Como crear un bot para telegram en Java

Telegram es la herramienta de mensajería mas usada en el mundo probablemente después de Whatsap, como principal ventaja sobre esta ultima tenemos que su API es totalmente gratuita y soporta la creación de bots. Un bot nos permite crear una cuenta o canal de interacción con infinitas posibilidades.
En esta entrada veremos como crear un bot para Telegram en Java usando Spring Boot.
Dependencias
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sacavix</groupId> <artifactId>telegramboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>telegramboot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-spring-boot-starter</artifactId> <version>6.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
La dependencia mas importante y que realmente aporta valor a nuestro bot es:
<dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots-spring-boot-starter</artifactId> <version>6.1.0</version> </dependency>
Código principal
A continuación el código principal de nuestro bot.
import org.springframework.stereotype.Service; import org.telegram.telegrambots.bots.TelegramLongPollingBot; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @Service public class TelegramBoot extends TelegramLongPollingBot { @Override public void onUpdateReceived(Update update) { // Se obtiene el mensaje escrito por el usuario final String messageTextReceived = update.getMessage().getText(); System.out.println("Escribieron en el bot " + messageTextReceived); // Se obtiene el id de chat del usuario final long chatId = update.getMessage().getChatId(); // Se crea un objeto mensaje SendMessage message = new SendMessage(); message.setChatId(chatId); message.setText("Gracias por escribirnos"); try { // Se envía el mensaje execute(message); } catch (TelegramApiException e) { e.printStackTrace(); } } @Override public String getBotUsername() { return "SCVYoutube_bot"; } @Override public String getBotToken() { return "5486119217:AAFBkK9mjjXUQVRPELnt8sQPf3VBMzNpLvs"; } }
En el siguiente video te explicamos todos los detalles en profundidad.
Espero te sea útil y aprendas.