1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-27 19:17:12 +00:00
Type-safe library for work with Telegram Bot API
Go to file
2019-08-14 10:50:02 +06:00
gradle/wrapper update version of gradle and libs 2019-02-21 12:10:21 +08:00
src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI bot command entity command extracting fix 2019-08-12 17:25:51 +06:00
.gitignore init 2018-12-26 16:21:52 +08:00
build.gradle "implementation" replaced by "api" in build gradle 2019-07-23 07:34:12 +08:00
CHANGELOG.md update ktor 2019-08-12 18:03:38 +06:00
gradle.properties update ktor 2019-08-12 18:03:38 +06:00
gradlew init 2018-12-26 16:21:52 +08:00
gradlew.bat init 2018-12-26 16:21:52 +08:00
LICENSE init 2018-12-26 16:21:52 +08:00
maven.publish.gradle remove redundant part of maven.publish.gradle 2019-03-21 08:54:57 +08:00
publish.gradle add opportunity to use properties of project in user and key of bintray publication 2018-12-28 10:40:24 +08:00
README.md update README 2019-08-14 10:50:02 +06:00
settings.gradle init 2018-12-26 16:21:52 +08:00

TelegramBotAPI

Download Build Status

What is it?

It is one more project which wish to be useful and full Telegram Bots API bridge for Kotlin. Most part of some specific solves or unuseful moments are describing by official Telegram Bot API.

Compatibility

This version compatible with 29th of July 2019 update of TelegramBotAPI. There is Telegram Passport API exception of implemented functionality, which was presented in August 2018 update of TelegramBotAPI update. It will be implemented as soon as possible. All included and supported API can be found on wiki.

How to implement library?

Common ways to implement this library are presented here. In some cases it will require additional steps like inserting of additional libraries (like kotlin stdlib). In the examples will be used variable telegrambotapi.version, which must be set up by developer. Available versions are presented on bintray, next version is last published:

Download

Maven

<dependency>
  <groupId>com.github.insanusmokrassar</groupId>
  <artifactId>TelegramBotAPI</artifactId>
  <version>${telegrambotapi.version}</version>
</dependency>

Gradle

implementation "com.github.insanusmokrassar:TelegramBotAPI:${telegrambotapi.version}"

Gradle (old)

compile "com.github.insanusmokrassar:TelegramBotAPI:${telegrambotapi.version}"

How to work with library?

For now, this library have no some API god-object. Instead of this, this library has several important objects:

Types

Types declare different objects representation. For example, Chat for now represented as interface and has several realisations:

  • PrivateChat
  • GroupChat
  • SupergroupChat
  • ChannelChat

Instead of common garbage with all information as in original Chat, here it was separated for more obvious difference between chats types and their possible content.

The same principle work with a lot of others things in this Telegram bot API.

Requests

Requests usually are very simple objects, but some of them are using their own build factories. For example, the next code show, how to get information about bot:

val requestsExecutor: RequestsExecutor = ...
requestsExecutor.execute(GetMe())

The result type of GetMe request is User. In fact, in this result must contain isBot equal to true always.

RequestsExecutor

It is base object which can be used to execute requests in API. For now by default included Ktor realisation of RequestsExecutor, but it is possible, that in future it will be extracted in separated project. How to create RequestsExecutor:

val requestsExecutor = KtorRequestsExecutor(TOKEN)

Here KtorRequestsExecutor - default realisation with Ktor. TOKEN is just a token of bot which was retrieved according to instruction.

Besides, for correct usage of this, you must implement in your project both one of engines for client and server Ktor libraries:

dependencies {
    // ...
    implementation "io.ktor:ktor-server-cio:$ktor_version"
    implementation "io.ktor:ktor-client-okhttp:$ktor_version"
    // ...
}

It is able to avoid using of server dependency in case if will not be used Webhooks. In this case, dependencies list will be simplify:

dependencies {
    // ...
    implementation "io.ktor:ktor-client-okhttp:$ktor_version"
    // ...
}

Here was used okhttp realisation of client, but there are several others engines for Ktor. More information available on ktor.io site for client and server engines.

Getting updates

In this library currently realised two ways to get updates from telegram:

  • Polling - in this case bot will request updates from time to time (you can set up delay between requests)
  • Webhook via reverse proxy or something like this

Updates filters

Currently webhook method contains UpdatesFilter as necessary argument for getting updates. UpdatesFilter will sort updates and throw their into different callbacks. Currently supporting separate getting updates for media groups - they are accumulating with debounce in one second (for being sure that all objects of media group was received).

Updates polling also support UpdatesFilter but you must not use it and can get updates directly in UpdateReceiver, which you will provide to startGettingOfUpdates method

Webhook set up

If you wish to use webhook method, you will need:

  • White IP - your IP address or host, which available for calling. TelegramBotAPI recommend to use some unique address for each bot which you are using
  • SSL certificate. Usually you can obtain the certificate using your domain provider, Let'sEncrypt or create it
  • Nginx or something like this

Template for Nginx server config you can find in this gist.

For webhook you can provide File with public part of certificate, URL where bot will be available and inner PORT which will be used to start receiving of updates. Actually, you can skip passing of File when you have something like nginx for proxy forwarding.

In case of using nginx with reverse-proxy config, setting up of Webhook will look like:

requestsExecutor.setWebhook(
    WEBHOOK_URL,
    INTERNAL_PORT,
    filter,
    ENGINE_FACTORY
)

Here:

  • WEBHOOK_URL - the url which will be used by Telegram system to send updates
  • INTERNAL_PORT - the port which will be used in bot for listening of updates
  • filter - instance of UpdatesFilter, which will be used to filter incoming updates
  • ENGINE_FACTORY - used factory name, for example, CIO in case of usage io.ktor:ktor-server-cio as server engine