eleirbag89 / telegrambotphp
一个非常简单的PHP Telegram机器人API
Requires
- php: >=5.0
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2024-08-30 01:44:51 UTC
README
一个非常简单的PHP Telegram机器人API。
符合2022年4月16日Telegram机器人API更新。
要求
- PHP >= 5.3
- PHP5必须启用Curl扩展。
- Telegram API密钥,您可以通过创建机器人后使用简单命令的@BotFather轻松获取。
对于WebHook
- 有效的SSL证书(Telegram API要求此证书)。如果您使用CloudFlare DNS,可以使用Cloudflare的免费灵活SSL,该SSL会加密从终端用户到代理的Web流量。
自8月29日更新以来,您可以使用自签名ssl证书。
对于getUpdates(长轮询)
- 某种方式来执行脚本以提供消息(例如cronjob)
下载
使用Composer
在项目目录中运行
composer require eleirbag89/telegrambotphp
或
php composer.phar require eleirbag89/telegrambotphp
注意:如果您没有Composer,您可以从这里下载。
使用发布存档
https://github.com/Eleirbag89/TelegramBotPHP/releases
使用Git
在项目目录中运行
git clone https://github.com/Eleirbag89/TelegramBotPHP.git
安装
通过Composer的自动加载器
使用Composer下载后,您可以包含Composer的自动加载器
include (__DIR__ . '/vendor/autoload.php'); $telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');
通过TelegramBotPHP类
将Telegram.php复制到您的服务器,并在新的机器人脚本中包含它
include 'Telegram.php'; $telegram = new Telegram('YOUR TELEGRAM TOKEN HERE');
注意:为了启用错误日志文件,还需要将TelegramErrorLogger.php复制到Telegram.php文件相同的目录中。
配置(WebHook)
导航到https://api.telegram.org/bot(BOT_TOKEN)/setWebhook?url=https://yoursite.com/your_update.php或使用Telegram类的setWebhook方法。
示例
$telegram = new Telegram('YOUR TELEGRAM TOKEN HERE'); $chat_id = $telegram->ChatID(); $content = array('chat_id' => $chat_id, 'text' => 'Test'); $telegram->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的机器人可以有两种不同类型的键盘:内联和回复。
内联键盘与特定消息相关联,而回复键盘与整个聊天相关联。
它们都是按钮的数组数组,表示行和列。
例如,您可以这样安排回复键盘:[图片链接] 使用此代码
$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数组。
有关更多信息,请查看ReplyKeyBoardMarkUp。
buildInlineKeyBoard(array $inline_keyboard)
发送一个自定义键盘。$inline_keyboard是一个InlineKeyboardButton数组。
有关更多信息,请查看InlineKeyboardMarkup。
buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query)
创建一个InlineKeyboardButton。
有关更多信息,请查看InlineKeyBoardButton。
buildKeyBoardButton($text, $url, $request_contact, $request_location)
创建一个KeyboardButton。
有关更多信息,请查看KeyBoardButton。
buildKeyBoardHide($selective=true)
隐藏一个自定义键盘。
有关更多信息,请查看ReplyKeyBoarHide。
buildForceReply($selective=true)
向用户显示回复界面。
有关更多信息,请查看ForceReply。
表情符号
有关可在您的机器人消息中使用表情符号的列表,请参阅此表的Bytes列:http://apps.timwhitlock.info/emoji/tables/unicode
许可
此开源软件根据MIT许可证分发。请参阅LICENSE.md。
贡献
欢迎所有类型的贡献——代码、测试、文档、错误报告、新功能等...
- 发送反馈。
- 提交错误报告。
- 编写/编辑文档。
- 修复错误或添加新功能。
联系我
您可以通过Telegram 与我联系,但如果您有问题,请打开一个。
支持我
或使用Paypal为我买一杯啤酒。