All Projects → addo47 → AbilityBots

addo47 / AbilityBots

Licence: GPL-3.0 license
The AbilityBots abstraction and API for building expressive Java Telegram Bots

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to AbilityBots

telresender
A Telegram bot, which resend your message to another account
Stars: ✭ 22 (+83.33%)
Mutual labels:  telegram-api, telegram-bots
gotgbot
Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
Stars: ✭ 178 (+1383.33%)
Mutual labels:  telegram-api, telegram-bots
tdlight-java
Complete Bot and Userbot Telegram library based on TDLib
Stars: ✭ 128 (+966.67%)
Mutual labels:  telegram-api, telegram-bots
Telegram-mailer
Web-application for sending messages to list of users. Use several accounts to avoid ban.
Stars: ✭ 28 (+133.33%)
Mutual labels:  telegram-api, telegram-bots
teleirc
Go implementation of a Telegram <=> IRC bridge for use with any IRC channel and Telegram group
Stars: ✭ 112 (+833.33%)
Mutual labels:  telegram-api, telegram-group
TelegramScraper
Using this tool you can easily add so many members from any group to your group. Less than 2 minutes. Super easy. Time saver. But this tool is only for educational purpose. You could be banned from Telegram. So be careful. Recommanded to use this tool only on Termux.
Stars: ✭ 234 (+1850%)
Mutual labels:  telegram-group, telegram-bots
pmChatBot
A simple feed-back bot written in PHP. Like Livegram. Heroku Support
Stars: ✭ 31 (+158.33%)
Mutual labels:  telegram-api, telegram-bots
Txt2SpeechBot
The only Text to Speech Telegram Inline Bot
Stars: ✭ 26 (+116.67%)
Mutual labels:  telegram-group, telegram-bots
telegram
📚 Golang bindings for Telegram API
Stars: ✭ 15 (+25%)
Mutual labels:  telegram-api, telegram-bots
grouphelperbot
A Telegram Bot made to help group admins, with Italian/English support.
Stars: ✭ 26 (+116.67%)
Mutual labels:  telegram-api, telegram-bots
tdlight-telegram-bot-api
The TDLight Telegram Bot API is an actively enhanced fork of the original Bot API, featuring experimental user support, proxies, unlimited files size, and more.
Stars: ✭ 71 (+491.67%)
Mutual labels:  telegram-api, telegram-bots
echotron
An elegant and concurrent library for Telegram bots in Go.
Stars: ✭ 95 (+691.67%)
Mutual labels:  telegram-api, telegram-bots
Telegram-PHP-App
App base for Telegram bots
Stars: ✭ 14 (+16.67%)
Mutual labels:  telegram-api, telegram-bots
wptelegram
Integrate your WordPress site perfectly with Telegram with full control.
Stars: ✭ 31 (+158.33%)
Mutual labels:  telegram-group, telegram-bots
Go Tdlib
Golang Telegram TdLib JSON bindings
Stars: ✭ 244 (+1933.33%)
Mutual labels:  telegram-api
hidethisbot
An inline Telegram bot to keep your private messages hidden from prying eyes.
Stars: ✭ 44 (+266.67%)
Mutual labels:  telegram-api
Mtproto Core
Telegram API JS (MTProto) client library for browser and nodejs
Stars: ✭ 242 (+1916.67%)
Mutual labels:  telegram-api
Core
PHP Telegram Bot based on the official Telegram Bot API
Stars: ✭ 2,899 (+24058.33%)
Mutual labels:  telegram-api
tgmount
Mount Telegram dialogs and channels as a Virtual File System.
Stars: ✭ 52 (+333.33%)
Mutual labels:  telegram-api
TeleBot
A minimal library to develop your new Telegram bot 🐘
Stars: ✭ 40 (+233.33%)
Mutual labels:  telegram-api

IMPORTANCE NOTICE

This module is now part of the official Telegram Java SDK. As such, please refer to that page for the latest updates on AbilityBot! The module in this repository is now maintained in the official SDK. Thanks to everyone who has helped in the making of this project. See you on the next update! ^^

abilitybots

Build Status Jitpack JavaDoc Telegram ghit.me

Usage

Maven & Gradle - JitPack

Plain imports - Here

Motivation

Ever since I've started programming bots for Telegram, I've been using the Telegram Bot Java API. It's a basic and nicely done API that is a 1-to-1 translation of the HTTP API exposed by Telegram.

