赵默拉 / telegram_bot_php
一个非常简单的PHP Telegram机器人API
Requires
- php: >=5.0
This package is auto-updated.
Last update: 2024-09-29 05:01:41 UTC
README
一个非常简单的PHP Telegram机器人API.
符合2017年8月23日Telegram机器人API更新。
要求
- PHP5
- PHP5必须启用Curl。
- 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_token);
- 要启用错误日志文件,请将TelegramErrorLogger.php复制到Telegram.php文件相同的目录中
使用Composer
从您的项目目录中,运行
composer require eleirbag89/telegrambotphp
配置(WebHook)
导航到 https://api.telegram.org/bot(BOT_TOKEN)/setWebhook?url=https://yoursite.com/your_update.php 或使用Telegram类的setWebhook方法。
示例
$telegram = new Telegram($bot_token); $chat_id = $telegram->ChatID(); $content = array('chat_id' => $chat_id, 'text' => "Test"); $telegram->sendMessage($content);
如果您想从Telegram响应中获取一些特定参数
$telegram = new Telegram($bot_token); $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,您需要在循环中调用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的机器人可以有两种不同的键盘:内联和回复。
内联键盘与特定的消息相关联,而回复键盘与整个聊天相关联。
它们都是按钮的数组,代表行和列。
例如,您可以这样安排回复键盘: 使用此代码
$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)
向用户显示回复界面。
查看强制回复获取更多信息。
使用该库的机器人列表
如果你使用此API创建了一个机器人,请通过此问题通知我,我会将其添加到本节。
- Notifyadbot - 语言:波斯语/法语(自2017年9月16日起下线)
- Partners_shakibonline_bot - 语言:波斯语/法语(自2017年9月16日起下线)
- Pishvazrobot - 语言:波斯语/法语 - 是伊朗最受欢迎和最著名的两个移动运营商MTN Irancell和Hamrahe Aval的搜索和收听超过100,000个RBT声音的最全面的机器人
- iHarfBot - 语言:波斯语/法语
- AsansorBot - 语言:波斯语/法语
- IDPbot - 语言:波斯语/法语
- Evil Insult Generator - 语言:英语/德语
- ibelitbot - 语言:波斯语
- skydrivebot - 语言:波斯语 - Instagram帖子下载器 - YouTube链接生成器
- Advanced Pools Bot - 语言:意大利语/英语(贡献)
表情符号
有关在您的机器人消息中使用表情符号的列表,请参阅此表的Bytes列:http://apps.timwhitlock.info/emoji/tables/unicode
联系我
您可以通过Telegram联系我,但如果您有问题请打开一个问题。
支持我
您可以通过Paypal为我买几杯啤酒。