By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

IRC bot problem

Thekid
100+
P: 145
How can I go about writing a simple IRC bot? I read one thread in this forum about an IRC bot but I couldn't get it to work. I've looked at a few like eggdrop, supybot and phenny but I want to try a very simple basic one so I can understand the code a little better. Where should I start?
Jul 23 '07 #1
Share this Question
Share on Google+
7 Replies


Thekid
100+
P: 145
import sys
import socket
import string
import os

HOST='irc.freenode.net'
PORT=6667
NICK='Thekid'
IDENT='IRCbot'
REALNAME='TheDuke'
OWNER='Me'

s=socket.socket( )
s.connect((HOST, PORT))
s.send('NICK '+NICK+'n')
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'n')
Jul 24 '07 #2

Thekid
100+
P: 145
Why won't something like this work to simply connect to a site and join?

Expand|Select|Wrap|Line Numbers
  1. import irclib
  2.  
  3. # Connection information
  4. network = 'irc.blahblah.net'
  5. port = 6667
  6. channel = '#blah'
  7. nick = 'blah'
  8. name = 'blah'
  9.  
  10. # Create an IRC object
  11. irc = irclib.IRC()
  12.  
  13. # Create a server object, connect and join the channel
  14. server = irc.server()
  15. server.connect ( network, port, nick, ircname = name )
  16. server.join ( channel )
  17.  
  18. # Jump into an infinite loop
  19. irc.process_once()
  20.  
Jul 24 '07 #3

Thekid
100+
P: 145
Why won't something like this work to simply connect to a site and join?

Expand|Select|Wrap|Line Numbers
  1. import irclib
  2.  
  3. # Connection information
  4. network = 'irc.blahblah.net'
  5. port = 6667
  6. channel = '#blah'
  7. nick = 'blah'
  8. name = 'blah'
  9.  
  10. # Create an IRC object
  11. irc = irclib.IRC()
  12.  
  13. # Create a server object, connect and join the channel
  14. server = irc.server()
  15. server.connect ( network, port, nick, ircname = name )
  16. server.join ( channel )
  17.  
  18. # Jump into an infinite loop
  19. irc.process_once()
  20.  
Line 19 should read irc.process_forever() instead of _once
Jul 24 '07 #4

P: 56
That code looks exactly like mine, and I've had mine working for over a onth using irclib. Try setting 'debug' to 1 in the irclib module.
Jul 25 '07 #5

Thekid
100+
P: 145
Thanks for the reply. I've gone in another direction with it and can now connect and have the bot repy to some basic comments but I still have things that need worked out.
Aug 2 '07 #6

