GithubHelp home page GithubHelp logo

yusufusta / boting Goto Github PK

View Code? Open in Web Editor NEW
19.0 4.0 12.0 64 KB

Simple but powerful Telegram Bot library

License: MIT License

PHP 100.00%
telegram telegram-bot telegram-bot-api telegrambot async multithreading phpasync

boting's Introduction

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.

boting's People

Contributors

yusufusta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.