我已经能够建立一个Twitter机器人,可以接收并回复直接消息。但后来就出现了这个问题。我有php脚本(bot脚本),它接收来自twitter的消息并相应地回复。现在,这是问题 - 我使用cloud9来托管我的脚本。当用户向我的机器人(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));
}
** - 我的机器人必须在php中完成,并将托管在Windows平台上。 谢谢 - 非常感谢任何帮助/建议。