Skip to content

yusufusta/Boting

Repository files navigation

Boting

Simple yet Powerful.

🇹🇷 Türkçe | 🇬🇧 English

Boting, The best Telegram Bot library for fast and asynchronous bot with PHP.

Features

  • %100 Async (😳)
  • Always compatible with the latest BotAPI
  • Single file, small size, simple to upload.
  • File download/upload
  • Events
  • WebHook & GetUpdates support

Requirements

If you can install Guzzle, you can use it easily.

Install

If you have Composer, you can install it very easily:

composer require quiec/boting

If you want to use the beta version:

composer require quiec/boting:dev-master

If Composer is not installed, you can easily install it Here.

Get Update

You can get Update with two ways;

Webhook

If you are going to receive Updates with Webhook method, just add "true" to the handler.

...
$Bot->Handler("Token", true);

Get Updates

This method is used by default. You don't need to add anything extra.

...
$Bot->Handler("Token");

Events

With the new feature added to Boting 2.0, you can now add convenience commands and capture message types with on.

$bot->command

The command, must be regex.

Example (Let's catch /start command):

$Bot->command("/\/start/m", function ($Update, $Match) use ($Bot) {
    $ChatId = $Update["message"]["chat"]["id"]; 
    $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
});

Let's add another command handler:

$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
    $ChatId = $Update["message"]["chat"]["id"]; 
    $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
});

The bot will now also respond to /start,!Start,.start commands.

$bot->on

The bot will execute the function if a message of the specified type arrives.

No match is used, On.

Example (If the photo comes):

$Bot->on("photo", function ($Update) use ($Bot) {
    $ChatId = $Update["message"]["chat"]["id"]; 
    $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Photo came"]);
});

You can look at the On Types here.

$bot->answer

You can use the answer function to answer inline_query or callback_query.

Example (Let's answer inline):

$Bot->answer("inline_query", function ($Update) use ($Bot) {
    $Bir = ["type" => "article", "id" => 0, "title" => "test", "input_message_content" => ["message_text" => "This bot created by Boting..."]];
    $Bot->answerInlineQuery(["inline_query_id" => $Update["inline_query"]["id"], "results" => json_encode([$Bir])]);    
});

Special Events

If you do not want to use ready-made functions, you can define your own function.

❗️Type true if you are going to use Webhook or false if you will get it with GetUpdates.

$Main = function ($Update) {...};
$Bot->Handler("Token", false, $Main);

Example (A function that responds to the /start message):

<?php
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
use Boting\Boting; // We say we want to use the base.

$Bot = new Boting(); // We start the base.
$Main = function ($Update) use ($Bot) { // We create a function called Main.
    if (!empty($Update["message"])) { // We check if a message has arrived.
        $Mesaj = $Update["message"]["text"]; // We throw the message into the variable.
        $ChatId = $Update["message"]["chat"]["id"]; // We get the chat id to send messages.

        if ($Mesaj === "/start") { // We check if the message is start.
            $Bot->sendMessage(["chat_id" => $ChatId, "text" => "You started the bot."]); // We use the sendMessage function.
        }
    }
};
$Bot->Handler("Here ur bot token", false, $Main); // We define our bot token and function.

Commands

Commands are the same as BotAPI commands. You can use BotAPI commands in the same way.

Let's give an example you wanted to send a message,We look at the required parameters from BotAPI.

We need chat_id and text. So let's write our code.

$Bot->sendMessage(["chat_id" => "@fusuf", "text" => "Hello!"]);

The process is complete. Commands return Array, after operation.

Examples

We can show this file as a very good example of using the library. Also a code that responds to a simple /start message:

<?php
require __DIR__ . '/vendor/autoload.php'; //We include the base of the bot.
use Boting\Boting; // We say we want to use the base.

$Bot = new Boting(); // We start the base.
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) {
    $ChatId = $Update["message"]["chat"]["id"]; 
    $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]);
});
$Bot->Handler("Here ur bot token"); // We define our bot token.

Licence

This project is completely open source and protected under MIT license. Please refer to the LICENSE.md file

Contact

You can contact me on Telegram or open Issue.