quiec / boting
简单但强大且异步(多线程)的Telegram机器人库
Requires
- guzzlehttp/guzzle: ^6.5
- spatie/regex: ^1.4
README
简单而强大。
Boting,最快的PHP异步机器人库。
功能
- %100 异步 (😳)
- 始终兼容最新的BotAPI
- 单文件,体积小,上传简单。
- 文件下载/上传
- 事件
- WebHook & GetUpdates支持
要求
如果您能安装 Guzzle,您就可以轻松使用它。
安装
如果您有 Composer,您可以非常容易地安装它
composer require quiec/boting
如果您想使用beta版本
composer require quiec/boting:dev-master
如果Composer未安装,您可以在 这里 轻松安装。
获取更新
您可以通过两种方式获取更新;
Webhook
如果您将通过Webhook方法接收更新,只需将处理器中的“true”添加即可。
... $Bot->Handler("Token", true);
获取更新
这是默认方法。您不需要添加任何额外的内容。
... $Bot->Handler("Token");
事件
通过Boting 2.0新增的功能,您现在可以使用on
添加便捷命令并捕获消息类型。
$bot->command
命令必须是 正则表达式。
示例 (捕获 /start 命令)
$Bot->command("/\/start/m", function ($Update, $Match) use ($Bot) { $ChatId = $Update["message"]["chat"]["id"]; $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]); });
让我们添加另一个命令处理器
$Bot->command("/[!.\/]start/m", function ($Update, $Match) use ($Bot) { $ChatId = $Update["message"]["chat"]["id"]; $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Started bot."]); });
现在,机器人还将对 /start,!Start,.start
命令做出响应。
$bot->on
如果收到指定类型的消息,机器人将执行该函数。
不使用匹配,使用On。
示例 (如果收到照片)
$Bot->on("photo", function ($Update) use ($Bot) { $ChatId = $Update["message"]["chat"]["id"]; $Bot->sendMessage(["chat_id" => $ChatId, "text" => "Photo came"]); });
您可以在 这里 查看On类型。
$bot->answer
您可以使用answer函数来回答 inline_query
或 callback_query
。
示例 (让我们回答内联)
$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])]); });
特殊事件
如果您不想使用现成的函数,您可以定义自己的函数。
❗️如果您将使用Webhook,则输入 true
;如果您将使用GetUpdates获取它,则输入 false
。
$Main = function ($Update) {...}; $Bot->Handler("Token", false, $Main);
示例 (响应 /start 消息的函数)
<?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.
命令
命令与 BotAPI 命令相同。您可以使用BotAPI命令以相同的方式进行。
让我们以您想要发送消息的示例为例,我们查看 BotAPI 中所需的参数。
我们需要 chat_id
和 text
。因此,让我们编写我们的代码。
$Bot->sendMessage(["chat_id" => "@fusuf", "text" => "Hello!"]);
过程完成。命令在操作后返回数组。
示例
我们可以将 此文件 作为使用库的极好示例。还有一个响应简单 /start
消息的代码。
<?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.
许可协议
此项目完全开源,受MIT许可协议保护。请参阅LICENSE.md文件
联系方式
您可以在 Telegram 上联系我或打开问题。