PHPackages                             naftali1000/async\_bot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. naftali1000/async\_bot

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

naftali1000/async\_bot
======================

async telegram bots library

v3.2.20(4mo ago)362MITPHP

Since Jun 20Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/naftali100/async_tgbot)[ Packagist](https://packagist.org/packages/naftali1000/async_bot)[ Docs](https://github.com/naftali100/async_tgbot)[ RSS](/packages/naftali1000-async-bot/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (5)Dependencies (9)Versions (81)Used By (0)

bot\_lib
========

[](#bot_lib)

simple flexible and async library based on amphp for telegram bot api.

> NOTE: this is an old project and very poorly written. I wouldn't recommend to use it.

installation
------------

[](#installation)

- run `composer require naftali100/async_bot`.

getting started
---------------

[](#getting-started)

#### server.php

[](#serverphp)

```
require_once './vendor/autoload.php';

use bot_lib\Server;

$server = new Server("127.0.0.1:8080"); // create server instance listening to port 8080
$server->load_file("bot.php"); // load the handlers in "bot.php" and store them in "bot.php" path
$server->load_file("bot1.php", "index"); // you can add second param for different path
$server->load_folder("folder", true); // load all files in a folder. the second param is whether to load recursively or not
$server->run();
```

#### bot.php

[](#botphp)

```
require_once './vendor/autoload.php';

use bot_lib\Config;
use bot_lib\Handler;
use bot_lib\Update;
use bot_lib\Filter;

$config = new Config;
$config->load("conf.json"); // can store token
$config->server_url = "http://loadlhost:8081/bot"; // if you using local telegram-bot-api

$handler = new Handler;
$handler->on_message(
    fn(Update $u) => $u->reply("hello"),
    Filter::Message('/start')
);
```

set webhook to 127.0.0.1:8080/you\_bot\_file\_name.php (or custom name passed in second argument to load\_file).

you can add `token` parameter to the webhook url and the server will set it and use this token.

run `php server.php`.

a lot more handler, config and server options in examples folder.

---

explanation
===========

[](#explanation)

there is 5 main objects in the library

1. Server: extends Loader. load files, running the http-server and activating handlers.
2. Config: configuration.
3. Update: extends API and HTTP. contains all the method to send request to bot api.
4. Handler: create handlers that will run asynchronously.
5. Filter: static methods to create filters.

### Server

[](#server)

the server is loading all of your robots files, take the handlers, and run a server listen to incoming requests. once there is a request to the server, it activates the handlers set in the request path. you can set any request path to any file.

### Handler

[](#handler)

all handlers run asynchronously on every request from the bot. there is a verity of handlers you can set and ways to control how they will activate.

to create handler, simply call the method on Handler instance as handler name you want `$handler->handler_name()`.

you can give it any name you want (except the handler class methods). the name can control when the handler is activating.

handler accepts 4 parameter

- function (named func): the function to run when handler activated. accept Update instance as parameter.
- filter: must be callable. if you passed a function, it should receive one argument - Update instance
- last: if true and handler is activated, the handler will be the last handler to run in current request.
- name: name of the handler, useful for debugging what handler is activated.

you can pass the arguments by order (function, filter, last, name) or by name

```
$handler->on_message(
    filter: "blabal",
    func: fn($u) => $u->reply("bla"),
    last: true,
    name: 'reply bla to blabla'
);
```

#### special handlers names

[](#special-handlers-names)

##### this list of special handler activated in specific TBD.

[](#this-list-of-special-handler-activated-in-specific-tbd)

- before: activated before any other handler. can return new array of function to run instead of existing handlers, useful for disabling all of them by returning empty array.
- middle: run before every handler. accept 2 arguments: Update and $next witch us the function of the handler.
- after: activates after all handlers finished. useful for cleaning, or writing to db.
- fallback: activates only if no other handler was activated.

##### this list of special handler activated in specific update types.

[](#this-list-of-special-handler-activated-in-specific-update-types)

- on\_update: activates on every update. accepts update type/s as filter (message, callback\_query, etc).
- on\_message: activates on 'message' updates. accept message/s as filter (/start, menu, word, test, etc).
- on\_edit: activates on 'edited\_message' updates. accept new message/s as filter.
- on\_cbq: activates on 'callback\_query' updates. accept text to match callback\_data as filter.
- on\_file: activates when there is file in the request (no matter what update type). accept file type/s as filter (photo, audio, etc).
- on\_service: activated when update is service message, do not accept string or array filter, only function (string or array will result the handler not activating).

### Config

[](#config)

you can config various things see src/config.php file. can be set in json file and load using `load` method as shown above.

### Update

[](#update)

All bot api method and some more in this class. instance of this class is passed to the handlers.

telegram api methods -

#### Added methods:

[](#added-methods)

- reply: reply to the message.
- delete: delete the message.
- ban: \[only in groups and channels\] ban the user from chat.
- leave: leave group or channel.
- edit: edit the message.
- forward: forward the message to another chat with credit or as copy.
- alert: reply to callback\_query.
- download: download media in message.
- editKeyboard: edit inline keyboard.
- editButton: edit only one inline button.

Also there is a lot of preset variables to many update parts. see update.php file.

#### variables

[](#variables)

Partial list:

- chat: the chat where the message sent.
- from: the user that send the message. same as chat in private message.
- updateType: the update type (message, callback\_query, etc).
- cb\_answered: whether callback\_query answered or not. can be used in after handler to answer if not answered.
- service: if message is service message.
- media: contain media from message.

you can access the update as object or as array. `$u->message->chat->id` or `$u['message']['chat']['id']`.

you can skip the update type (message in above example).

`$u->data` will be the callback data in callback update. `$u->message_id` is the message\_id.

### Loader

[](#loader)

The Server class extends the Loader. you shouldn't use it directly.

You can load file, folders or Handler.

##### extra access

[](#extra-access)

File loaded with the ability to change handlers of other bots.

The handlers in file loaded with $this of bot\_lib/Server.

The `files` prop in Server class contain the

- path
    - file\_name
    - handler
    - config

of every file loaded by the server.

Examples what you can do with extra access in examples folder.

### Helper

[](#helper)

contain static helpers functions.

- keyboard: easily create inline keyboard. see comment how to use.
- permissions: create [ChatPermissions](https://core.telegram.org/bots/api#chatpermissions) json.

debugging
---------

[](#debugging)

to enable debug logs for the server

```
use Psr\Log\LogLevel;

$server->setLogLevel(LogLevel::DEBUG);
```

to enable logging per bot you can add this to the bot file

```
use Psr\Log\LogLevel;

$config->setLevel(LogLevel::DEBUG);
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance74

Regular maintenance activity

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Recently: every ~28 days

Total

80

Last Release

145d ago

Major Versions

v1.5.3 → v2.02022-09-27

v2.1.8 → v3.0.02024-11-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/c1409bf779dbe85e9b10144ff300001f99e5368ac9930518b6fea7045e19cd73?d=identicon)[naftali100](/maintainers/naftali100)

---

Top Contributors

[![naftali100](https://avatars.githubusercontent.com/u/48069590?v=4)](https://github.com/naftali100 "naftali100 (183 commits)")

---

Tags

amphptelegram-botasync-telegram-bots

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/naftali1000-async-bot/health.svg)

```
[![Health](https://phpackages.com/badges/naftali1000-async-bot/health.svg)](https://phpackages.com/packages/naftali1000-async-bot)
```

###  Alternatives

[danog/madelineproto

Async PHP client API for the telegram MTProto protocol.

3.4k855.0k18](/packages/danog-madelineproto)[amphp/process

A fiber-aware process manager based on Amp and Revolt.

25552.6M52](/packages/amphp-process)[amphp/windows-registry

Windows Registry Reader.

10518.5M2](/packages/amphp-windows-registry)[dantleech/fink

Checks Links

2266.3k2](/packages/dantleech-fink)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
