All pastes #718926 Raw Edit

ron paul donation irc bot

public text v1 · immutable
#718926 ·published 2007-09-28 20:06 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";

# some miscellaneous variables
my $last_donations=0;
my $current_donations=0;

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!");
}
        $conn->privmsg($conn->{channel}, "Hello, $nick!");
sub on_part {
        art {
        # 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;
$last_donations=$current_donations;
$current_donations=$amount;
         return $amount;
      } else {
        return "No sum found";
      }
  }
  else {
      return "Some error occurred";
  }
}

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];
        my $text = $event->{args}[0];
        # regex the text to see if it begins with !roker
        if ( ( $text =~ /^\!\$/ || $text =~ /$nick/ ) && $text !~ /Donations are at/) {
my $message;
get_donate;
my $difference=$current_donations-$last_donations;
if($current_donations>$last_donations)
{
$message= "Donations are at $current_donations.";
}
if($difference==0)
{
$message.=" Nothing has changed!";
}
if($difference<=25)
{
$message.=" Chill out.";
}
                $conn->privmsg($conn->{channel}, $message);
        }
}
$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 IRC
$irc->start();