我正在使用 Twitter API 和 PHP 来回复特定的推文。
例如。如果另一个用户提到我,例如“@Diga 回复我!” 我正在回复他们。否则,我就放弃提及。
这是一个基本的 if 语句,我可以处理这个。问题是,当另一个用户提到我并且我再次运行脚本时,脚本会回复他们两个,因为已经有另一个提及。
$statues = $connection->get("statuses/mentions_timeline");
if(count($statues)>0)
{
$message= strtolower($statues[0]->text);
$message= trim(str_replace("@diga","",$message));
if($message== "reply me!")
{
$user_name = "@".trim($statues[0]->user->screen_name);
$tweetID = $statues[0]->id_str;
$statues = $connection->post("statuses/update", ["status" => $user_name." I can reply you!", "in_reply_to_status_id" => $tweetID]);
}
}
else
{
echo "no mention.";
}
Run Code Online (Sandbox Code Playgroud)
在此代码中,如果有任何提及,脚本就会开始检查消息。实际上,由于测试,我只检查一项提及(0)。但我仍然只想回复这条推文 1 次。之后,即使我运行这个脚本,我也不希望它回复。
您有多种方法可以做到这一点: