Skip to content

Instantly share code, notes, and snippets.

@donatj
Created April 14, 2012 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donatj/2381257 to your computer and use it in GitHub Desktop.
Save donatj/2381257 to your computer and use it in GitHub Desktop.
Simple IRC Bot
#!/usr/bin/php -q
<?php
include('pinch.php');
$pinch = new Pinch('irc.----.com', 'Manbot', '#channel', 'password');
$pinch->on('/:test ([a-z0-9 ]+)/i', function($msg, $obj){
$obj->msg( $msg[1], '#capdig' );
});
$pinch->on('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', function($msg, $obj){
$page = file_get_contents( $msg[0] );
$page = preg_replace('%<(script|style|noscript|title|textarea|th|head)[^>]*>(.*?)</\1>%s', ' ', $page);
do {
$prev_page = $page;
$page = preg_replace('%<(i|b|span|strong|th|head|h1|h2|h3|h4|h5)[^>]*>(?P<content>[^>]*)</\1>%', ' $2 ', $page);
}while( $prev_page != $page );
$page = preg_replace('/[ \t]+/i', ' ', $page);
$page = preg_replace('%<([a-z]+)[^>]*>[^>]{0,20}</\1>%', ' ', $page);
$page = preg_replace('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', ' [link] ', $page);
$page = preg_replace('/ +/i', ' ', preg_replace('/[^a-zA-Z0-9.,\\[\\] ]/', ' ', html_entity_decode(strip_tags($page))));
$page = `osascript -e 'return summarize "{$page}" in 3'`;
$obj->msg( 'Summary: ' . substr($page, 0, 500), '#capdig' );
});
$pinch->on('/(Do|Did|Is|Are|What|Where|Who|Why|When|How) (.*)\?/i', function($msg, $obj){
$search = urlencode($msg[0]);
$url = 'http://api.wolframalpha.com/v2/query?input='.$search.'&format=plaintext&appid={APPID}';
echo PHP_EOL . $url . PHP_EOL;
$data = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadXML($data);
$pts = $doc->getElementsByTagName('plaintext');
$count = 0;
foreach( $pts as $pt ) {
$obj->msg( $pt->textContent, '#capdig');
if( $count++ > 2 ) { break; }
}
});
$pinch->on('/(fake|false|lie|artifical)/', function($msg, $obj){
$data = json_decode( file_get_contents('http://twitter.com/status/user_timeline/fakehenrymoy.json?count=100'), true );
$msg = $data[array_rand($data)]['text'];
$msg = str_replace('"', ' ', $msg);
$msg = trim($msg);
$obj->msg($msg, '#capdig');
});
$pinch->connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment