lukasss93/telegrambot-php

此包已被弃用且不再维护。作者建议使用 nutgram/nutgram 包。

Telegram Bot API 框架

安装: 561

依赖: 0

建议者: 0

安全性: 0

星星: 23

关注者: 4

分支: 7

公开问题: 1

类型:项目

v1.14.1 2021-09-07 00:24 UTC

README


TelegramBot-PHP

API PHP CURL

Latest Stable Version Total Downloads License

一个非常简单的用于发送消息的 PHP Telegram Bot API

要求

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

对于 WebHook

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

对于 GetUpdates

  • 一种执行脚本以服务消息的方法(例如 cronjob)

安装

您可以使用 composer 安装此库

composer require lukasss93/telegrambot-php

配置(WebHook)

导航至 https://api.telegram.org/bot(TOKEN)/setWebhook?url=https://yoursite.com/your_update.php 或使用 Telegram 类的 setWebhook 方法。

信息

此简单框架是面向对象的,所有方法都返回 TelegramBot/Types 命名空间中的 Telegram 对象。

示例

use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$bot->sendMessage([
    'chat_id' => $update->message->chat->id,
    'text' => 'Hello world!'
]);

如果您想从 Telegram webhook 获取某些特定参数,只需在对象中调用参数名称即可

use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$text=$update->message->text;

要上传照片或其他文件,您需要使用 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');
$bot->sendPhoto([
    'chat_id' => $chat_id, 
    'photo' => $img
]);

要下载 Telegram 服务器上的文件

$file = $bot->getFile($file_id);
$bot->downloadFile($file->file_path, "./my_downloaded_file_on_local_server.png");

如果您想使用 getUpdates 而不是 WebHook,则需要使用 for 循环。

$updates=$bot->getUpdated();
for ($i = 0; $i < count($updates); $i++) {
    //single update
    $update=$updates[$i];
    if($update->message->getCommand()=='/start'){
        //working!
    }
}

消息对象方法

消息对象主要提供两种方法

  • getCommand()

    //$update->message->text => '/test@ExampleBot my args'
    $command=$update->message->getCommand();
    //$command => '/text'
  • getArgs(bool $asString=false)

    //$update->message->text => '/test@ExampleBot my args'
    $args_array=$update->message->getArgs();
    //$args_array[0] => 'my'
    //$args_array[1] => 'args'
    $args_string=$update->message->getArgs(true);
    //$args_string => 'my args'

构建键盘参数

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

$buttons = [ 
    //First row
    [
        $bot->buildKeyboardButton("Button 1"),
        $bot->buildKeyboardButton("Button 2")
    ], 
    //Second row 
    [
        $bot->buildKeyboardButton("Button 3"),
        $bot->buildKeyboardButton("Button 4"),
        $bot->buildKeyboardButton("Button 5")], 
    //Third row
    [
        $bot->buildKeyboardButton("Button 6")]
    ];
    
$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

当用户点击按钮时,按钮文本会发送回机器人。对于内联键盘,几乎相同(但您需要提供一个有效的 URL 或回调数据)

$buttons = [ 
    //First row
    [
        $bot->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"), 
        $bot->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")
    ], 
    //Second row 
    [
        $bot->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"),
        $bot->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"),
        $bot->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")
    ], 
    //Third row
    [
        $bot->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")
    ]
];

$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildInlineKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

这是所有制作键盘的辅助函数列表

$bot->buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true);

发送自定义键盘。$option 是一个 KeyboardButton 数组。
查看 回复键盘标记 获取更多信息。

$bot->buildInlineKeyBoard(array $inline_keyboard);

发送自定义键盘。$inline_keyboard 是一个 InlineKeyboardButton 数组。
查看 内嵌键盘标记 获取更多信息。

$bot->buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query);

创建一个 InlineKeyboardButton。
查看 内嵌键盘按钮 获取更多信息。

$bot->buildKeyBoardButton($text, $url, $request_contact, $request_location);

创建一个 KeyboardButton。
查看 键盘按钮 获取更多信息。

$bot->buildKeyBoardHide($selective=true);

隐藏自定义键盘。
查看 回复键盘隐藏 获取更多信息。

$bot->buildForceReply($selective=true);

向用户显示回复界面。
查看 强制回复 获取更多信息。

联系我

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

待办事项列表

  • 方法中的可选预测参数
  • 优化键盘
  • 聊天对话
  • 带有回调和事件的命令监听器

变更日志

关于此项目的所有重大变更都将在此处记录 这里