dannsbass/bots
Composer 安装命令:
composer require dannsbass/bots
包简介
Simple Telegram Bot API library for PHP
README 文档
README
Simple Telegram Bot API library for PHP
Quick Start
Initiate with setToken and end with run method.
Sample 1: Start
require 'Bot.php'; Bot::setToken(BOT_TOKEN, BOT_USERNAME); //init (required) Bot::start('Welcome, I am a bot.'); //event Bot::run(); //launch (required)
Sample 2: Keyboard
Bot::chat('/help', function(){ $keyboard = Bot::keyboard(' [/info] [/admin] [/help] '); $options = ['reply_markup' => $keyboard]; return Bot::sendMessage("List of Commands:", $options); });
Sample 3: Inline Keyboard
Bot::chat('/inline_keyboard', function(){ $inline_keyboard = Bot::inline_keyboard(' [Google|https://www.google.com] [Facebook|https://www.facebook.com] [More|more_data] '); $options = ['reply_markup' => $inline_keyboard]; return Bot::sendMessage("Options:", $options); });
Sample 4: Sending Document
Bot::chat('/send', function($file){ if (file_exists($file)) return Bot::sendDocument($file); return Bot::sendMessage("$file not exists"); });
Sample 5: Chat Array
$help = function(){ return Bot::sendMessage("This is <b>bold</b> and <i>italic</i> text.", ['parse_mode' => 'html']); }; $info = function() { Bot::sendChatAction('typing'); Bot::sendDocument('composer.json', ['caption' => 'for composer']); }; Bot::chat_array([ '/help' => $help, '/info' => $info, ]);
Sample 6: Regex
Bot::regex('/^\/start (\d+)$/', function($m){ return Bot::sendMessage($m[1]); }); Bot::start(function(){ return Bot::sendMessage('Press button bellow:', [ 'reply_markup' => Bot::inline_keyboard(' [BUTTON|https://t.me/Testing58384bot?start=1234567890] ') ] ); });
Documentation
Note that all properties and methods are static. For example: Bot::$property or Bot::method().
Bot Properties
| Property | Type | Description |
|---|---|---|
| token | string | bot token from @BotFather |
| name | string | bot name from @BotFather |
| url | string | telegram URL for endpoint |
| getUpdates | array | parsed-JSON from Telegram server |
| inputObject | object | decoded-JSON from Telegram server |
| _command | array | list of commands and responses |
| _onMessage | array | list of events (types) and the responses |
| version | integer | app version |
| message_id | string | message ID |
| message_text | string | message text |
| user | string | first name (and last name) |
| from_id | integer | from ID |
| chat_id | integer | from ID |
| admin_id | integer | admin ID |
Bot Methods
| Method | Parameter(s) | Description |
|---|---|---|
| setToken | string bot token, string bot name |
|
| setAdmin | integer admin ID |
|
| isAdmin | to check if user is admin | |
| name | to get bot name | |
| chat | string command, string or callable response |
to set command and its response |
| cmd | string command, string or callable response |
to set command and its response |
| chat_array | associative array list of command (key) and its response (value) |
to set command and its response |
| keyboard | string keyboard pattern, string input_field_placeholder = 'type here', boolean resize_keyboard = true, boolean one_time_keyboard = true |
to create keyboard(s) from string |
| inline_keyboard | string keyboard pattern |
to create inline keyboard(s) from string |
| message_id | to get message id | |
| message_text | to get message text | |
| user | to get user first name (and last name) | |
| from_id | to get user ID | |
| chat_id | to get chat ID | |
| on | string type, string or callable response |
to set response |
| regex | string pattern, string or callable response |
to set response |
| run | to run the bot | |
| send | string method, associative array data |
to send request to Telegram server |
| answerInlineQuery | result, array options = [] |
to answer Inline Query |
| answerCallbackQuery | text, array options = [] |
to answer Callback Query |
| message | to get body of message JSON from user | |
| type | to get type of message | |
__callStatic |
string method, array parameters |
to call any method |
| prosesPesan | string teks, array data = null |
to send a long message |
| bg_exec | string function name, array parameters, string PHP script to be loaded first, integer timeout = 1000 |
to call function in background |
Telegram Events
Note that event parameters up to 2 parameters. For example: Bot::start($param1, $param2).
all(when user sends anything, it's similar withon('*', $response)method)start(when user sends/starttext message or pressesSTARTbutton)text(see: docs)animationaudiodocumentphotostickervideovideo_notevoicecontactdicegamepollvenuelocationmessage_auto_delete_timer_changedpinned_messageinvoicesuccessful_paymentuser_sharedchat_sharedwrite_access_allowedpassport_dataproximity_alert_triggeredforum_topic_createdforum_topic_editedforum_topic_closedforum_topic_reopenedgeneral_forum_topic_hiddengeneral_forum_topic_unhiddenvoice_chat_scheduledvoice_chat_startedvoice_chat_endedvoice_chat_participants_invitedinline_querycallback_queryedited_messagechannel_postedited_channel_post
Telegram Methods
All Telegram methods are compatible with this bot. For example:
echo Bot::getMe(); echo Bot::setWebhook(WEBHOOK_URL); echo Bot::getWebhookInfo(); echo Bot::deleteWebhook(true); //default is `false` for `drop_pending_updates`, see: https://core.telegram.org/bots/api#deletewebhook
See more: https://core.telegram.org/bots/api#available-methods
dannsbass/bots 适用场景与选型建议
dannsbass/bots 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「telegram」 「telegram bot」 「telegram bot api」 「Telegram bot library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dannsbass/bots 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dannsbass/bots 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dannsbass/bots 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dialogflow php sdk
Laravel 5.6 Monolog custom telegram channel
A PSR-7 compatible library for making CRUD API endpoints
PHP Wrapper for Telegram Bot API
VK driver for botman
Telegram bot integration with laravel
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-08






