![]() 快照
|
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.
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
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?
c ?
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!
[…] 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> […]
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.
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.
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
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.
New To This: How do I execute the script after modification?
[…] read more | digg story […]
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.