Come creare un bot per Telegram: Hello World

      Nessun commento su 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

[code lang=”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>
[/code]

log4j.properties:

[code lang=”plain”]
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

[/code]

Nella cartella contenente i sorgenti Java…

HelloWorldCommand Class:

[code lang=”java”]
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();
}
}
}

[/code]

HelloWorldCommandFactory Class:

[code lang=”java”]
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);
}
}
[/code]

Main Class:

[code lang=”java”]
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();
}
}
[/code]

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