Dealing with a basic API has its advantages and disadvantages. Obviously, there's nothing hidden. If it's there on Telegram, it's here in the Java API. When you want to implement a feature in your bot, you start asking these questions:

  • The WHO?
    • Who is going to use this feature? Should they be allowed to use all the features?
  • The WHAT?
    • Under what conditions should I allow this feature?
    • Should the message have a photo? A document? Oh, maybe a callback query?
  • The HOW?
    • If my bot crashes, how can I resume my operation?
    • Should I utilize a DB?
    • How can I separate logic execution of different features?
    • How can I unit-test my feature outside of Telegram?

Every time you write a command or a feature, you will need to answer these questions and ensure that your feature logic works.

Ability Bot Abstraction

After implementing my fifth bot using that API, I had had it with the amount of boilerplate code that was needed for every added feature. Methods were getting overly-complex and readability became subpar. That is where the notion of another layer of abstraction (AbilityBot) began taking shape.

The AbilityBot abstraction defines a new object, named Ability. An ability combines conditions, flags, action, post-action and replies. As an example, here is a code-snippet of an ability that creates a /hello command:

public Ability sayHelloWorld() {
    return Ability
              .builder()
              .name("hello")
              .info("says hello world!")
              .input(0)
              .locality(USER)
              .privacy(ADMIN)
              .action(ctx -> sender.send("Hello world!", ctx.chatId()))
              .post(ctx -> sender.send("Bye world!", ctx.chatId()))
              .build();
}

Here is a breakdown of the above code snippet:

  • .name() - the name of the ability (essentially, this is the command)
  • .info() - provides information for the command
    • More on this later, but it basically centralizes command information in-code.
  • .input() - the number of input arguments needed, 0 is for do-not-care
  • .locality() - this answers where you want the ability to be available
    • In GROUP, USER private chats or ALL (both)
  • .privacy() - this answers who you want to access your ability
    • CREATOR, ADMIN, or everyone as PUBLIC
  • .action() - the feature logic resides here (a lambda function that takes a MessageContext)
    • MessageContext provides fast accessors for the chatId, user and the underlying update. It also conforms to the specifications of the basic API.
  • .post() - the logic executed after your main action finishes execution

The following is a snippet of how this would look like with the plain basic API.

   @Override
   public void onUpdateReceived(Update update) {
       // Global checks...
       // Switch, if, logic to route to hello world method
       // Execute method
   }

   public void sayHelloWorld(Update update) {
       if (!update.hasMessage() || !update.getMessage().isUserMessage() || !update.getMessage().hasText() || update.getMessage.getText().isEmpty())
           return;
       User maybeAdmin = update.getMessage().getFrom();
       /* Query DB for if the user is an admin, can be SQL, Reddis, Ignite, etc...
          If user is not an admin, then return here.
       */

       SendMessage snd = new SendMessage();
       snd.setChatId(update.getMessage().getChatId());
       snd.setText("Hello world!");

       try {
           sendMessage(snd);
       } catch (TelegramApiException e) {
           BotLogger.error("Could not send message", TAG, e);
       }
   }

I will leave you the choice to decide between the two snippets as to which is more readable, writable and testable.

You can do so much more with abilities, besides plain commands. Head over to our examples to check out all of its features!

Objective

The AbilityBot abstraction intends to provide the following:

  • New feature is a new Ability, a new method - no fuss, zero overhead, no cross-code with other features
  • Argument length on a command is as easy as changing a single integer
  • Privacy settings per Ability - access levels to Abilities! User | Admin | Creator
  • Embedded database - available for every declared ability
  • Proxy sender interface - enhances testability; accurate results pre-release

Alongside these exciting core features of the AbilityBot, the following have been introduced:

  • The bot automatically maintains an up-to-date set of all the users who have contacted the bot
    • up-to-date: if a user changes their Username, First Name or Last Name, the bot updates the respective field in the embedded-DB
  • Backup and recovery for the DB
    • Default implementation relies on JSON/Jackson
  • Ban and unban users from accessing your bots
    • The bot will execute the shortest path to discard the update the next time they try to spam
  • Promote and demote users as bot administrators
    • Allows admins to execute admin abilities

What's next?

I am looking forward to:

  • Provide a trigger to record metrics per ability
  • Implement AsyncAbility
  • Maintain integration with the latest updates on the basic API
  • Enrich the bot with features requested by the community

Examples

Do you have a project that uses AbilityBots? Let us know!

Support

For issues and features, please use GitHub's issues tab.

For quick feedback, chatting or just having fun, please come and join us in our Telegram Supergroup.

Telegram

Credits

This project would not have been made possible had it not been for Ruben's work with the Telegram Bot Java API. I strongly urge you to check out that project and implement a bot to have a sense of how the basic API feels like. Ruben has done a great job in supplying a clear and straightforward API that conforms to Telegram's HTTP API. There is also a chat for that API.

Telegram

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].