A lightweight, feature-rich wrapper for the Telegram Bot API, providing a handy Kotlin DSL to quickly build your bot.

Overview

Kotlin Telegram Bot

Kotlin based wrapper over Telegram API.

Current version of the Telegram Api: 6.0

Principles

Annotations Magic.

The basic interaction with the library is done on the basis of annotations, which simplifies working with commands and gives the possibility to separate by context.
It is very similar to the Spring RestController.

The main goal was to reduce the time spent on commands routing.

Less time to the bot interface - More time to the business logic behind it.

Create application - Put annotation on - Write business logic.
This keeps development to a minimum of interactions with the library interface, and allows you to focus on the business logic.

The interface should be lightweight.

The library interface is intentionally not complicated by all sorts of filters, steps, exception handling and other all sorts of features to not complicate the basic processing as the library API already gives the opportunity to build the necessary actions yourself.

Installation

Add the JitPack repository to your root build.gradle.kts file:

repositories {
    maven("https://jitpack.io")
}

Now add the library itself to the dependencies' module that you need it.

dependencies {
    implementation("com.github.vendelieu:telegram-bot:1.1.0")
}

Samples

You can see the samples in the samples' folder there you can find:

  • Conversation - An example of using Inputs and storing data in UserData.
  • Echo - Echo bot :)
  • Exception-handling - Simple example of exception handling
  • Ktor-webhook - An example of using webhook with Ktor
  • Poll - An example of how to build a bot questionnaire.

Usage

suspend fun main() {
    val bot = TelegramBot("BOT_TOKEN", object {}.javaClass.`package`)
    /**
     * Second parameter is the package in which commands/inputs will be searched.
     * In this case it will take the current one for the class in which the bot is running.
     * If your main function is in a class, you can just use javaClass.`package` instead of declaring an object.
     */

    bot.update.setListener { // set long-polling listener and use defined action over updates. 
        bot.update.handle(it)
    }
}

@TelegramCommand(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
    message { "Hello" }.send(to = user, via = bot)
}

for webhook handling you can use any server and use bot.update.handle and for set webhook you can use this method:

setWebhook("https://site.com").send(bot)

also it is possible directly instantiate actions

SendMessageAction("Hello").send(to = user, via = bot)

and there's some methods applicable depending on the action:

message { "Hello *user*! \$hello" }.options { parseMode = ParseMode.Markdown }.markup {
    // this method takes all Keyboard types: ReplyKeyboardMarkup, InlineKeyboardMarkup, ForceReply, ReplyKeyboardRemove.
    inlineKeyboardMarkup {
        "testButton" callback "callbackPoint"
    } // this is InlineKeyboardMarkup builder interface, but you can directly create your own markup.
    /**
     * InlineKeyboardMarkup(InlineKeyboardButton("test", callbackData = "testCallback"))
     */
}.entities {
    arrayOf(MessageEntity(EntityType.Cashtag, 14, 5))
}.send(user, bot)

if you want to operate with response you can use sendAsync() instead of send() method, which returns Response:

message { "test" }.sendAsync(user, bot).await().onFailure {
    println("code: ${it.errorCode} description: ${it.description}")
}

Any async request returns a Response on which you can also use methods getOrNull() or isSuccess().

Input waiting

The library also provides an input waiting interface that can be used for multistep logic:

@TelegramCommand(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
    message { "Hello, please enter your name:" }.send(to = user, via = bot)
    bot.input.set(user.id, "nameInputCatch")
}

@TelegramInput(["nameInputCatch"])
suspend fun nameInputCatch(update: ProcessedUpdate, bot: TelegramBot) {
    message { "My name is Adam, nice to meet you ${update.text}!" }.send(update.user, bot)
}

The basic implementation of waiting inputs uses synchronized(WeakHashMap<>)
but for custom use we still recommend you to use your own implementation of storage based on your preferred tools (redis/caffeine/etc.).
All you have to do is implement BotWaitingInput interface.

Handling unhandled :)

It is also possible to process updates that have not been processed by any command or input.

@TelegramUnhandled
suspend fun unhandledUpdates(user: User, bot: TelegramBot) {
    message {
        "The bot does not understand what you want him to do."
    }.send(user, bot)
}

Callback processing

With the callback you can pass data in the format query parameters,
e.g. greeting?name=Adam in this example greeting is a command and everything after ? is data
in the format of key=value with & separator.

you must use the parameter name in the target function or use the @TelegramParameter annotation to pass the data correctly.

The correct target function for our command greeting?name=Adam would be this example:

@TelegramCommand(["greeting"])
suspend fun greeting(name: String, user: User, bot: TelegramBot) {
    message { "Hello, $name" }.send(to = user, via = bot)
}

or with @TelegramParameter annotation:

