我正在尝试创建一个Telegram机器人,该机器人可以获取网页的屏幕截图,并在调用某个命令后将其中继回用户。为此,我正在使用php-telegram-bot和php-phantomjs。
到目前为止,我已经设法制作了一个可以成功拍摄屏幕截图并将其保存到服务器的脚本。我还做了返回新捕获图像的命令。但是,当我尝试将两者结合起来以使流程自动化时,我的include
语句似乎不起作用。
例如,TestCommand.php
(如下)接受用户输入,并且在execute()
函数内调用createimage.php
以生成图像。我认为范围可能存在一些问题,但是我无法在运行createimage.php
函数时想出其他方法来执行execute()
文件,从而最终结果将发送新的屏幕截图。
这里是TestCommand.php
:
<?php
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
class ChartCommand extends UserCommand
{
protected $name = 'test'; // Your command's name
protected $description = 'test desc'; // Your command description
protected $usage = '/test'; // Usage of your command
protected $version = '1.0.0'; // Version of your command
public function execute()
{
$message = $this->getMessage(); // Get Message object
$chat_id = $message->getChat()->getId(); // Get the current Chat ID
include "script.php"; // Include file to generate image
$testImage = "test.jpg"; // Set image url
Request::sendPhoto([
'chat_id' => $chat_id,
'photo' => Request::encodeFile($testImage),
]);
}
}
下面是制作图像的文件,如果作为独立文件执行,则效果很好:
<?php
require __DIR__ . '/vendor/autoload.php';
use JonnyW\PhantomJs\Client;
$client = Client::getInstance();
// isLazy() is useful when we want to wait for all resources on the page to load.
$client->isLazy();
// Some options
$client->getEngine()->addOption('--load-images=true');
$client->getEngine()->addOption('--ignore-ssl-errors=true');
$vWidth = 2560;
$vHeight = 1440;
$width = 1910;
$height = 670;
$top = 95;
$left = 15;
// Here specify the page to take the screenshot
$request = $client->getMessageFactory()->createCaptureRequest('https://google.com');
$request->setCaptureDimensions($width, $height, $top, $left);
$request->setViewportSize($vWidth, $vHeight);
$request->setBodyStyles(['backgroundColor' => '#ffffff']);
$file = 'test.jpg';
$request->setOutputFile($file);
$response = $client->getMessageFactory()->createResponse();
// Send the request
$client->send($request, $response);