{"id":25063038,"url":"https://github.com/naftali100/async_tgbot","last_synced_at":"2025-09-10T21:15:59.852Z","repository":{"id":47436520,"uuid":"378715222","full_name":"naftali100/async_tgbot","owner":"naftali100","description":"async telelgram bots library based on amphp ","archived":false,"fork":false,"pushed_at":"2025-01-01T12:37:50.000Z","size":1173,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T05:11:10.723Z","etag":null,"topics":["amphp","telegram-bot"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/naftali100.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-20T18:33:39.000Z","updated_at":"2025-03-02T14:31:00.000Z","dependencies_parsed_at":"2024-12-12T21:21:56.969Z","dependency_job_id":"b7732e61-f64e-4f12-9eff-63775e442ad1","html_url":"https://github.com/naftali100/async_tgbot","commit_stats":{"total_commits":180,"total_committers":4,"mean_commits":45.0,"dds":0.0444444444444444,"last_synced_commit":"b96f4d71a26a4e33a5198c454a5b5668cbd6a72f"},"previous_names":[],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naftali100%2Fasync_tgbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naftali100%2Fasync_tgbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naftali100%2Fasync_tgbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naftali100%2Fasync_tgbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/naftali100","download_url":"https://codeload.github.com/naftali100/async_tgbot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248915072,"owners_count":21182557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amphp","telegram-bot"],"created_at":"2025-02-06T17:35:27.186Z","updated_at":"2025-04-14T16:24:37.918Z","avatar_url":"https://github.com/naftali100.png","language":"PHP","funding_links":[],"categories":["Bots"],"sub_categories":[],"readme":"# bot_lib\n\nsimple flexible and async library based on amphp for telegram bot api.\n\n\u003e NOTE: this is an old project and very poorly written. I wouldn't recommend to use it.\n\n## installation\n\n- run ```composer require naftali100/async_bot```.\n\n## getting started\n\n#### server.php\n\n```php\nrequire_once './vendor/autoload.php';\n\nuse bot_lib\\Server; \n\n$server = new Server(\"127.0.0.1:8080\"); // create server instance listening to port 8080\n$server-\u003eload_file(\"bot.php\"); // load the handlers in \"bot.php\" and store them in \"bot.php\" path\n$server-\u003eload_file(\"bot1.php\", \"index\"); // you can add second param for different path\n$server-\u003eload_folder(\"folder\", true); // load all files in a folder. the second param is whether to load recursively or not\n$server-\u003erun();\n```\n#### bot.php\n\n```php\nrequire_once './vendor/autoload.php';\n\nuse bot_lib\\Config;\nuse bot_lib\\Handler;\nuse bot_lib\\Update;\nuse bot_lib\\Filter;\n\n$config = new Config;\n$config-\u003eload(\"conf.json\"); // can store token\n$config-\u003eserver_url = \"http://loadlhost:8081/bot\"; // if you using local telegram-bot-api\n\n$handler = new Handler;\n$handler-\u003eon_message(\n    fn(Update $u) =\u003e $u-\u003ereply(\"hello\"),\n    Filter::Message('/start')\n);\n```\nset webhook to 127.0.0.1:8080/you_bot_file_name.php (or custom name passed in second argument to load_file).\n\nyou can add `token` parameter to the webhook url and the server will set it and use this token.\n\nrun `php server.php`.\n\na lot more handler, config and server options in examples folder.\n\n---\n# explanation\n\nthere is 5 main objects in the library\n\n1. Server: extends Loader. load files, running the http-server and activating handlers.\n2. Config: configuration.\n3. Update: extends API and HTTP. contains all the method to send request to bot api.\n4. Handler: create handlers that will run asynchronously.\n5. Filter: static methods to create filters.\n\n### Server \nthe server is loading all of your robots files, take the handlers, and run a server listen to incoming requests.\nonce 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.\n\n### Handler\nall handlers run asynchronously on every request from the bot. \nthere is a verity of handlers you can set and ways to control how they will activate.\n\nto create handler, simply call the method on Handler instance as handler name you want `$handler-\u003ehandler_name()`.\n\nyou can give it any name you want (except the handler class methods).\nthe name can control when the handler is activating.\n\nhandler accepts 4 parameter\n\n- function (named func): the function to run when handler activated. accept Update instance as parameter.\n- filter: must be callable. if you passed a function, it should receive one argument - Update instance\n- last: if true and handler is activated, the handler will be the last handler to run in current request.\n- name: name of the handler, useful for debugging what handler is activated.\n\nyou can pass the arguments by order (function, filter, last, name) or by name\n```php\n$handler-\u003eon_message(\n    filter: \"blabal\", \n    func: fn($u) =\u003e $u-\u003ereply(\"bla\"),\n    last: true,\n    name: 'reply bla to blabla'\n);\n```\n\n#### special handlers names\n##### this list of special handler activated in specific TBD.\n- 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.\n- middle: run before every handler. accept 2 arguments: Update and $next witch us the function of the handler.\n- after: activates after all handlers finished. useful for cleaning, or writing to db.\n- fallback: activates only if no other handler was activated.\n\n##### this list of special handler activated in specific update types.\n\n- on_update: activates on every update. accepts update type/s as filter (message, callback_query, etc).\n- on_message: activates on 'message' updates. accept message/s as filter (/start, menu, word, test, etc).\n- on_edit: activates on 'edited_message' updates. accept new message/s as filter.\n- on_cbq: activates on 'callback_query' updates. accept text to match callback_data as filter.\n- on_file: activates when there is file in the request (no matter what update type). accept file type/s as filter (photo, audio, etc).\n- 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).\n\n### Config\nyou can config various things see src/config.php file. can be set in json file and load using `load` method as shown above.\n\n### Update\nAll bot api method and some more in this class. instance of this class is passed to the handlers.\n\ntelegram api methods - https://core.telegram.org/bots/api#available-methods\n\n#### Added methods:\n\n- reply: reply to the message.\n- delete: delete the message.\n- ban: [only in groups and channels] ban the user from chat.\n- leave: leave group or channel.\n- edit: edit the message.\n- forward: forward the message to another chat with credit or as copy.\n- alert: reply to callback_query.\n- download: download media in message.\n- editKeyboard: edit inline keyboard.\n- editButton: edit only one inline button.\n\nAlso there is a lot of preset variables to many update parts. see update.php file.\n\n#### variables \nPartial list:\n\n- chat: the chat where the message sent.\n- from: the user that send the message. same as chat in private message.\n- updateType: the update type (message, callback_query, etc).\n- cb_answered: whether callback_query answered or not. can be used in after handler to answer if not answered.\n- service: if message is service message.\n- media: contain media from message.\n\nyou can access the update as object or as array. `$u-\u003emessage-\u003echat-\u003eid` or `$u['message']['chat']['id']`.\n\nyou can skip the update type (message in above example). \n\n`$u-\u003edata` will be the callback data in callback update. `$u-\u003emessage_id` is the message_id.\n\n### Loader\nThe Server class extends the Loader. you shouldn't use it directly.\n\nYou can load file, folders or Handler.\n\n##### extra access\nFile loaded with the ability to change handlers of other bots.\n\nThe handlers in file loaded with $this of bot_lib/Server.\n\nThe `files` prop in Server class contain the\n\n- path \n    - file_name \n    - handler \n    - config \n \nof every file loaded by the server.\n\nExamples what you can do with extra access in examples folder.\n\n### Helper\ncontain static helpers functions.\n\n- keyboard: easily create inline keyboard. see comment how to use.\n- permissions: create [ChatPermissions](https://core.telegram.org/bots/api#chatpermissions) json.\n\n## debugging\n\nto enable debug logs for the server\n```php\nuse Psr\\Log\\LogLevel;\n\n$server-\u003esetLogLevel(LogLevel::DEBUG);\n```\n\nto enable logging per bot you can add this to the bot file\n```php\nuse Psr\\Log\\LogLevel;\n\n$config-\u003esetLevel(LogLevel::DEBUG);\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaftali100%2Fasync_tgbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnaftali100%2Fasync_tgbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaftali100%2Fasync_tgbot/lists"}