Thekid
100+
P: 145
When my bot joins a room it will respond to certain words that are typed. An example would be that if someone typed "hello" the bot would reply "Hello <user>! How are you doing?" My problem is that I can't seem to get the bot to reply to NOTICE or PRIVMSG from a user. I'm not sure what I'm overlooking. I can get the bot to reply with a NOTICE or PRIVMSG to a user but only if the received message is from the channel and NOT a NOTICE or PRIVMSG from a user. example: channel = #bottest
User types "hello"
(I get "Received message from <userinfo> NOTICE #bottest:hello
bot replies NOTICE user "Hello <user>! How are you doing?"
How can I get the bot to respond to a NOTICE from a user instead of a NOTICE from the channel?

I've tried changing the def main() portion to def messageFromUser but that didn't do it. Now before,
I had self.sendmessageToChannel(channel, "blah blah") but had to change 'channel' to 'user' because it would send the reply by NOTICE or PRIVMSG to everyone in the channel instead of to just the user.
I hope I'm making sense.... http://python-forum.org/py/images/sm...n_confused.gif

I've included the portions of code that I think the problem is in, it's not the complete code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #debug set to 'True' so I can read the output
  3. #starts with the usual of host, port, nick, etc.....
  4. #then the ping & pong........
  5.  
  6. # Callbacks you may implement instead of delving into the raw data passed to  IRCBot.act
  7.     def messageFromChannel(self, channel, user, message):
  8.         """ Callback called when a message is received from a channel the bot is residing 
  9. in."""
  10.         dbg("Received message from '" + str(user) + "' in channel '" + "':\n" + message)
  11.  
  12.     def sendMessageToChannel(self, channel, message):
  13.         """ Sends a message to a channel. """
  14.         self.send("NOTICE " + channel + " :" + message)
  15.  
  16.     def messageFromUser(self, user, message, msgtype=None):
  17.         """ Callback called when a message is received from a user."""
  18.         dbg("Received message from '" + user + "':\n" + message)
  19.  
  20.     def sendMessageToUser(self, user, message):
  21.         """ Sends a message to a user. """
  22.         self.send("NOTICE " + user + " :" + message)
  23.  
  24.  
  25.     def userEntered(self, user, channel):
  26.         #""" Callback called when a new user enters a channel you are in. """
  27.         dbg(user + " entered channel " + channel)
  28.     def act(self, data):
  29.         """ Callback which is passed raw data from the IRC server received from the 
  30. socket."""
  31.         pass
  32.  
  33.     def reply(self, data, message):
  34.         """ Sends a message to either a channel or an individual, based on the text 
  35. passed."""
  36.         # call getChannel passing True as the username param.
  37.         self.sendMessageToChannel(self.getChannel(data), message, True)    
  38.  
  39.     def getSenderName(self, data):
  40.         """ Get the name of the person who sent a message, given the raw IRC message."""
  41.         try:
  42.             regSenderName = re.compile(":[\w\s]*")
  43.             return re.sub("^:", "", regSenderName.findall(data)[0])
  44.         except IndexError:
  45.             return None
  46.  
  47.     def getChannel(self, data, username=True):
  48.         """ Gets the channel a message was sent from."""
  49.         # make self.reply handle the checking of user/channel.
  50.         if not data.count("#") and username:
  51.             return self.getSenderName(data)
  52.         else:
  53.             try:
  54.                 channel = data[data.index("#"):]
  55.                 return channel[:channel.index(" ")]
  56.             except ValueError:
  57.                 return None
  58.  
  59.     def recv(self, amount):
  60.         """ Receives text from the IRC server."""
  61.         return self.sock.recv(amount)
  62.  
  63.     def send(self, text):
  64.         """ Send a raw IRC command to the server."""
  65.         self.sock.send(text + "\n\r")
  66.         dbg("bot -> " + text)
  67.  
  68.     # <code removed for sake of space......connection code
  69.  
  70. def baseAct(self, data):
  71.         """ Parses the received data and responds accordingly. It parses the data and calls
  72.             the necessary callbacks."""
  73.         # Call the callbacks.
  74.         channel = self.getChannel(data, False)
  75.         sender = self.getSenderName(data)
  76.         # messageFromChannel
  77.         if channel:
  78.             # Get senders name when message received from channel
  79.             if not sender:
  80.                 sender = None
  81.             self.messageFromChannel(channel, sender, data[data.find(" :") + 2:])
  82.         # userEntered
  83.         elif sender and data[data.find(" ") + 1:].startswith("JOIN :"):
  84.             self.userEntered(sender, data[data.find("JOIN :") + 6:])
  85.         elif sender:
  86.             # Make sure this is a message (NOTICE or PRIVMSG).
  87.             ok = False
  88.             if data[data.find(" ") + 1:].startswith("NOTICE %s :" % self.nick):
  89.                 ok = True
  90.             elif data[data.find(" ") + 1:].startswith("PRIVMSG %s :" % self.nick):
  91.                 ok = True
  92.             # messageFromUser
  93.             if ok:
  94.                 self.messageFromUser(sender, data[data.find(":") + 2:])
  95.             # Pass the data to self.act
  96.         self.act(data)
  97.  
  98. def main():
  99.     """ Called when this module is ran directly. """
  100.     class MrXbot(IRCBot):
  101.         def messageFromChannel(self, channel, user, message):
  102.             if message.startswith("hello"):
  103.                 self.sendMessageToChannel(user, "Hello %s! How are you doing?" % user)
  104.             elif message.startswith("wtf"):
  105.                 self.sendMessageToChannel(user, "That's not very nice behavior " + user )
  106.  
  107.              # < I have a longer list of replies and comments but you get the idea......
  108.  
  109. bot.close()
  110.  
  111.  
Aug 8 '07 #7

Thekid
100+
P: 145
Nevermind, I simplified the code and figured it out.
Aug 15 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.