So lets get started, the basics are very simple there are several ways to write an IRC bot in Python, but this one is pretty good. If you want to go more advanced I would suggest looking into twistedmatrix.com Twisted.
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 is the full source code:
#!/usr/bin/env python
import sys
import socket
import string
HOST="irc.freenode.net"
PORT=6667
NICK="asdf"
IDENT="asdf"
REALNAME="asdf"
CHAN="#asdf"
readbuffer=""
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)
s.send("PRIVMSG %s :%s\r\n" % (CHAN, "Hello There!"))
s.send("PRIVMSG %s :%s\r\n" % (CHAN, "I am a bot"))
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])
Follow Me!