shakibonline / telegrambotphp
一个非常简单的PHP Telegram Bot API
Requires
- php: >=5.0
This package is not auto-updated.
Last update: 2024-09-28 00:13:28 UTC
README
用于发送消息的非常简单的PHP Telegram Bot API
(几乎)符合2017年5月18日更新的Telegram Bot API。
需求
- PHP5
- Curl for PHP5必须启用。
- Telegram API密钥,您可以在创建机器人后通过@BotFather使用简单命令轻松获取。
对于WebHook
- SSL证书(Telegram API需要此证书)。如果您使用CloudFlare DNS,可以使用Cloudflare的免费灵活SSL,它将加密从最终用户到代理的Web流量。
自8月29日更新以来,您可以使用自签名SSL证书。
对于GetUpdates
- 某种方式来执行脚本以提供服务消息(例如cronjob)
安装
- 将Telegram.php复制到您的服务器,并在新机器人脚本中包含它
include("Telegram.php"); $telegram = new Telegram($bot_id);
- 要启用错误日志文件,也请将TelegramErrorLogger.php复制到Telegram.php文件所在的同一目录中
使用Composer
在项目目录中运行
composer require eleirbag89/telegrambotphp
配置(WebHook)
转到https://api.telegram.org/bot(BOT_ID)/setWebhook?url=https://yoursite.com/your_update.php或使用Telegram类的setWebhook方法。
示例
$telegram = new Telegram($bot_id); $chat_id = $telegram->ChatID(); $content = array('chat_id' => $chat_id, 'text' => "Test"); $telegram->sendMessage($content);
如果您想从Telegram响应中获取某些特定参数
$telegram = new Telegram($bot_id); $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
函数。
$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);
当用户点击按钮时,按钮文本会被发送回机器人。
对于InlineKeyboard,操作几乎相同(但您需要提供一个有效的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)
向用户显示回复界面。
查看强制回复获取更多信息。
使用此库的机器人列表
如果您使用此API创建了机器人,请使用此问题通知我,我会将其添加到本节。
- Notifyadbot - 语言:波斯语/法语
- Partners_shakibonline_bot - 语言:波斯语/法语
表情符号
有关在您的机器人消息中使用表情符号的列表,请参阅此表的“Bytes”列:http://apps.timwhitlock.info/emoji/tables/unicode
联系我
您可以通过Telegram联系我,但如果您有任何问题,请打开一个问题。
支持我
您可以使用Paypal为我买一杯或两杯啤酒
或使用 Flattr 支持我。