alefsheen / balebotphp
一个用于创建Bale API机器人的简单PHP类
Requires
- php: >=5.0
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2024-09-15 15:07:40 UTC
README
一个用于创建 Bale API 机器人 的简单PHP类。符合 Bale Bot API 规范。
要求
- PHP >= 5.3
- Curl扩展必须启用(针对PHP5)。
- 需要Bale API密钥,您可以通过创建机器人后使用简单的命令从 @BotFather 获取。
关于WebHook
- 有效的SSL证书(Bale API需要此证书)。您可以使用提供免费SSL服务的CDN,如arvancloud或cloudflare。
关于getUpdates(长轮询)
- 一些执行脚本以提供消息的方法(例如cronjob)
下载
使用Composer
从项目目录中,运行
composer require alefsheen/balebotphp
或
php composer.phar require alefsheen/balebotphp
注意:如果您没有Composer,您可以从 这里 下载。
使用发布存档
https://github.com/alefsheen/BaleBotPHP/releases
使用Git
从项目目录中,运行
git clone https://github.com/alefsheen/BaleBotPHP.git
安装
通过Composer的自动加载器
使用Composer下载后,您可以包含Composer的自动加载器
include (__DIR__ . '/vendor/autoload.php'); $BaleBot = new BaleBot('YOUR BOT TOKEN HERE');
通过BaleBot类
将Telegram.php复制到您的服务器,并在新的机器人脚本中包含它
include 'BaleBot.php'; $balebot = new BaleBot('YOUR BOT TOKEN HERE');
注意:要启用错误日志文件,还需将BaleBotErrorLogger.php复制到BaleBot.php文件相同的目录中。
配置(WebHook)
导航到 https://tapi.bale.ai/bot(BOT_TOKEN)/setWebhook?url=https://yoursite.com/your_update.php 或使用BaleBot类的setWebhook方法。
示例
$balebot = new BaleBot('YOUR BOT TOKEN HERE'); $chat_id = $balebot->ChatID(); $content = array('chat_id' => $chat_id, 'text' => 'Test'); $balebot->sendMessage($content);
如果您想从Telegram响应中获取某些特定参数
$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE'); $result = $telegram->getData(); $text = $result['message'] ['text']; $chat_id = $result['message'] ['chat']['id']; $content = array('chat_id' => $chat_id, 'text' => 'Test'); $telegram->sendMessage($content);
要上传照片或其他文件,您需要使用CurlFile加载它
// Load a local file to upload. If is already on Telegram's Servers just pass the resource id $img = curl_file_create('test.png','image/png'); $content = array('chat_id' => $chat_id, 'photo' => $img ); $telegram->sendPhoto($content);
要下载Telegram服务器上的文件
$file = $telegram->getFile($file_id); $telegram->downloadFile($file['result']['file_path'], './my_downloaded_file_on_local_server.png');
请参阅update.php或update cowsay.php以获取完整示例。如果您想查看CowSay机器人的实际效果 添加它。
如果您想使用getUpdates而不是WebHook,您需要在for循环中调用serveUpdate
函数。
$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE'); $req = $telegram->getUpdates(); for ($i = 0; $i < $telegram-> UpdateCount(); $i++) { // You NEED to call serveUpdate before accessing the values of message in Telegram Class $telegram->serveUpdate($i); $text = $telegram->Text(); $chat_id = $telegram->ChatID(); if ($text == '/start') { $reply = 'Working'; $content = array('chat_id' => $chat_id, 'text' => $reply); $telegram->sendMessage($content); } // DO OTHER STUFF }
请参阅getUpdates.php以获取完整示例。
函数
要查看完整和最新的函数文档,请检查 http://eleirbag89.github.io/TelegramBotPHP/
构建键盘
Telegram机器人可以有两种不同的键盘:内联和回复。
InlineKeyboard与特定消息相关联,而ReplyKeyboard与整个聊天相关联。
它们都是按钮的二维数组,代表行和列。
例如,您可以这样安排一个ReplyKeyboard: 使用此代码
$option = array( //First row array($telegram->buildKeyboardButton("Button 1"), $telegram->buildKeyboardButton("Button 2")), //Second row array($telegram->buildKeyboardButton("Button 3"), $telegram->buildKeyboardButton("Button 4"), $telegram->buildKeyboardButton("Button 5")), //Third row array($telegram->buildKeyboardButton("Button 6")) ); $keyb = $telegram->buildKeyBoard($option, $onetime=false); $content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "This is a Keyboard Test"); $telegram->sendMessage($content);
当用户点击按钮时,按钮文本会被发送回机器人。
对于内联键盘来说,基本上是一样的(但你需要提供有效的URL或回调数据)
$option = array( //First row array($telegram->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"), $telegram->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")), //Second row array($telegram->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"), $telegram->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"), $telegram->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")), //Third row array($telegram->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")) ); $keyb = $telegram->buildInlineKeyBoard($option); $content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => "This is a Keyboard Test"); $telegram->sendMessage($content);
这是所有辅助函数的列表,以便轻松创建键盘
buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true)
发送一个自定义键盘。$option是一个KeyboardButton数组的数组。
查看回复键盘标记获取更多信息。
buildInlineKeyBoard(array $inline_keyboard)
发送一个自定义键盘。$inline_keyboard是一个InlineKeyboardButton数组的数组。
查看内联键盘标记获取更多信息。
buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query)
创建一个InlineKeyboardButton。
查看内联键盘按钮获取更多信息。
buildKeyBoardButton($text, $url, $request_contact, $request_location)
创建一个KeyboardButton。
查看键盘按钮获取更多信息。
buildKeyBoardHide($selective=true)
隐藏自定义键盘。
查看回复键盘隐藏获取更多信息。
buildForceReply($selective=true)
向用户显示回复界面。
查看强制回复获取更多信息。
表情符号
关于在您的机器人消息中使用表情符号的列表,请参阅此表的“字节”列:http://apps.timwhitlock.info/emoji/tables/unicode
许可证
此开源软件是根据MIT许可证分发的。请参阅LICENSE.md
贡献
所有类型的贡献都受欢迎——代码、测试、文档、错误报告、新功能等...
- 发送反馈。
- 提交错误报告。
- 编写/编辑文档。
- 修复错误或添加新功能。
联系我
您可以通过Telegram与我联系,但如果您有问题,请打开一个问题。
支持我
或者使用Paypal为我买一杯啤酒或两杯。