@TelegramCommand(["greeting"])
suspend fun greeting(@TelegramParameter("name") anyParameterName: String, user: User, bot: TelegramBot) {
    message { "Hello, $anyParameterName" }.send(to = user, via = bot)
}

The use of @TelegramParameter annotation is also very helpful
when you need to send a large amount of data because the Telegram API has a limit of 64 characters in the callback.\

So you can abbreviate the variable names in the callback itself and use clear readable names in the code.

Supported value types

Values passed through the callback can only be of certain java base types as:

  • String
  • Integer
  • Long
  • Short
  • Float
  • Double

If a parameter was declared as a certain type, but the passed value does not match, then null will be passed.

Magical objects

After explaining the magic of transferring data through the callback, a reasonable question may arise,
what are the objects User, TelegramBot, ProcessedUpdate and how do they get to the target function
if they are not specified anywhere in the callback, in the command or in the input?

The point is that these are internal library magic objects that are automatically passed to the target function if they have been declared.
That is, if you need them, you can declare, and they will be transferred, if not, and there will be no problem.

  • ProcessedUpdate - current update processed by bot.
  • User - user object, for those cases when we do not need to use all update, but only user to send him a message.
  • And finally TelegramBot, this is the instance of the current bot required for sending actions.

Inline buttons builder

Also for inline buttons there is a handy markup build interface.

inlineKeyboardMarkup {
    "name" callback "callbackData"         //---\
    "buttonName" url "https://google.com"  //--- these two buttons will be in the same row.
    newLine() // or br()
    "otherButton" webAppInfo "data"       // this will be in other row

    // you can also use a different style within the builder:
    callbackData("buttonName") { "callbackData" }
}

but it also does not prevent you from manually building a markup as seen in the initial example:

InlineKeyboardMarkup(
    InlineKeyboardButton("test", callbackData = "testCallback")
)

Bot Context

The bot can also provide the ability to remember some data through the userData and chatData interfaces.

  • userData is a user-level cache.
  • chatData is a class-level cache, i.e. the cache will be stored until the user moves to a command or input that is in a different class. (userData must be present for this scheme to work correctly)

These tools are not provided by default, but can be implemented through BotUserData and BotChatData interfaces using the data storage tools of your choice.

To install, all you need to do is assign via TelegramBot:

suspend fun main() {
    val bot = TelegramBot("BOT_TOKEN", object {}.javaClass.`package`)
    bot.userData = BotUserDataImpl()
    bot.chatData = BotChatDataImpl()

    bot.update.setListener {
        bot.update.handle(it)
    }
}

@TelegramCommand(["/start"])
suspend fun start(user: User, bot: TelegramBot) {
    bot.userData?.set(user.id, "test", "value") // now we can use it in any part of our code
    message { "Hello" }.send(to = user, via = bot)
}

@TelegramCommand(["/test"])
suspend fun test(user: User, bot: TelegramBot) {
    val testVal = bot.userData?.get(user.id, "test").toString()
    message { "stored value: $testVal" }.send(to = user, via = bot)
}

DI

Also, if you want to use some libraries for Dependency Injection you can redefine the ClassManager interface using your preferred mechanism and pass it on when initializing the bot.

suspend fun main() {
    val bot = TelegramBot("BOT_TOKEN", object {}.javaClass.`package`, classManager = ClassManagerImpl())

    bot.update.setListener {
        bot.update.handle(it)
    }
}

Documentation

You can read more about the methods in the documentation:
https://vendelieu.github.io/telegram-bot/

You might also like...
Pass parameters safely and quickly between activities or fragments.

Bracer Pass parameters safely and quickly between activities or fragments. Read this in other languages: 中文, English, Change Log Prepare Add the JitPa

Get a libGDX-powered Android live wallpaper up and running quickly with this project template

This project is a libGDX Android live wallpaper template. It's the demo application created by the libGDX code generator, remade as a live wallpaper.

The third-party Telegram android app.
The third-party Telegram android app.

The third-party Telegram android app.

Telegram messenger for Android

Telegram messenger for Android Telegram is a messaging app with a focus on speed and security. It’s superfast, simple and free. This repo contains the

Re - Redirect outgoing calls to Signal or Telegram
Re - Redirect outgoing calls to Signal or Telegram

Re Redirect outgoing calls to Signal or Telegram. Tiny app to redirect outgoing

Redirect outgoing calls to Signal/Telegram/Threema.
Redirect outgoing calls to Signal/Telegram/Threema.

Red Redirect outgoing calls to Signal/Telegram/Threema. Tiny app to redirect outgoing calls to Signal/Telegram/Threema if available. You can cancel re

An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc.  & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains along with valuable tips and hacks from mentors for a particular domain.
An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc. & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains along with valuable tips and hacks from mentors for a particular domain.

An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc. & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains along with valuable tips and hacks from mentors for a particular domain.

Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.
Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.

