的Heroku,PHP,exec和电报机器人
问题描述:
我已经创建了一个电报机器人和我想能够使当电报机器人接收特定使用exec命令在index.php执行在背景另一个PHP(worker.php)来自用户的消息。 如果可能的话,我希望以比Heroku example更简单的方式做到这一点。 worker.php代码也应该能够发送消息给电报聊天,因为index.php正在执行。 然后我设置了这个例子来检查参数传递(特别是聊天信息),但事实并非如此。这是代码,有人可以帮忙吗?目前并不真正在后台运行,因为/ dev/null &命令未在“exec”命令中设置。的Heroku,PHP,exec和电报机器人
在 “发件人” 的一面:
$update_j = json_encode($update);
$response = $client->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => $update_j
]);
exec('php /app/worker.php?update=' . $update_j, $out, $ret_v);
的$ update_j输出看起来是正确的:
{"update_id":XXXXXXXXX,"message":{"message_id":YYYY,"from":{"id":ZZZZZZZZ,"first_name":"Name"},"chat":{"id":ZZZZZZZZ,"first_name":"Name","type":"private"},"date":WWWWWWWWWWW,"text":"\/runjob","entities":[{"type":"bot_command","offset":0,"length":7}]}}
在worker.php侧,代码是这样的:
<?php
require 'vendor/autoload.php';
require 'require.php';
try {
$foo = file_get_contents("php://input");
$out = json_decode($foo, true);
$update = $out->{'update'};
$response = $client->sendChatAction(['chat_id' => $update->message->chat->id, 'action' => 'typing']);
$response = $client->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => "var_dump = " . var_dump($out)
]);
} catch (\Zelenin\Telegram\Bot\NotOkException $e) {}
$ ret_v的值为1.我没有收到任何包含var_dump($ out)值的消息。
有人可以帮助解决问题吗? 谢谢。
答
正如你现在text
在sendMessage应该是一个字符串
所以,你可以使用json_encode
代替var_dump()
串json_encode(混合$值[摘要$选项= 0 [,int $ depth = 512]])
返回一个字符串,其中包含的JSON表示价值。
更新您的代码:
$response = $client->sendMessage([
'chat_id' => $update->message->chat->id,
'text' => "var_dump = " . json_encode($out)
]);