有一个PHP Telegram机器人,当用户发送单词时,它会在数据库中搜索并找到适当的答案并回复用户的消息。 我不希望该机器人立即发送答案并希望延迟一些时间来答复,该时间已保存在数据库中
在将答案发送回用户之前,我使用了sleep($row[delay])
,但是当我发送一些单词时,机器人只会连续多次答复前一个。
这些是我的代码:
function delay(){
global $db;
$query="SELECT * FROM Turningoff WHERE 1";
$result = mysqli_query($db, $query);
$row = mysqli_fetch_assoc($result);
sleep($row[delay]);
}
function find($sharh,$chat_id,$message_id)
{
global $db;
$query = "select distinct sel.id_forooshande,sel.message from Forooshande As sel
INNER JOIN Kala_forooshande AS ss ON sel.id_forooshande = ss.id_forooshande
INNER JOIN Kala AS s ON ss.id_kala=s.id_kala
WHERE REPLACE(`sharhe_kala`, '/', '')='" . $sharh . "'
$res = mysqli_query($db, $query);
while ($row = mysqli_fetch_assoc($res)) //For each forooshande
{
$query2 = "SELECT s.mark, s.sharhe_kala FROM Kala AS s
INNER JOIN Kala_forooshande AS ss ON ss.id_kala=s.id_kala
INNER JOIN Forooshande AS sel ON sel.id_forooshande = ss.id_forooshande
WHERE (REPLACE(`sharhe_kala`, '/', '')='" . $sharh . "')
AND sel.id_forooshande=" . $row[id_forooshande]; //find available kala
$res2 = mysqli_query($db, $query2);
$javab = $row[message] . "\r\n";
while ($row2 = mysqli_fetch_assoc($res2)) {
$javab .= $row2[mark] . "_" . $row2[sharhe_kala] . "\r\n";
delay();
message($chat_id, urlencode($javab), $message_id);
}
}
当用户发送东西时,我调用了find()函数。
我应该使用数据库来保存每个单词并在延迟时间之后一个接一个地回答吗?还是有其他解决方案?