Real time gps location based alarming system tracking the road accident and providing medical assitance plus concern from near by police station.

Asynchronous Yandex.Predictor API wrapper for Kotlin/JVM.

Asynchronous Yandex.Predictor API wrapper for Kotlin/JVM.

Comments
  • Help with API - setMyCommands

    Help with API - setMyCommands

    Hello, I've already tried .js implementations and nodejs http request to get some positive result when using setMyCommands. The request/response says O.K. but nothing was changed (tried several settings with @telegram‧botFather on my bot, as well).

    If telegram-bot can show that setMyCommands works, I would have to switch to Kotlin.

    P.S. clever, the interface BotWaitingInput

    help wanted 
    opened by kdabwl 9
  • fix #5 IllegalArgumentException wrong number of arguments.

    fix #5 IllegalArgumentException wrong number of arguments.

    • keep class instance

    All Submissions:

    • [x] Have you followed the guidelines in our Contributing document?
    • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

    New Feature Submissions:

    1. [x] Does your submission pass tests?
    2. [x] Have you lint your code locally prior to submission?

    Changes to Core Features:

    • [x] Have you added an explanation of what your changes do and why you'd like us to include them?
    • [x] Have you written new tests for your core changes, as applicable?
    • [x] Have you successfully ran tests with your changes locally?
    opened by adi-itgg 0
  • ClassManagerImpl throw IllegalArgumentException wrong number of arguments.

    ClassManagerImpl throw IllegalArgumentException wrong number of arguments.

    The basic ClassicManagerImpl in some environments takes an empty list as an argument and gives IllegalArgumentException wrong number of arguments.

    To Reproduce Steps to reproduce the behavior: I used to get this behavior when trying to deplocate a bot on a heroku.

    Expected behavior The same work of designers in all environments

    bug 
    opened by vendelieu 0
  • Bugs fix & add new feature

    Bugs fix & add new feature

    All Submissions:

    • [x] Have you followed the guidelines in our Contributing document?
    • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

    New Feature Submissions:

    1. [x] Does your submission pass tests?
    2. [x] Have you lint your code locally prior to submission?

    Changes to Core Features:

    • [x] Have you added an explanation of what your changes do and why you'd like us to include them?
    • [x] Have you written new tests for your core changes, as applicable?
    • [x] Have you successfully ran tests with your changes locally?
    opened by adi-itgg 0
Releases(v1.0.0)
Owner
Jey
Jey
A repository that contains various examples of how to use the telegram-bot library.

Telegram-bot templates This repository contains simple different examples of how to use the telegram-bot library. How to use All the examples are conv

Jey 3 Nov 15, 2022
Simple Telegram bot that responds with list of results obtained from Google.

Simple project focused on learning a little more about how Telegram bots work and are created. The bot has a very basic functionality: the user types

Jesus Ericks 3 Nov 13, 2022
A handy phone call manager with phonebook, number blocking and multi-SIM support

Simple Dialer A lightweight app for handling your calls, no matter where are you. Comes with a handy call log for easy call initiation. There is a qui

Simple Mobile Tools 443 Jan 6, 2023
📱 Android Library to implement Rich, Beautiful, Stylish 😍 Material Navigation View for your project with Material Design Guidelines. Easy to use.

Material NavigationView for Android ?? ?? Android Library to implement Rich, Beautiful Material Navigation View for your project with Material Design

Shreyas Patil 198 Dec 17, 2022
Bringing webhooks into Telegram. Push messages to chats via URL with a simple API.

Webhooks over Telegram (WoT) WoT aims to provide a simple API for registering webhooks and sending messages to Telegram chats via them using a single

d1s utils 2 Oct 5, 2022
Matomo wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in the official package.

@mccsoft/react-native-matomo Matomo wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in th

MCC Soft 4 Dec 29, 2022
Synapse Moderation Bot - A bot for managing and moderating our community Discord server

Synapse Moderation Bot A bot for managing and moderating our community Discord server. Note This bot is not public. While you can host it on your own,

Synapse Technologies, LLC 1 Jul 16, 2022
Experimental Discord Mobile Rich Presence (Android)

MRPC Experimental Discord Mobile Rich Presence (Android) How does it work? It's pretty simple. Connect to the Discord Gateway as a normal Discord Clie

Duy Tran Khanh 41 Dec 25, 2022
neutriNote: All-in-one preservation of written thoughts, be those text, math (LaTeX), rich markdown, drawings

Official | FAQ | Documentation | Mastodon | XDA neutriNote (Community Edition) What is neutriNote? In a nutshell, all-in-one preservation of written t

AppML 185 Dec 24, 2022
🎧 Hacky last.fm Discord rich presence

LastFmRichPresence ?? Hacky last.fm Discord rich presence How to use?

MrPowerGamerBR 10 Oct 14, 2022