在Twitter的Twitter机器人的问题?

时间:2017-03-02 09:47:34

标签: php twitter

我已经在php中构建了一个twitter机器人,它能够接收消息并响应消息,但之后就出现了这个问题。当我向我的机器人发送消息时,我必须刷新机器人脚本以便机器人回复。我希望机器人不断检查任何新的传入消息并做出相应的响应。我该如何解决这个错误? 这是我的代码 -

 <?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));
}

0 个答案:

没有答案