百度首页 | 
百度搜藏
快照
(百度和http://gfxfor.us/general/tutorial-how-to-make-a-simple-irc-bot-from-scratch-in-python的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。)

GFXFOR.US .:. Tutorial: How To Make a Simple IRC Bot From Scratch In Python —

Feature
Technique of the Week #2: Caustics
Feature
Technique of the Week #1: Depth of Field
Registered Users
Find What You're Looking For:
Subscribe
Links
The Categories
The Archives
Preview Image

Tutorial: How To Make a Simple IRC Bot From Scratch In Python

By nzknzknzk

There are several ways to write an IRC bot in Python, but this one is pretty good.
First you make a file called ircBot.py, or whatever you want to call it.
We have to put the she-bang line, which you should include in all your code.
#!/usr/bin/env python
Now we have to import the modules necessary for the program.
import […]

There are several ways to write an IRC bot in Python, but this one is pretty good.

First you make a file called ircBot.py, or whatever you want to call it.

We have to put the she-bang line, which you should include in all your code.

#!/usr/bin/env python

Now we have to import the modules necessary for the program.

import sys
import socket
import string

Now we have to define some variables that we will use in the code.

First is the IRC network to connect to, in our case it is Freenode.
HOST=”irc.freenode.net”

Now the port for Freenode…
PORT=6667

Here just put the nick you want the bot to have, for me it is nzkBot.
NICK=”nzkBot”

Same thing as above…
IDENT=”nzkBot”

Here is the “real name” the bot will assume when whois’d.
REALNAME=”nzksBot”

And finally, the channel. Keep in mind that this bot is not auth’d, so it wont be able to go into many channels. I just made my own.
CHAN=”#nzkbot”

This is to make sure everything works fine.
readbuffer=”"

Now thats done. On to the fun stuff! All the variables are defined, so this doesn’t need much editing.
s=socket.socket( )
s.connect((HOST, PORT))
s.send(”NICK %s\r\n” % NICK)
s.send(”USER %s %s bla :%s\r\n” % (IDENT, HOST, REALNAME))
s.send(”JOIN :%s\r\n” % CHAN)

But here, you can enter the message you want the bot to say. If you want to add more messages, just make more lines as you see here.
s.send(”PRIVMSG %s :%s\r\n” % (CHAN, “What poppin? The popcorn in my kettle, y’all”))
s.send(”PRIVMSG %s :%s\r\n” % (CHAN, “Right?”))

That will be on 2 separate lines.

And finally, the boring part.

while 1:
readbuffer=readbuffer+s.recv(1024)
temp=string.split(readbuffer, “\n”)
readbuffer=temp.pop( )

for line in temp:
line=string.rstrip(line)
line=string.split(line)

if(line[0]==”PING”):
s.send(”PONG %s\r\n” % line[1])

Doesn’t need much explanation. Just makes sure everything is fine and the IRC server can see the bot, hence the PING PONG.

Now, simply run the program and a bot will come into the channel you specified and say something!

Thats all there is to it, good luck!

Heres the full source code.


If you enjoyed this post, get free updates by email or RSS.

Digg This | Del.icio.us | Reddit

12 Responses to this post
  1. anthony Said:
    December 3rd, 2006 at 4:08 pm

    id there a way that when the bot sees a text string in the channel ex. web, it will open a file on your computer ex iexplorer.exe

  2. nzknzknzk Said:
    December 3rd, 2006 at 4:40 pm

    Yes. Although I don’t know how to do it. It has something to do with looking at all messages directed to it and seeing what they say. What would you need such a thing for, anyway?

  3. jhkljkl Said:
    December 19th, 2006 at 10:36 am

    c ?

  4. Rob Said:
    December 19th, 2006 at 12:37 pm

    If you wanted to respond to things that happen in the channel, you just need to do more input parsing on the if() statement at the end of the code.

    Look at the IRC RFC (1419) and see the various commands that can be sent between a client and server.

    Obviously there are nicer ways of doing this but for a simple bot it’s quite easy!

  5. Cartoons Fans Lounge Said:

    […] story No Comments so far Leave a comment RSS feed for comments on this post. TrackBack URI Leave a comment Line and paragraph breaks automatic, e-mail address never displayed, HTMLallowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> […]

  6. bill Said:

    I am no Python expert but when running this script it takes 99% of CPU, is this normal or can it be written some other way.

  7. bill Said:

    Actually this only happens when the nick is in use by someone else. Maybe there should be a check for this.

    If you do a print in the while loop after the readbuffer statement you will get this

    NOTICE AUTH :*** Looking up your hostname…

    NOTICE AUTH :*** Found your hostname, welcome back
    NOTICE AUTH :*** Checking ident

    NOTICE AUTH :*** Got ident response
    :pratchett.freenode.net 433 * asdf :Nickname is already in use.
    :pratchett.freenode.net 451 * JOIN :Register first.
    :pratchett.freenode.net 451 * PRIVMSG :Register first.
    :pratchett.freenode.net 451 * PRIVMSG :Register first.

  8. John Said:

    Here is the same code in ruby:

    require ’socket’

    HOST=”irc.freenode.net”
    PORT=6667
    NICK=”rbasdf”
    IDENT=”rbasdf”
    REALNAME=”rbasdf”
    CHAN=”#rbasdf”

    s = TCPSocket.new HOST, PORT
    s.puts “NICK #{NICK}”
    s.puts “USER #{IDENT} #{HOST} bla :#{REALNAME}”
    s.puts “JOIN :#{CHAN}”
    s.puts “PRIVMSG #{CHAN} :Hello There!”
    s.puts “PRIVMSG #{CHAN} :I am a bot”

    while true do
    line = s.gets.chomp

    s.puts “PONG #{line.spit[1]}” if line.split[0] == “PING”
    end

  9. futureboy Said:

    If you’re looking to build an IRC bot, I recommend the Pircbot framework in Java ( http://www.jibble.org/pircbot.php ). It took me about a half hour to make a really interesting bot that provided a front-end to my software.

  10. Mephux Said:

    New To This: How do I execute the script after modification?

  11. How To Make a Simple IRC bot From SCRATCH In Python! « Universe_JDJ’s News Blog Said:

    […] read more | digg story […]

  12. nzknzknzk Said:
    December 19th, 2006 at 10:17 pm

    Mephux: Just run the script with in a command prompt, make sure you are in an irc channel (the one you specified) in order to be able to see what you are doing.

Feed: RSS

Leave a Reply

Other Recent Posts

Unbeatable Python Resources

Filed under Coding. Created on December 20th.

What Kind of Tutorials Would You Like To See Here?

Filed under General, Site News. Created on December 18th.

Site Forums Are Live!

Filed under Site Changes, Site News. Created on December 17th.

Technique of the Week #4: Inking

Filed under TOTW. Created on December 15th.

Site Forums - To Add or Not To Add?

Filed under Site Changes, Site News. Created on December 13th.

Tutorial: A Guide To The Incredibly Useful OS Module In Python

Filed under Coding, Tutorials. Created on December 10th.

Technique of the Week #3: Radiosity

Filed under TOTW. Created on December 9th.

How To Create a Design Trend

Filed under Graphic Design, Articles. Created on December 6th.

Technique of the Week #2: Caustics

Filed under TOTW. Created on December 1st.

Tutorial: How To Make a Strip of Duct Tape in GIMP

Filed under Graphic Design, Tutorials. Created on December 1st.