Last active
December 17, 2015 05:38
-
-
Save simonwelsh/5558914 to your computer and use it in GitHub Desktop.
A bot for App.net that monitors its PMs and replies with a link to a "I'm feeling lucky" search for the text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A bot for App.net that monitors its PMs and replies with a link to a | |
* "I'm feeling lucky" search for the text. | |
* | |
* @author Simon Welsh (@simon_w) | |
* @license WTFPL Version 2 (http://www.wtfpl.net/txt/copying/) | |
*/ | |
require_once dirname(__FILE__) . '/AppDotNet.php'; | |
// We're going to the user token from dev-lite, so don't need client id/secret set | |
// since they're not used anywhere else. | |
$adn = new AppDotNet(null, null); | |
$adn->includeResponseEnvelope(); | |
$adn->setAccessToken('TOKEN'); | |
while(true) { | |
// This makes a request to: | |
// https://alpha-api.app.net/stream/0/channels?channel_types=net.app.core.pm&include_marker=1&include_read=0&count=200 | |
// Which gets all the PM channels we're subscribed to that have unread messages up to 200 channels in total. | |
// We also include each channel's stream marker. | |
$channels = $adn->getUserSubscriptions(array( | |
'channel_types' => 'net.app.core.pm', | |
'include_marker' => true, | |
'include_read' => false, | |
'count' => 200 | |
)); | |
handleRateLimit(); | |
if($channels['data']) { | |
foreach($channels['data'] as $channel) { | |
// Process channel | |
$marker = $channel['marker']; | |
$channel_id = $channel['id']; | |
// We're only going to want the messages we haven't seen yet. The stream marker tells us | |
// which messages we haven't seen yet. | |
$last_read_id = null; | |
if(isset($marker['last_read_id'])) { | |
$last_read_id = $marker['last_read_id']; | |
} | |
// This makes a request to: | |
// https://alpha-api.app.net/stream/0/channels/$channel_id/messages?since_id=$last_read_id&count=5 | |
$messages = $adn->getMessages($channel_id, array( | |
'since_id' => $last_read_id, | |
'count' => 5, | |
)); | |
handleRateLimit(); | |
if($messages['data']) { | |
// We store the maximum ID of any message we create so we can update the stream marker | |
$max_new_id = 0; | |
foreach($messages['data'] as $message) { | |
// Process message | |
$text = $message['text']; | |
// We're going to make the text a link entity, so we should remove #s and @s | |
// as a link entity can't contain hashtags or mentions. | |
$body = preg_replace('/[#@](\w+)/', '$1', $text); | |
$url = 'http://www.google.com/search?hl=en&btnI=I%27m+Feeling+Lucky&pws=0&q=' . rawurlencode($text); | |
// We get the multibyte length of the string, as it's UTF8 rather than ASCII. | |
// The link entity is going to be the entire text that was given to us | |
$entity = array( | |
'pos' => 0, | |
'len' => mb_strlen($body), | |
'url' => $url, | |
); | |
// Create a new message with our sanitised body and link entity. | |
// This is converted to a JSON object and POSTed to: | |
// https://alpha-api.app.net/stream/0/channel/$channel_id/messages | |
$newMessage = array( | |
'text' => $body, | |
'entities' => array( | |
'links' => array($entity) | |
) | |
); | |
$createdMessage = $adn->createMessage($channel_id, $newMessage); | |
handleRateLimit(); | |
// As it's new, the ID of this message is always going to be greater than the current one | |
$max_new_id = $createdMessage['data']['id']; | |
} | |
if($max_new_id) { | |
// We update the stream marker for the channel, which we requested earlier, to be at least | |
// the id of the last message we created. This is converted to a JSON object and POSTed to: | |
// https://alpha-api.app.net/stream/0/posts/marker | |
$adn->updateStreamMarker(array( | |
'name' => $marker['name'], | |
'id' => $max_new_id, | |
)); | |
handleRateLimit(); | |
} | |
} | |
} | |
} | |
// Sleep for 30 seconds so as to not hammer the API. | |
sleep(30); | |
} | |
function handleRateLimit() { | |
global $adn; | |
if($adn->getRateLimitRemaining() == 0) { | |
sleep($adn->getRateLimitReset()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment