All Projects → WebPajooh → TeleBot

WebPajooh / TeleBot

Licence: MIT license
A minimal library to develop your new Telegram bot 🐘

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to TeleBot

Core
PHP Telegram Bot based on the official Telegram Bot API
Stars: ✭ 2,899 (+7147.5%)
Mutual labels:  telegram-api, php-telegram-bot
Mtproto Core
Telegram API JS (MTProto) client library for browser and nodejs
Stars: ✭ 242 (+505%)
Mutual labels:  telegram-api
Kepka
Unofficial Telegram Desktop messaging app
Stars: ✭ 109 (+172.5%)
Mutual labels:  telegram-api
Micro Bot
🤖 Zero-configuration Telegram bot runner
Stars: ✭ 173 (+332.5%)
Mutual labels:  telegram-api
Madelineproto
Async PHP client/server API for the telegram MTProto protocol
Stars: ✭ 1,776 (+4340%)
Mutual labels:  telegram-api
Telegramapi
Java library to create Telegram Clients
Stars: ✭ 198 (+395%)
Mutual labels:  telegram-api
Mattata
A powerful, plugin-based, multi-purpose Telegram bot designed to serve a wide variety of purposes
Stars: ✭ 107 (+167.5%)
Mutual labels:  telegram-api
tdlib-binaries
prebuilt TDLib binaries
Stars: ✭ 26 (-35%)
Mutual labels:  telegram-api
Sentry Telegram
Plugin for Sentry which allows sending notification via Telegram messenger.
Stars: ✭ 168 (+320%)
Mutual labels:  telegram-api
Magento Chatbot
Magento Chatbot Integration with Telegram, Messenger, Whatsapp, WeChat, Skype and wit.ai.
Stars: ✭ 149 (+272.5%)
Mutual labels:  telegram-api
Telegram media downloader
Download media files from a telegram conversation/chat/channel up to 2GiB
Stars: ✭ 113 (+182.5%)
Mutual labels:  telegram-api
Pyrogram
Telegram MTProto API Client Library and Framework in Pure Python for Users and Bots
Stars: ✭ 2,252 (+5530%)
Mutual labels:  telegram-api
Telegram Bot Api
First Telegram Bot API node.js library
Stars: ✭ 205 (+412.5%)
Mutual labels:  telegram-api
Novagram
An Object-Oriented PHP library for Telegram Bots
Stars: ✭ 112 (+180%)
Mutual labels:  telegram-api
Go Tdlib
Golang Telegram TdLib JSON bindings
Stars: ✭ 244 (+510%)
Mutual labels:  telegram-api
Grammers
(tele)gramme.rs - use Telegram's API from Rust
Stars: ✭ 109 (+172.5%)
Mutual labels:  telegram-api
Tdl
Node.js bindings to TDLib.
Stars: ✭ 177 (+342.5%)
Mutual labels:  telegram-api
ilovepdf
Telegram Bot that helps you to convert Images to pdf, pdf to images, 45+ file formats to pdf, more features Soon..
Stars: ✭ 140 (+250%)
Mutual labels:  telegram-api
Python Telegram
Python client for the Telegram's tdlib
Stars: ✭ 246 (+515%)
Mutual labels:  telegram-api
Botgram
⚙️ Microframework to build Telegram bots
Stars: ✭ 215 (+437.5%)
Mutual labels:  telegram-api

Latest Stable Version Total Downloads PHP Version Require License

TeleBot

A minimal library to develop your new Telegram bot

Installation

composer require webpajooh/telebot

How to use

Start point

We start by creating an instance of TeleBot class:

try {
    $tg = new TeleBot('YOUR_BOT_TOKEN');
} catch (Throwable $th) {...}

Get the update object

There are short ways to access the update object and some important fields. I recommend you to read the official documentation to understand these objects well.

$tg->update
$tg->message
$tg->chat
$tg->user

You also can use hasCallbackQuery() method, when you want to check if the update object has a callback_query field.

Methods

Thanks to magic methods, we can use API methods without implementing them, and just call them by name and pass an array as parameter:

$tg->editMessageText([...])

Router

You may define some routes to your bot features; define them by listen() method:

$tg->listen('/start', function () use ($tg) {
    $tg->sendMessage([
        'chat_id' => $tg->user->id,
	'text' => 'Hello, world!',
    ]);
}, false);

The third parameter that is true by default, makes you able to terminate the script after running a command. In the previous example we passed false so script continues.

You can also get parameters and use them as variables:

$tg->listen('set_age_%d', function ($age) use ($tg) {
    // TODO
});

TeleBot translates them to regex, so it will be good to take a look at this table to know how to use them efficiently:

Type TeleBot Regex
Digits %d (\d+)
String (Anything but a whitespace) %s (\S+)
Character %c (\S)
Everything including an empty string %p (.*)

Logger

Use this if you need to log something into a log.txt file:

Logger::log($tg->user->id);
tl($tg->user->id); // Does the same thing

Keyboard

TeleBot includes two classes for making keyboards; InlineKeyboard and ReplyKeyboard. Here you see an example:

$keyboard = (new InlineKeyboard())
    ->addCallbackButton('📕 Help', 'help_callback')
    ->addUrlButton('📱 Share', 'https://t.me/share/url?url=https://t.me/your_awesome_bot&text=Some text')
    ->chunk(1)
    ->rightToLeft()
    ->get();

Next use it like this:

$tg->sendMessage([
    // Other parameters
    'reply_markup' => $keyboard,
]);

Consider that chunk() method accepts multiple numbers as well! You may pass an array like [1, 3, 2] to build such a keyboard:

[        1        ]  
[ 2 ]  [ 3️ ]  [ 4 ]  
[   5   ] [   6   ]  

Default parameters

Sometimes you do not want to repeat some parameters everywhere, so you can define default parameters for each method. Here are three example that makes it clear how you can use it:

$tg->setDefaults('sendMessage', ['parse_mode' => 'html']); // You will not need passing parse_mode anymore
$tg->setDefaults(['sendMessage', 'banChatMember'], ['chat_id' => $chatId]);
$tg->setDefaults('*', ['chat_id' => $chatId]); // a default parameter for all methods

Extend it!

You may want to add some methods to TeleBot class to improve your code readability and avoid duplication. Look at this simple example as an inspiration:

TeleBot::extend('isReply', function () {
    return property_exists($this->message, 'reply_to_message');
});

if ($tg->isReply()) { ... }

Have you seen a problem?

Create an issue and explain your problem!

Made with TeleBot

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].