Basicbot

Catagories: bots, perl

Perl Bot::BasicBot

This is a simple irc bot class you can base your bot off. It handles the basics of connecting to an irc server and channels.

https://metacpan.org/pod/Bot::BasicBot

This isn't likely the class you want to build your bot off of as it is very limited beyond the basics. Its the base class used by the next one though, so the documentation is relevant for all bots based off of it.

Perl Bot::BasicBot::Pluggable

This is the main class you will want to base your bot off of. It's based off Bot::BasicBot but also adds pluggable modules.

https://metacpan.org/pod/Bot::BasicBot::Pluggable

Installing

To create your bot, you'll need to install the perl modules. Easiest way to do this is with cpan command. This will install all the dependencies so expect it will take a while to complete and produce a lot of output. You'll need more then a coffee break; might I suggest finding a book or movie while you wait... :( I'm quite sure not all of the packages installed are needed so this could probably be optimized at some point. The output is limited to just the relevant parts of the install.

cpan -i Bot::BassicBot::Pluggable

(...)
Building Bot-BasicBot-Pluggable                                                                                               
Installing /usr/local/man/man1/bot-basicbot-pluggable.1                                                                       
Installing /usr/local/man/man1/bot-basicbot-pluggable-cgi.1                                                                   
Installing /usr/local/libdata/perl5/site_perl/App/Bot/BasicBot/Pluggable.pm                                                   
Installing /usr/local/libdata/perl5/site_perl/App/Bot/BasicBot/Pluggable/Terminal.pm                                          
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable.pm                                                       
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module.pm                                                
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Store.pm                                                 
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Terminal.pm                                              
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Auth.pm                                           
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Base.pm                                           
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/ChanOp.pm                                         
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/DNS.pm                                            
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Google.pm                                         
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Infobot.pm                                        
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Join.pm                                           
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Karma.pm                                          
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Loader.pm                                         
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Seen.pm                                           
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Title.pm                                          
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Module/Vars.pm                                           
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Store/DBI.pm                                             
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Store/Deep.pm                                            
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Store/Memory.pm
Installing /usr/local/libdata/perl5/site_perl/Bot/BasicBot/Pluggable/Store/Storable.pm
Installing /usr/local/libdata/perl5/site_perl/Test/Bot/BasicBot/Pluggable.pm
Installing /usr/local/libdata/perl5/site_perl/Test/Bot/BasicBot/Pluggable/Store.pm
Installing /usr/local/man/man3p/App::Bot::BasicBot::Pluggable.3p
Installing /usr/local/man/man3p/App::Bot::BasicBot::Pluggable::Terminal.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Auth.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::ChanOp.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::DNS.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Google.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Infobot.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Store.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Join.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Karma.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Loader.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Seen.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Title.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Module::Vars.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Store::DBI.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Store::Deep.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Store::Memory.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Terminal.3p
Installing /usr/local/man/man3p/Bot::BasicBot::Pluggable::Store::Storable.3p
Installing /usr/local/man/man3p/Test::Bot::BasicBot::Pluggable.3p
Installing /usr/local/man/man3p/Test::Bot::BasicBot::Pluggable::Store.3p
Installing /usr/local/bin/bot-basicbot-pluggable
Installing /usr/local/bin/bot-basicbot-pluggable-cgi
Installing /usr/local/bin/bot-basicbot-pluggable-cli
Installing /usr/local/bin/bot-basicbot-pluggable-infobot-upgrade
Installing /usr/local/bin/bot-basicbot-pluggable.rej
Installing /usr/local/bin/storable2dbi.pl
  BIGPRESH/Bot-BasicBot-Pluggable-1.30.tar.gz
  ./Build install  -- OK

Create a User

You'll want to run your bot in its own user account for security reasons.

doas useradd -m mybot
doas su - mybot

Create your bot

The code snip from the perldoc page is a good starting point for your bot. The following example connects to the ircnow network.

vi mybot.pl

#!/usr/bin/perl
use strict;
use warnings;
use Bot::BasicBot::Pluggable;

#Create your bot and connect it to ircnow
my $bot = Bot::BasicBot::Pluggable->new(
  channels => ["#bottest"],
  server   => "irc.ircnow.com",
  port     => "6667",

  nick      => "pluggabot",
  alt_nicks => ["pbot", "pluggable"],
  username  => "bot",
  name      => "Yet Another Pluggable Bot", 
);

print "Starting Bot\n";
$bot->run();

Save and quit with :wq, then make it executable and run it like this:

chmod +x mybot.pl
./mybot.pl

That's it! You should now see your bot in the #bottest channel if all went well. Congratulations! Unfortunately, it wont do anything more then join the channel at this point. You hit ctrl-c to stop it.

NOTE: If the bot doesn't join the channel, it may be a firewall issue or port conflict. If you're running this on your irc server, you want to use server => "localhost" or it wont be able to connect as port 6667 is already in use for incoming traffic.

Loading Modules

To make your bot do something useful, you'll need to add some modules. We'll start with the following:

Add the following lines to your mybot.pl before the $bot->run();

# Load modules
my $modules=[qw{Loader Auth}];
my $mod={};
foreach (@$modules) {
    $mod->{$_}=$bot->load($_);
}

print "Starting Bot\n";
$bot->run();

WARNING: The Auth module has a hard coded password for admin of julia. You will need to change this. See the link above for Auth for details.

You'll need to /query the bot to authenticate before you can issue commands to it. Once authenticated, you'll be able to request a list of available modules thanks to the loader module. See the doc link above for it

!list

<pluggabot> Loaded modules: Auth, Loader
<pluggabot> ..Available modules: ChanOp, DNS, Google, Infobot, Join, Karma, Seen, Title, Vars

Links to Modules

The following are some of the modules available:

  • Auth - Basic authentication module. Very basic - I may write something better at some point...
  • Loader - Lets you Load/unload and list modules on the fly.
  • Join - Lets you Join/part channels on the fly.
  • Seen - Tracks when a nick was last seen.
  • Karma - tracks karma for various concepts.
  • DNS - Perform dns lookups for names and ip addresses. Usage: 'dns <ip address>' for the hostname, 'nslookup <hostname>' for the IP address.
  • Infobot - Allows users to add factoids for the bot to recall on demand. try asking it wiki? the trigger is the ? See the link for programming it.
  • wiki - Module izzyb created to track changes on the wiki and post notices in channels.

Creating your own Bot::BasicBot::Pluggable::Module

Without going into too much detail of how perl modules work, this is a quick overview to get you started.

First off, we'll assume you're pluggin isn't installed in the system, so you'll need a local lib directory in your bots home directory to create your modules in.

mkdir -p lib/Bot/BasicBot/Pluggable/Module

This is the directory where you will create your Modules. Lets call this one MyModule.

vim lib/Bot/BasicBot/Pluggable/Module/MyModule.pm

It should look something like this:

package Bot::BasicBot::Pluggable::Module::MyModule;

use warnings;
use strict;
use base qw(Bot::BasicBot::Pluggable::Module);

our $VERSION = '0.01';

sub help {
  return "MyModule: My first BasicBot Module";
}

sub told {
  my ($self,$message) = @_;
  # Only act if we are addressed
  if($message->{address}) {
    my $body = $message->{body};
    my $channel = $message->{channel};
    if ($channel eq 'msg') { # was a private message so respond in private message
      $channel = $message->{who};
    }
    if($body =~ /MyModule/i) { # Only reply if MyModule Mentioned
      my $reply = qq/Hello $message->{who}, This response is from MyModule./;
      return $reply;
    }
  }
  return 0;
}

1;

To use this new module, you'll want to tell your bot to use the new lib dir and add your modules to the $modules list like this:

use lib qw(./lib);

my $modules=[qq{Loader Auth MyModule}];

The final bot.pl code from above would look like this now:

#!/usr/bin/perl
use strict;
use warnings;
use Bot::BasicBot::Pluggable;
use lib qw(./lib);

#Create your bot and connect it to ircnow
my $bot = Bot::BasicBot::Pluggable->new(
  channels => ["#bottest"],
  server   => "irc.ircnow.com",
  port     => "6667",

  nick      => "pluggabot",
  alt_nicks => ["pbot", "pluggable"],
  username  => "bot",
  name      => "Yet Another Pluggable Bot", 
);

# Load modules
my $modules=[qw{Loader Auth MyModule}];
my $mod={};
foreach (@$modules) {
    $mod->{$_}=$bot->load($_);
}

print "Starting Bot\n";
$bot->run();

Catagories: bots, perl