Thread: IRC Bot

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    IRC Bot


    I'm interessted in making a simple IRC bot, but I'm knew at Python and have no experience in making bots. Does anybody know any good resources? How much work is it to write one to simple connect to an IRC network and say "Hello, World!" ?

    From what I understand, Strike, you are developing an IRC bot (it's acutaly where I got the idea ), but I downloaded the source and it looks a bit much for me, hehe .
    Last edited by XxChris; January 28th, 2004 at 07:15 PM.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Basic example (the IRC thing i wrote for X):

    Code:
    #!/usr/bin/env python
    
    import socket
    
    class IRC:
    
    	def __init__(self):
    		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    	def connect(self):
    		try:
    			self.sock.connect((self.host, self.port))
    			self.sock.send('NICK %s\n\r' % self.nick)
    			self.sock.send('USER localhost localhost localhost : Python Powered\n\r')
    			self.sock.send('JOIN %s\n\r' % self.room)
    			self.sock.send('PRIVMSG %s : Python Powered\n\r' % self.room)
    			self.sock.send('NAMES\n\r')
    
    			while True:
    				print self.recieve()
    
    		except:
    			print 'Error Occured'
    		
    	def recieve(self):
    		line = self.sock.recv(self.byte)
    
    		if line.startswith('PING'):
    			self.sock.send('PONG : PING\n\r')
    			return 'PONG has been sent'
    		else:
    			return line
    
    if __name__ == '__main__':
    
    	pybot = IRC()
    	pybot.host = 'irc.h4ckerx.net'
    	pybot.room = '#bot'
    	pybot.nick = 'Netytan'
    	pybot.port = 6667
    	pybot.byte = 1025
    	pybot.connect()
    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks alot for the code. I'm not sure if it's working right, though (for Quakenet anyway)

    Code:
    [Aurora:~/dev/python] chris% python pybot.py
    NOTICE AUTH :*** Looking up your hostname
    
    NOTICE AUTH :*** Checking Ident
    
    NOTICE AUTH :*** Found your hostname
    
    NOTICE AUTH :*** No ident response
    
    PONG has been sent
    :tiscali.dk.quakenet.org 513 shpleh :To connect, type /QUOTE PONG 980340643
    [edit] It didn't work on irc.wyldryde.net but did on irc.freenode.net. What's making the difference between servers and is it easy to fix? [/edit]
    Last edited by XxChris; January 28th, 2004 at 07:52 PM.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmm, sorry, don't know what the problem here is sorry, works fine for me as is... Think Strikes the guy for this one!!!

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Actually, as far as low-level IRC stuff goes, I'm not so great with it. Though this is the second Python bot I'll have worked with (the first one being one that I built along with another coder), I've never actually built the base IRC library. For moobot (the first bot, the one for which I did the majority of the coding), I used Joel Rosdahl's IRC library. For supybot, I'm using jemfinch's (project founder) IRC library which is a lot better than Joel's, though a bit more complex, I believe.

    I'd recommend using an existing IRC library and playing around with that in order to make your bot. Joel's will do for you (it's the most widely used, I think, search for python irclib), but note that it is limited in capability. I believe Twisted has one as well, I'm not too familiar with it. And jemfinch's is of course, excellent, but I'm not so sure how complex it is or how newbie friendly it might be.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks Strike. One question though: I see that when I talk in the channel, my message appears in the Bot window. For the bot to recognize commands, do I simply parse that output?

    Code:
    :port80_!~chris@bleh.net PRIVMSG #thisisatest :Test
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Originally posted by XxChris
    Thanks Strike. One question though: I see that when I talk in the channel, my message appears in the Bot window. For the bot to recognize commands, do I simply parse that output?

    Code:
    :port80_!~chris@bleh.net PRIVMSG #thisisatest :Test
    Are you trying to develop for supybot? If so, check out all the stuff in the docs/ folder. There are a lot of good tips and hints in there. If you want more specific info, feel free to drop by #supybot on either irc.freenode.net or irc.oftc.net (a supybot runs a relay between the two) and ask people who are about. jemfinch (the founder/creator), jamessan, and I in particular are fairly well-versed on the internals of the bot.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    At the moment I've just been messing with netytan's code and what I posted earlier was the output I get when I type something into the channel. I'm just not sure if this is the proper way to parse for a command like ".say Hello"... I'd see:

    Code:
    rt80_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello
    Parse out '.say Hello' and get the bot to say 'Hello'.

    I will take a look at supybot when I have a bit more time, and I'll probably drop by #supybot sometime
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    1
    Rep Power
    0
    Originally Posted by XxChris
    Thanks alot for the code. I'm not sure if it's working right, though (for Quakenet anyway)

    Code:
    [Aurora:~/dev/python] chris% python pybot.py
    NOTICE AUTH :*** Looking up your hostname
    
    NOTICE AUTH :*** Checking Ident
    
    NOTICE AUTH :*** Found your hostname
    
    NOTICE AUTH :*** No ident response
    
    PONG has been sent
    :tiscali.dk.quakenet.org 513 shpleh :To connect, type /QUOTE PONG 980340643
    [edit] It didn't work on irc.wyldryde.net but did on irc.freenode.net. What's making the difference between servers and is it easy to fix? [/edit]
    Heya, I had the same problem and just found the solution. Although this thread is old enough that you probably already know the answer lol :P

    http://web.irc.org/mla/ircd-users/2002/msg00141.html

    Basically, the server is PINGing your client with a random number and you have to PONG that number back at him. All you're sending at the moment is a straight string "PONG : PING" which is incorrect. The link I provided explains it much better than I can lol

    Steve
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    1
    Rep Power
    0
    I dont know Python at all but i know why its asking to quote with the unique PONG is part of the p10 protocol. You have to add a snippit into the PONG reply in your script to send back the PONG with the quote'd number. If that makes any sense at all... It happens with really any language you program bots with. p10 or I think most server protocol's these days will require you send a PONG back with the respective number it generates.

IMN logo majestic logo threadwatch logo seochat tools logo