php-telegram-bot/telegram-bot-manager
Composer 安装命令:
composer require php-telegram-bot/telegram-bot-manager
包简介
PHP Telegram Bot Manager
README 文档
README
This project builds on top of PHP Telegram Bot and as such, depends on it!
The main purpose of this mini-library is to make the interaction between your webserver and Telegram easier. I strongly suggest your read the PHP Telegram Bot instructions first, to understand what this library does exactly.
Installation and usage is pretty straight forward:
Require this package with Composer
composer require php-telegram-bot/telegram-bot-manager:^2.1
NOTE: This will automatically also install PHP Telegram Bot into your project (if it isn't already).
Advanced: Due to the fact that the core library is not a stable version yet, this project is partly locked to the core version, to ensure reliable functioning.
It is possible however, to override the core version that this library requires:
"require": { "php-telegram-bot/telegram-bot-manager": "^2.1", "longman/telegram-bot": "dev-master as 0.81" }
This example will pull the master version of the core library, making it appear to be version 0.81, which then satisfies the requirement.
Performing actions
What use would this library be if you couldn't perform any actions?!
There are a few parameters available to get things rolling:
| Parameter | Description |
|---|---|
| s | secret: This is a special secret value defined in the main manager.php file. |
| This parameter is required to call the script via browser! | |
| a | action: The actual action to perform. (handle (default), webhookinfo, cron, set, unset, reset) |
handle executes the getUpdates method; webhookinfo to get result from getWebhookInfo, cron executes cron commands; set / unset / reset the webhook. |
|
| l | loop: Number of seconds to loop the script for (used for getUpdates method). |
| This would be used mainly via CLI, to continually get updates for a certain period. | |
| i | interval: Number of seconds to wait between getUpdates requests (used for getUpdates method, default is 2). |
| This would be used mainly via CLI, to continually get updates for a certain period, every i seconds. | |
| g | group: Commands group for cron (only used together with cron action, default group is default). |
| Define which group of commands to execute via cron. Can be a comma separated list of groups. |
via browser
Simply point your browser to the manager.php file with the necessary GET parameters:
http://example.com/manager.php?s=<secret>&a=<action>&l=<loop>&i=<interval>
Webhook
Set, unset and reset the webhook:
http://example.com/manager.php?s=super_secret&a=sethttp://example.com/manager.php?s=super_secret&a=unsethttp://example.com/manager.php?s=super_secret&a=reset(unset & set combined)
getUpdates
Handle updates once:
http://example.com/manager.php?s=super_secret&a=handleor simplyhttp://example.com/manager.php?s=super_secret(handleaction is the default)
Handle updates for 30 seconds, fetching every 5 seconds:
http://example.com/manager.php?s=super_secret&l=30&i=5
cron
Execute commands via cron:
http://example.com/manager.php?s=super_secret&a=cron&g=maintenanceor multiple groupshttp://example.com/manager.php?s=super_secret&a=cron&g=maintenance,cleanup
via CLI
When using CLI, the secret is not necessary (since it could just be read from the file itself).
Call the manager.php file directly using php and pass the parameters:
$ php manager.php a=<action> l=<loop> i=<interval>
Webhook
Set, unset and reset the webhook:
$ php manager.php a=set$ php manager.php a=unset$ php manager.php a=reset(unset & set combined)
getUpdates
Handle updates once:
$ php manager.php a=handleor simply$ php manager.php(handleaction is the default)
Handle updates for 30 seconds, fetching every 5 seconds:
$ php manager.php l=30 i=5
cron
Execute commands via cron:
$ php manager.php a=cron g=maintenanceor multiple groups$ php manager.php a=cron g=maintenance,cleanup
Create the manager PHP file
You can name this file whatever you like, it just has to be somewhere inside your PHP project (preferably in the root folder to make things easier).
(Let's assume our file is called manager.php)
Let's start off with a simple example that uses the webhook method:
<?php use TelegramBot\TelegramBotManager\BotManager; // Load composer. require_once __DIR__ . '/vendor/autoload.php'; try { $bot = new BotManager([ // Vitals! 'api_key' => '12345:my_api_key', // Extras. 'bot_username' => 'my_own_bot', 'secret' => 'super_secret', 'webhook' => [ 'url' => 'https://example.com/manager.php', ] ]); $bot->run(); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; }
Set vital bot parameters
The only vital parameter is the API key:
$bot = new BotManager([ // (string) Bot API key provided by @BotFather. 'api_key' => '12345:my_api_key', ... ]);
Set extra bot parameters
Apart from the necessary API key, the bot can be easily configured using extra parameters.
Set the webhook? Enable admins? Add custom command paths?
All no problem!
The secret is a user-defined key that is required to execute any of the library's features via webhook.
Best make it long, random and very unique!
For 84 random characters:
- If you have
pwgeninstalled, just executepwgen 84 1and copy the output. - If you have
opensslinstalled, useopenssl rand -hex 84. - Or just go here and put all the output onto a single line.
(You get 2 guesses why 84 is a good number 😉)
Below is a complete list of all available extra parameters.
$bot = new BotManager([ ... // (string) Bot username that was defined when creating the bot. 'bot_username' => 'my_own_bot', // (string) A secret password required to authorise access to the webhook. 'secret' => 'super_secret', // (array) All options that have to do with the webhook. 'webhook' => [ // (string) URL to the manager PHP file used for setting up the webhook. 'url' => 'https://example.com/manager.php', // (string) Path to a self-signed certificate (if necessary). 'certificate' => __DIR__ . '/server.crt', // (int) Maximum allowed simultaneous HTTPS connections to the webhook. 'max_connections' => 20, // (array) List the types of updates you want your bot to receive. 'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'], // (string) Secret token to validate webhook requests. 'secret_token' => 'super_secret_token', ], // (bool) Only allow webhook access from valid Telegram API IPs. 'validate_request' => true, // (array) When using `validate_request`, also allow these IPs. 'valid_ips' => [ '1.2.3.4', // single '192.168.1.0/24', // CIDR '10/8', // CIDR (short) '5.6.*', // wildcard '1.1.1.1-2.2.2.2', // range ], // (array) All options that have to do with the limiter. 'limiter' => [ // (bool) Enable or disable the limiter functionality. 'enabled' => true, // (array) Any extra options to pass to the limiter. 'options' => [ // (float) Interval between request handles. 'interval' => 0.5, ], ], // (array) An array of user ids that have admin access to your bot (must be integers). 'admins' => [12345], // (array) Mysql credentials to connect a database (necessary for [`getUpdates`](#using-getupdates-method) method!). 'mysql' => [ 'host' => '127.0.0.1', 'port' => 3306, // optional 'user' => 'root', 'password' => 'root', 'database' => 'telegram_bot', 'table_prefix' => 'tbl_prfx_', // optional 'encoding' => 'utf8mb4', // optional ], // (array) List of configurable paths. 'paths' => [ // (string) Custom download path. 'download' => __DIR__ . '/Download', // (string) Custom upload path. 'upload' => __DIR__ . '/Upload', ], // (array) All options that have to do with commands. 'commands' => [ // (array) A list of custom commands paths. 'paths' => [ __DIR__ . '/CustomCommands', ], // (array) A list of all custom command configs. 'configs' => [ 'sendtochannel' => ['your_channel' => '@my_channel'], 'weather' => ['owm_api_key' => 'owm_api_key_12345'], ], ], // (array) All options that have to do with cron. 'cron' => [ // (array) List of groups that contain the commands to execute. 'groups' => [ // Each group has a name and array of commands. // When no group is defined, the default group gets executed. 'default' => [ '/default_cron_command', ], 'maintenance' => [ '/db_cleanup', '/db_repair', '/message_admins Maintenance completed', ], ], ], // (string) Override the custom input of your bot (mostly for testing purposes!). 'custom_input' => '{"some":"raw", "json":"update"}', ]);
Using getUpdates method
Using the getUpdates method must not have a webhook parameter set and requires a MySQL database connection:
$bot = new BotManager([ ... // Extras. 'mysql' => [ 'host' => '127.0.0.1', 'port' => 3306, // optional 'user' => 'root', 'password' => 'root', 'database' => 'telegram_bot', 'table_prefix' => 'tbl_prfx_', // optional 'encoding' => 'utf8mb4', // optional ], ]);
Now, the updates can be done either through the browser or via CLI.
Custom getUpdates output
A callback can be defined, to override the default output when updates are handled via getUpdates.
Example of the default output:
...
2017-07-10 14:59:25 - Updates processed: 1
123456: <text>
2017-07-10 14:59:27 - Updates processed: 0
2017-07-10 14:59:30 - Updates processed: 0
2017-07-10 14:59:32 - Updates processed: 0
2017-07-10 14:59:34 - Updates processed: 1
123456: <photo>
2017-07-10 14:59:36 - Updates processed: 0
...
Using custom callback that must return a string:
// In manager.php after $bot has been defined: $bot->setCustomGetUpdatesCallback(function (ServerResponse $get_updates_response) { $results = array_filter((array) $get_updates_response->getResult()); return sprintf('There are %d update(s)' . PHP_EOL, count($results)); });
output:
...
There are 0 update(s)
There are 0 update(s)
There are 2 update(s)
There are 1 update(s)
...
Development
When running live bot tests on a fork, you must enter the following environment variables to your repository settings:
API_KEY="12345:your_api_key"
BOT_USERNAME="username_of_your_bot"
It probably makes sense for you to create a new dummy bot for this.
Security
See SECURITY for more information.
Donate
All work on this bot consists of many hours of coding during our free time, to provide you with a Telegram Bot library that is easy to use and extend. If you enjoy using this library and would like to say thank you, donations are a great way to show your support.
Donations are invested back into the project 👍
Thank you for keeping this project alive 🙏
Patreon.com/phptelegrambot
OpenCollective.com/php-telegram-bot
Ko-fi.com/phptelegrambot
Tidelift.com/longman/telegram-bot
Liberapay.com/PHP-Telegram-Bot
PayPal.me/noplanman (account of @noplanman)
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
php-telegram-bot/telegram-bot-manager 适用场景与选型建议
php-telegram-bot/telegram-bot-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.28k 次下载、GitHub Stars 达 219, 最近一次更新时间为 2017 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「manager」 「bot」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-telegram-bot/telegram-bot-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-telegram-bot/telegram-bot-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-telegram-bot/telegram-bot-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dialogflow php sdk
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Laravel 5.6 Monolog custom telegram channel
PHP Wrapper for Telegram Bot API
KCFinder web file manager
Publish / Subscribe / Event Manager
统计信息
- 总下载量: 15.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 219
- 点击次数: 8
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-08