Skip to content

sirati/telegrambot-japi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

TelegramBot JavaAPI

I made this fork because marzn's Telegram Bot Api only allows you to run one telegram bot per java classloader (or program in normal environment).

If you have any question feel free to contact me via Telegram.

Project state

This software is still under development and need some more time to be finished. But it is already working nice, so we want to share the software with you. If you want, feel free to contribute.

Changelog 0.9.0.2:

  • Bot is no longer shutting down the hole application if another application stole the connection to the bot api.
  • more reconnect tries.

Changelog 0.9.0.1:

  • Reprogrammed parts of the bot to allow multiple instances.

HowTo get the software

Just download the jar

You can download the first release here. I tested it with my two testbots. It works fine for me.

HowTo use the software

Send a message and receive a message

package de.vivistra.telegrambot.example;

import org.apache.logging.log4j.LogManager;

import de.vivistra.telegrambot.TelegramBot;
import de.vivistra.telegrambot.model.message.Message;
import de.vivistra.telegrambot.model.message.TextMessage;
import de.vivistra.telegrambot.receiver.IReceiverService;

public class BotExample {
	TelegramBot telegramBot;

	public BotExample() {
		// Set API token
		telegramBot = new TelegramBot("<Your TelegramAPI token here>",
				LogManager.getLogger("Telegram Bot 1"));
	}

	// Send a message to you. (You have to message the bot first to allow him to
	// send you messages)
	public void sendMessageToMe() {

		// A Telegram ID. It is a negative Integer for bots and a positive
		// Integer for humans.
		int recipient = 123456;// <Your TelegramID here>;

		// Create a message
		Message message = new TextMessage(recipient, "Hello =)");

		// Send the message
		telegramBot.send(message);
	}

	// First you need to implement the `IReceiverService` interface.
	public class GetMessage implements IReceiverService {

		@Override
		public void received(Message message) {
			switch (message.getMessageType()) {
			case TEXT_MESSAGE:
				String sender = message.getSender().toString();

				String text = message.getMessage().toString();

				System.out.println(sender + " wrote: " + text);

				break;
			default:
				System.out.println("Ignore received message.");
			}
		}
	}

	// Now we will create a object of our `GetMessage` and subscribe the
	// `Receiver`. Our method `received` will be called every time a `Message`
	// was received.
	public void subscribe() {
		// Create an IReceiverService object
		GetMessage getMessage = new GetMessage();

		// Subscribe the receiver
		telegramBot.subscribe(getMessage);
	}
}

About

TelegramBot JavaAPI

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%