parsapoorsh/telegrambotphp

一个非常简单的PHP Telegram Bot API

2.6.0 2023-05-06 13:46 UTC

README

API PHP CURL

Total Downloads License StyleCI

一个非常简单的PHP Telegram Bot API
符合2023年4月21日Telegram Bot API更新。

要求

  • PHP >= 7.4
  • PHP7的Curl扩展必须启用。
  • Telegram API密钥,您可以通过在创建机器人后使用简单的命令从 @BotFather 轻松获取。

对于WebHook

  • 有效的SSL证书(Telegram API需要此证书)。如果您使用CloudFlare DNS,可以使用 Cloudflare的免费灵活SSL,它加密了从终端用户到代理的Web流量。
    自8月29日更新以来,您可以使用自签名ssl证书。

对于getUpdates(长轮询)

  • 某种执行脚本以提供消息的方式(例如cronjob)

下载

使用Composer

从您的项目目录中,运行

composer require parsapoorsh/telegrambotphp

php composer.phar require parsapoorsh/telegrambotphp

注意:如果您没有Composer,可以在此 下载它

使用发行版存档

https://github.com/parsapoorsh/TelegramBotPHP/releases

使用Git

从项目目录中,运行

git clone https://github.com/parsapoorsh/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');

注意:要启用错误日志文件,还要在Telegram.php文件相同的目录中复制TelegramErrorLogger.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,您需要在循环中调用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以获取完整的示例。

函数

构建键盘

Telegram的机器人可以有两组不同的键盘:内联和回复。
内联键盘与特定消息相关联,而回复键盘与整个聊天相关联。
它们都是按钮的二维数组,代表行和列。
例如,您可以将回复键盘安排如下: ReplyKeyboardExample 使用此代码

$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或回调数据) InlineKeyboardExample

$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 是键盘按钮数组的数组。
有关更多信息,请参阅 回复键盘标记

buildInlineKeyBoard(array $inline_keyboard)

发送自定义键盘。$inline_keyboard 是内联键盘按钮数组的数组。
有关更多信息,请参阅 内联键盘标记

buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query)

创建一个内联键盘按钮。
有关更多信息,请参阅 内联键盘按钮

buildKeyBoardButton($text, $url, $request_contact, $request_location)

创建一个键盘按钮。
有关更多信息,请参阅 键盘按钮

buildKeyBoardHide($selective=true)

隐藏自定义键盘。
有关更多信息,请参阅 回复键盘隐藏

buildForceReply($selective=true)

向用户显示回复界面。
有关更多信息,请参阅 强制回复

表情符号

有关在您的机器人消息中使用表情符号的列表,请参阅此表的“字节”列:http://apps.timwhitlock.info/emoji/tables/unicode

许可证

此开源软件根据MIT许可证分发。请参阅LICENSE.md。

贡献

欢迎所有类型的贡献 - 代码、测试、文档、错误报告、新功能等...

  • 发送反馈。
  • 提交错误报告。
  • 编写/编辑文档。
  • 修复错误或添加新功能。

关于分支

此仓库基于 Eleirbag89/TelegramBotPHP。我分支了它,因为主仓库在那10个月内没有更新,而Telegram Bot API在这段时间内进行了8次更新。

联系我

您可以通过 Telegram 联系我,但如果您有问题,请 打开一个。