All pastes #719696 Raw Edit

ron paul donation irc bot

public text v1 · immutable
#719696 ·published 2007-09-29 14:57 UTC
rendered paste body
#!/usr/bin/perl -w

use Net::IRC;
use strict;
use LWP;

# create the IRC object
my $irc = new Net::IRC;

my $nick = 'donbot';
# Create a connection object.  You can have more than one "connection" per
# IRC object, but we'll just be working with one.
my $conn = $irc->newconn(
	Server 		=> shift || 'irc.ronpaul.name',
	Nick		=> $nick,
	Ircname		=> 'How Much',
	Username	=> 'donatebot'
);

# We're going to add this to the conn hash so we know what channel we
# want to operate in.
#$conn->{channel} = shift || '##';
$conn->{channel} = "#ronpaulradio";

sub on_connect {

	# shift in our connection object that is passed automatically
	my $conn = shift;
  
	# when we connect, join our channel and greet it
	$conn->join($conn->{channel});
	$conn->{connected} = 1;

}

sub on_join {

	# get our connection object and the event object, which is passed
	# with this event automatically
	my ($conn, $event) = @_;

	# this is the nick that just joined
	my $nick = $event->{nick};
	# say hello to the nick in public
	$conn->privmsg($conn->{channel}, "Hello, $nick!");
}
	
sub on_part {
	
	# pretty much the same as above

	my ($conn, $event) = @_;

	my $nick = $event->{nick};
	$conn->privmsg($conn->{channel}, "Goodbye, $nick!");
}

sub get_donate {

  # Create a user agent object
  use LWP::UserAgent;
  my $ua = LWP::UserAgent->new;
  $ua->agent("MyApp/0.1 ");

  # Create a request
  my $req = HTTP::Request->new(GET => 'http://www.ronpaul2008.com/webservices/REST/donation-total?format=nvp');

  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
      my $line = $res->content;
      if ($line =~ /sum/){
         $line =~ /sum=(.*?)&/;
         my $amount = "\$" . $1;
         return $amount;
      } else {
	return "No sum found";
      }
  }
  else {
      return "Some error ocurred";
  }
}

sub on_public {

	# on an event, we get connection object and event hash
	my ($conn, $event) = @_;

	# this is what was said in the event
	my $text = $event->{args}[0];
	
	# regex the text to see if it begins with !roker
	if ( ( $text =~ /^\!\$/ || $text =~ /$nick/ ) && $text !~ /Donations are at/) {
		$conn->privmsg($conn->{channel}, 'Donations are at ' . get_donate());
	}
}
$conn->add_handler('public', \&on_public);

# The end of MOTD (message of the day), numbered 376 signifies we've connect
$conn->add_handler('376', \&on_connect);

# start IRC1
$irc->start();