2

I have built a twitter bot in php and it's able to receive messages and respond to the messages but then there is this problem. When I send a message to my bot, I have to refresh my bot script for the bot to reply back. I would like the bot to constantly check for any new incoming message and respond accordingly. How can I fix this error? Here is my code -

 <?php
 ini_set('display_errors', 1);
 require_once('twitterapiexchange.php'); 
 require_once("twitteroauth.php");

$settings = array( 
    'oauth_access_token' => "XXXXXXXXXXXX",
    'oauth_access_token_secret' => "XXXXXXXXXXXX",
    'consumer_key' => "XXXXXXXXXXXX",
    'consumer_secret' => "XXXXXXXXXXXX");
  $url = 'https://api.twitter.com/1.1/direct_messages.json';
  $getfield = '?since_id=240136858829479935&count=1';
  $requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest(),$assoc = TRUE);

foreach($string as $items)
 {
            $url = 'https://api.twitter.com/1.1/direct_messages/show.json?';
            $requestMethod = 'GET';
            $getfields = array('id' => $items['id']);
            $twitter = new TwitterAPIExchange($settings);           
            $do = $twitter->setGetfield($getfield)
                          ->buildOauth($url, $requestMethod)                         
                          ->performRequest();
            echo "<strong>Teet:</strong> ".$items['text']."<br />";

            $senderId = $items['sender_id'];
            $messageText = $items['text'];
            $timeStamp = $items['created_at'];
            $recipentId = $items['recipient_id'];
            $messageId = $items['id'];
            $screenname=$items['sender_screen_name'];

            if(!empty($messageText)) {

  $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452');
  $obj = json_decode($json);
  //var_dump($obj->results[0]->formatted_address);
  $addr = $obj->results[0]->formatted_address;
    $answer = "You said ".$messageText . " " .$addr;
 echo "<strong>Answer:</strong> ".$answer."<br />";


$api_key='XXXXXXXXXXXX' ;
$api_secret= 'XXXXXXXXXXXX' ;
$access_token = 'XXXXXXXXXXXX';
$access_token_key='XXXXXXXXXXXX' ;
$connection = new TwitterOAuth($api_key,$api_secret, $access_token, $access_token_key);
$connection->post('direct_messages/new', array('user_id' => $senderId, 'text' => $answer));


}




//var_dump(json_encode($items, true));
}
LisaHayden
  • 43
  • 1
  • 4
  • 1
    If you want to check for new messages at a specific time interval, `Cronjobs` are what you're looking for. – Twinfriends Mar 02 '17 at 09:50
  • @Twinfriends:- Is there any other way to do it in the above php code?I have to run the script in windows. – LisaHayden Mar 02 '17 at 09:58
  • There is "Windows Task Scheduler" - its the same as cronjobs, but for windows. With PHP - No. There is no way. PHP runs serverside and will execute the script simply once. To execute it a second time you need a page reload, a cronjob (principally nothing else than a "page reload") or a call from a external source (Javascript -> AJAX). PHP can't know if there are new messages. Javascript could be a solution too, but I don't know if its possible in this case. – Twinfriends Mar 02 '17 at 10:06
  • how do I run a PHP script using windows schedule task? http://stackoverflow.com/questions/4701861/how-do-i-run-a-php-script-using-windows-schedule-task – Yohanes Gultom Mar 06 '17 at 12:35

0 Answers0