kyle2142/phpbot

一个简单的库,用于在PHP中使用Telegram的BotAPI

v0.7.1 2019-09-23 21:36 UTC

This package is auto-updated.

Last update: 2024-09-24 08:18:47 UTC


README

我决定创建一个Telegram机器人。看到API后我想

嘿,我何不自己创建一个库呢

这个库没有革命性的特性,但它让我能够开发自己的机器人,并能够根据需要编辑底层代码。

请阅读wiki!

入门

require 'PHPBot.php';
$bot = new kyle2142\PHPBot('12345678:ABCDEF123456890abcdef'); //replace with your token
print_r($bot->api->getMe()); //dump bot info
$me = 98765432; //replace with your ID
$bot->sendMessage($me, "Hello World!");

先决条件

  • PHP >= 7.0
  • cURL扩展已正确安装并启用

安装

您可以使用以下任何一种方法

require __DIR__.'/PHPBot.php';`
  • composer.json
"require": {
    "kyle2142/phpbot": "dev-master"
}
  • CLI: composer require kyle2142/phpbot=dev-master

使用这两种composer方法,请将以下内容添加到您的主文件中

require __DIR__.'/vendor/autoload.php';

用法

接收更新

强烈建议您设置一个webhook(需要SSL),这样您就可以使用以下惊人的简单代码来处理更新

$content = file_get_contents('php://input');
$update = json_decode($content, true);
//do stuff with $update:
if(isset($update['message']['text']) and $update['message']['text'] === "Hello!"){
    $msg_id = $update['message']['message_id'];
    $chat_id = $update['message']['chat']['id'];
    $name = $update['message']['from']['first_name'];
    $bot->sendMessage($chat_id, "Hello $name!", ['reply_to_message_id'=>$msg_id]);
}

调用方法

此库包含一些便利函数,以及访问正常botAPI函数的权限。

在大多数情况下,您可以参考Telegram的BotAPI来获取方法名称和参数。

$params = array('chat_id'=>511048636, 'from_chat_id'=>'@durov', 'message_id'=>79);
$bot->api->forwardMessage($params); //direct api method

$bot->editMessage(/*chat id*/ 511048646, /*msg id*/ 21, "New text!");

如果您真的想,您可以使用PHP的...运算符与便利函数来解包数组

$params = [511048646, 21, "New text!"];
$bot->editMessage(...$params);

请查看PHPBot类的示例和文档以获取详细信息

返回值

成功

在调用任何原始API方法或便利函数时,您将得到一个对象或布尔值的结果

查看输出

php > var_dump($bot->editMessage(343859930, 172, "New text!"));

object(stdClass)#4 (6) {
  ["message_id"]=>
  int(172)
  ["from"]=>
  object(stdClass)#5 (4) {
    ["id"]=>
    int(511048636)
    ["is_bot"]=>
    bool(true)
    ["first_name"]=>
    string(15) "Kyle's test bot"
    ["username"]=>
    string(14) "kyle_s_testbot"
  }
  ["chat"]=>
  object(stdClass)#6 (4) {
    ["id"]=>
    int(343859930)
    ["first_name"]=>
    string(4) "Kyle"
    ["username"]=>
    string(6) "Kyle_S"
    ["type"]=>
    string(7) "private"
  }
  ["date"]=>
  int(1528881693)
  ["edit_date"]=>
  int(1528881742)
  ["text"]=>
  string(9) "New text!"
}

php > var_dump($bot->deleteMessage(343859930, 172));

bool(true)

这意味着您可以这样访问返回值

$result = $bot->sendmessage('@mychannel', "New stuff at example.com!");

$params = ['chat_id'=>343859930, 'from_chat_id' => $result->chat->id, 'message_id' => $result->message_id];
$bot->api->forwardMessage($params); //put params as variable due to line length

错误

如果出现错误,将抛出kyle2142\TelegramException(或其子类)。

例如,让我们尝试删除一个不存在的消息

(注意,我们已经在这里删除了消息172,因此再次尝试删除它将引发错误)

php > var_dump($bot->deleteMessage(343859930, 172));

PHP Warning:  Uncaught TelegramException: 400 (Bad Request: message to delete not found)
Trace:
//trace omitted

当您捕获错误时,您可以获取错误代码和描述(由Telegram返回)

try{
    $bot->deleteMessage(343859930, 172);
}catch(\kyle2142\TelegramExeption $exception){
    echo "Error code was {$exception->getCode()}\n
    Telegram says '{$exception->getMessage()}' ";
}

Error code was 400
Telegram says 'Bad Request: message to delete not found'

如果您的机器人发出很多请求,您可能会遇到429 "FloodWait"错误,这会告诉您何时可以再次发出该请求

try{
    $bot->editMessage(343859930, 173, "beep");
}catch(\kyle2142\TelegramFloodWait $exception){
    echo "We need to wait {$exception->getRetryAfter()} seconds";
}

有问题?

请勿为小问题等打开问题,它那里是为了问题

贡献

如果您想贡献,请确保您遵循当前的样式,并且更改不会破坏任何内容!

测试

要运行测试,您需要将tests/example-config.php重命名/复制到tests/config.php并更新值。

请确保您有正确的测试环境!

例如,GROUP_WITH_ADMIN需要是机器人是管理员的群组的ID,或者跳过需要它们的测试。

许可证

本项目采用MIT许可证 - 请参阅LICENSE文件以获取详细信息

致谢

  • Lonami,他激励我使用Telegram进行开发
  • Telegram,它是一个具有开放API的聊天平台