Skip to content

SimonScholz/telegram-bot-java-api

Repository files navigation

Telegram Bot Java API

Build Status Code Coverage Status

This library contains the bot api and a retrofit client with a Jackson converter to get the data.

The domain objects are configured to work properly with Jackson, but should also work fine using GSON.

Jackson is used by default by the Spring framework so it works very well in a Spring or Spring Boot environment.

See https://github.com/SimonScholz/telegram-bot-spring-dmi-wheater and my other telegram repositories for examples.

Using Retrofit as rest client

package de.simonscholz.telegram.bot.api;

import com.jakewharton.retrofit2.adapter.reactor.ReactorCallAdapterFactory;

import de.simonscholz.telegram.bot.api.domain.File;
import de.simonscholz.telegram.bot.api.domain.Message;
import de.simonscholz.telegram.bot.api.domain.TelegramListResponse;
import de.simonscholz.telegram.bot.api.domain.TelegramObjectResponse;
import de.simonscholz.telegram.bot.api.domain.Update;
import reactor.core.publisher.Mono;
import retrofit2.Retrofit;
import retrofit2.Retrofit.Builder;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

public interface TelegramBotClient {

	/**
	 * This method creates a TelegramBotClient instance with the given parameters
	 * and uses the {@link JacksonConverterFactory} for converting JSON to POJOs and
	 * the {@link ReactorCallAdapterFactory} to get io reactor types as return
	 * values.
	 *
	 * @param baseUrl
	 *            url of the bot api, usually https://api.telegram.org/bot
	 * @param botToken
	 *            the secret token for the bot, which can be obtained from BotFather
	 * @return
	 *
	 * @see https://core.telegram.org/bots/api#authorizing-your-bot
	 */
	static TelegramBotClient createReactorJackson(String baseUrl, String botToken) {
		Builder builder = new Retrofit.Builder().addConverterFactory(JacksonConverterFactory.create())
				.addCallAdapterFactory(ReactorCallAdapterFactory.create());

		return builder.baseUrl(baseUrl + botToken + "/").build().create(TelegramBotClient.class);
	}

	@POST("sendMessage")
	@FormUrlEncoded
	Mono<Message> sendMessage(@Field("chat_id") long chatId, @Field("text") String text);

	@POST("sendPhoto")
	@FormUrlEncoded
	Mono<Message> sendPhoto(@Field("chat_id") long chatId, @Field("photo") String photoUrl);

	@GET("getUpdates")
	Mono<TelegramListResponse<Update>> getUpdates();

	@GET("getUpdates")
	Mono<TelegramListResponse<Update>> getUpdates(@Query("offset") int offset);

	@GET("getFile")
	Mono<TelegramObjectResponse<File>> getFile(@Query("file_id") String fileId);

	@GET("getChatMembersCount")
	Mono<TelegramObjectResponse<Integer>> getChatMembersCount(@Query("chat_id") String chatId);
}

How to use the API

You can create a TelegramBotClient instance like this:

String apiUrl = "https://api.telegram.org/bot";
String botToken = "*** your bot api token ***";

TelegramBotClient.createReactorJson(apiUrl, botToken);
  • your bot api token * can be obtained from the BotFahther bot.

In a Spring environment the url and token can be saved as environment variables and be obtained in a configuration class, which creates a TelegramBotClient bean like this:

	@Bean
	public TelegramBotClient getTelegramBotClientDmiWeather(@Value("${bot.api.url}") String apiUrl,
			@Value("${bot.dmiweather.token}") String botToken) {
		return TelegramBotClient.createReactorJson(apiUrl, botToken);
	}

Contributing

Feedback is highly appreciated and pull requests are appreciated even more.

I know this library does not contain all domain objects and not all API methods, but it fits for the needs of my Telegram bots.

So please feel free to ask for enhancements or offer pull requeusts for missing features.

About

This library contains the bot api and a retrofit client to get the data

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages