Home / Java / Come creare un bot per Telegram: Hello World

Come creare un bot per Telegram: Hello World

Vediamo velocemente come creare un Bot per Telegram scritto in Java, usando JTeleBot.

Struttura file:

telegram-bot-structure

pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>hello-word-bot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>io.github.nixtabyte.telegram</groupId>
<artifactId>jtelebot-core</artifactId>
<version>0.1.0</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

</dependencies>
</project>

log4j.properties:

log4j.rootLogger=INFO, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %d{HH:mm:ss,SSS} [%t] %-5p %x %C{1} : %m%n

Nella cartella contenente i sorgenti Java…

HelloWorldCommand Class:

package org.example.helloworld;
 
import io.github.nixtabyte.telegram.jtelebot.client.RequestHandler;
import io.github.nixtabyte.telegram.jtelebot.exception.JsonParsingException;
import io.github.nixtabyte.telegram.jtelebot.exception.TelegramServerException;
import io.github.nixtabyte.telegram.jtelebot.request.TelegramRequest;
import io.github.nixtabyte.telegram.jtelebot.request.factory.TelegramRequestFactory;
import io.github.nixtabyte.telegram.jtelebot.response.json.Message;
import io.github.nixtabyte.telegram.jtelebot.server.impl.AbstractCommand;
 
public class HelloWorldCommand extends AbstractCommand {
 
    public HelloWorldCommand(Message message, RequestHandler requestHandler) {
        super(message, requestHandler);
    }
 
    @Override
    public void execute() {
        try {
            TelegramRequest telegramRequest = TelegramRequestFactory.createSendMessageRequest(message.getChat().getId(), "Ciao Mondo!", true, message.getId(), null);
            requestHandler.sendRequest(telegramRequest);
        } catch (JsonParsingException | TelegramServerException e) {
            e.printStackTrace();
        }
    }
}

HelloWorldCommandFactory Class:

package org.example.helloworld;
 
import org.apache.log4j.Logger;
 
import io.github.nixtabyte.telegram.jtelebot.client.RequestHandler;
import io.github.nixtabyte.telegram.jtelebot.response.json.Message;
import io.github.nixtabyte.telegram.jtelebot.server.Command;
import io.github.nixtabyte.telegram.jtelebot.server.CommandFactory;
 
public class HelloWorldCommandFactory implements CommandFactory {
 
    private static final Logger LOG = Logger.getLogger(HelloWorldCommandFactory.class);
    @Override
    public Command createCommand(Message message, RequestHandler requestHandler) {
        LOG.info("MESSAGGIO: "+message.getText());
        return new HelloWorldCommand(message,requestHandler);
    }
}

Main Class:

package org.example.helloworld;
 
import io.github.nixtabyte.telegram.jtelebot.server.impl.DefaultCommandDispatcher;
import io.github.nixtabyte.telegram.jtelebot.server.impl.DefaultCommandQueue;
import io.github.nixtabyte.telegram.jtelebot.server.impl.DefaultCommandWatcher;
 
public class Main {
 
    public static void main(String []args){
        DefaultCommandDispatcher commandDispatcher = new DefaultCommandDispatcher(10,100, new DefaultCommandQueue());
        commandDispatcher.startUp();
         
        DefaultCommandWatcher commandWatcher = new DefaultCommandWatcher(2000,100,"INSERISCI IL TUO TOKEN QUI",commandDispatcher,new HelloWorldCommandFactory());
        commandWatcher.startUp();
    }
}

PS:non dimenticare di inserire il tuo token in questo file.

About Fabio

Esperienza nella progettazione e sviluppo di Applicazioni mobili. Integro le conoscenze acquisite in ambito accademico con un continuo studio di nuove tecnologie che mi permettano di creare applicazioni sempre più affidabili sicure ed efficienti. Venite a trovarmi sul blog Fabiosoft Google+
Scroll To Top
Si prega di attivare i Javascript! / Please turn on Javascript!

Javaskripta ko calu karem! / Bitte schalten Sie Javascript!

S'il vous plaît activer Javascript! / Por favor, active Javascript!

Qing dakai JavaScript! / Qing dakai JavaScript!

Пожалуйста включите JavaScript! / Silakan aktifkan Javascript!