Navigation Menu

Skip to content

LeonardoDemartino/telegrambot-japi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TelegramBot JavaAPI

Build state

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.

Stay updated with our changelog.

HowTo get the software

Clone the repository (recommended)

git clone https://github.com/marzn/telegrambot-japi.git

Just download a jar

Have a look at our builds.

HowTo use the software

Send a message

import de.vivistra.telegrambot.model.message.Message;
import de.vivistra.telegrambot.model.message.TextMessage;
import de.vivistra.telegrambot.sender.Sender;
import de.vivistra.telegrambot.settings.BotSettings;

/**
 * This file launches the a telegram bot.
 */
public class Launcher {

	public static void main(String[] args) throws Exception {
		new Launcher();
	}

	private Launcher() throws Exception {

		// Set API token
		BotSettings.setApiToken("<Your TelegramAPI token here>");
		
		// A Telegram ID. It is a negative Integer for bots and a positive Integer for humans.
		int recipient = <Your TelegramID here>;
		
		// Create a message
		Message message = new TextMessage(recipient, "Hello =)");

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

Receive a 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.

private Launcher() throws Exception {

	// Set API token
	BotSettings.setApiToken("<Your TelegramAPI token here>");

	// Create an IReceiverService object
	GetMessage getMessage = new GetMessage();

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

And for sure you can combine these two code examples, this was only how to get started.

Projects using this software

LosungsBot

Tell me, if you wanna have your project listed here =)

About

TelegramBot JavaAPI

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%