I had tried this a few days ago, but ran into a number of issues using the socket library in python. Jason, a fellow FUBARbian, found out what I was getting wrong - from there, I decided to create a simple template.
I present to you two codes - one's a simple script program that proves I can connect to the chat. Note that I get kicked for a ping time out - I'm yet to teach the bot how to respond to a ping.
The second is the beginning of a larger framework I can hand out at the Python study group on Wednesday - more object oriented in its approach, far easier to expand upon. This one is broken, but I'm too tired to explore why today.
First, the script.
import socket irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) network = "chat.freenode.com" port = 6667 channel = '#fubarlabs' nickname = "namenottakenBot" firstname="Ro" lastname="Botnik" location="FUBARlabs.New_Jersey" irc.connect((network,port)) irc.recv(4096) #Receive the intro stuff to clear the buffer. #Prep the bot irc.send('NICK ' + nickname + '\r\n') irc.send('USER ' + firstname + ' ' + lastname + ' ' + location + ' :' + nickname + '\r\n') irc.send('JOIN ' + channel + '\r\n') while True: print irc.recv(4096)
And the object oriented approach, which again, is unfinished:
import socket class ircBot: irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) network = None port = None channel = None nickname = None firstname = None lastname = None location = None dataBuffer = None def __init__(self, nickname, network=None, port=None, channel=None, firstname="Ro", lastname="Botnik", location="None"): self.nickname = nickname self.network = network self.port = port self.channel = channel self.firstname = firstname self.lastname= lastname self.location= location def connectToServer(self, network=None, port=None): if network is None: if self.network is None: print "Error - No network provided!" return else: self.network = network if port is None: if self.port is None: print "Error - No port provided!" return else: self.port = port self.irc.connect((self.network, self.port)) self.irc.send('NICK ' + self.nickname + '\r\n') self.irc.send('USER ' + self.firstname + ' ' + self.lastname + ' ' + self.location + ' :' + self.nickname + '\r\n') def joinChannel(self, channel=None): if channel is None: if self.channel is None: print "Error - No channel provided!" return else: self.channel = channel self.irc.send('JOIN ' + self.channel + '\r\n') def updateBuffer(self): self.irc.recv(4096)
No comments:
Post a Comment