https://github.com/simonscholz/telegram-bot-java-api
This library contains the bot api and a retrofit client to get the data
https://github.com/simonscholz/telegram-bot-java-api
domain java java-8 retrofit2 spring telegram telegram-bot telegram-bot-api
Last synced: 10 months ago
JSON representation
This library contains the bot api and a retrofit client to get the data
- Host: GitHub
- URL: https://github.com/simonscholz/telegram-bot-java-api
- Owner: SimonScholz
- License: epl-2.0
- Created: 2018-02-20T23:29:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-22T22:37:19.000Z (over 7 years ago)
- Last Synced: 2025-04-14T04:54:17.389Z (about 1 year ago)
- Topics: domain, java, java-8, retrofit2, spring, telegram, telegram-bot, telegram-bot-api
- Language: Java
- Size: 132 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- Contributing: CONTRIBUTING.adoc
- License: LICENSE
Awesome Lists containing this project
README
= Telegram Bot Java API
image:https://travis-ci.org/SimonScholz/telegram-bot-java-api.svg?branch=master["Build Status", link="https://travis-ci.org/SimonScholz/telegram-bot-java-api"]
image:https://codecov.io/gh/SimonScholz/telegram-bot-java-api/branch/master/graph/badge.svg["Code Coverage Status", link="https://codecov.io/gh/SimonScholz/telegram-bot-java-api"]
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
[source,java]
----
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 sendMessage(@Field("chat_id") long chatId, @Field("text") String text);
@POST("sendPhoto")
@FormUrlEncoded
Mono sendPhoto(@Field("chat_id") long chatId, @Field("photo") String photoUrl);
@GET("getUpdates")
Mono> getUpdates();
@GET("getUpdates")
Mono> getUpdates(@Query("offset") int offset);
@GET("getFile")
Mono> getFile(@Query("file_id") String fileId);
@GET("getChatMembersCount")
Mono> getChatMembersCount(@Query("chat_id") String chatId);
}
----
== How to use the API
You can create a TelegramBotClient instance like this:
[source, java]
----
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.
See https://core.telegram.org/bots/api#authorizing-your-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:
[source, java]
----
@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.