0

I'm using Twitter API and PHP to reply a specific tweet.

For example. If another user mentions me like "@Diga reply me!" I'm replying them. Otherwise, I just discard the mention.

This is a basic if statement and I could handle this. The problem is, when another user mentions me and I run the script again, scripts replies both of them because there is already another mention.

$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.";
}

In this code, if there are any mentions, scripts starts checking the messages. Actually, I'm only checking one mention (0) because of testing. But still, I want to reply this tweet only 1 time. After that, even if I run this script, I don't want it to reply.

Diga
  • 481
  • 2
  • 9
  • 20

1 Answers1

5

You have multiple ways to do that :

  1. Use a database to store the ID of the tweets you already have replied and ignore those where the ID is already in the database.
  2. If your script run automatically every 10 minutes, only reply to mentions from tweets sent in the last 10 minutes.
  3. Look if you have already replied to the tweet.