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?
7 3419
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')
Why won't something like this work to simply connect to a site and join? -
import irclib
-
-
# Connection information
-
network = 'irc.blahblah.net'
-
port = 6667
-
channel = '#blah'
-
nick = 'blah'
-
name = 'blah'
-
-
# Create an IRC object
-
irc = irclib.IRC()
-
-
# Create a server object, connect and join the channel
-
server = irc.server()
-
server.connect ( network, port, nick, ircname = name )
-
server.join ( channel )
-
-
# Jump into an infinite loop
-
irc.process_once()
-
Why won't something like this work to simply connect to a site and join? -
import irclib
-
-
# Connection information
-
network = 'irc.blahblah.net'
-
port = 6667
-
channel = '#blah'
-
nick = 'blah'
-
name = 'blah'
-
-
# Create an IRC object
-
irc = irclib.IRC()
-
-
# Create a server object, connect and join the channel
-
server = irc.server()
-
server.connect ( network, port, nick, ircname = name )
-
server.join ( channel )
-
-
# Jump into an infinite loop
-
irc.process_once()
-
Line 19 should read irc.process_forever() instead of _once
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.
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.
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: -
-
#debug set to 'True' so I can read the output
-
#starts with the usual of host, port, nick, etc.....
-
#then the ping & pong........
-
-
# Callbacks you may implement instead of delving into the raw data passed to IRCBot.act
-
def messageFromChannel(self, channel, user, message):
-
""" Callback called when a message is received from a channel the bot is residing
-
in."""
-
dbg("Received message from '" + str(user) + "' in channel '" + "':\n" + message)
-
-
def sendMessageToChannel(self, channel, message):
-
""" Sends a message to a channel. """
-
self.send("NOTICE " + channel + " :" + message)
-
-
def messageFromUser(self, user, message, msgtype=None):
-
""" Callback called when a message is received from a user."""
-
dbg("Received message from '" + user + "':\n" + message)
-
-
def sendMessageToUser(self, user, message):
-
""" Sends a message to a user. """
-
self.send("NOTICE " + user + " :" + message)
-
-
-
def userEntered(self, user, channel):
-
#""" Callback called when a new user enters a channel you are in. """
-
dbg(user + " entered channel " + channel)
-
def act(self, data):
-
""" Callback which is passed raw data from the IRC server received from the
-
socket."""
-
pass
-
-
def reply(self, data, message):
-
""" Sends a message to either a channel or an individual, based on the text
-
passed."""
-
# call getChannel passing True as the username param.
-
self.sendMessageToChannel(self.getChannel(data), message, True)
-
-
def getSenderName(self, data):
-
""" Get the name of the person who sent a message, given the raw IRC message."""
-
try:
-
regSenderName = re.compile(":[\w\s]*")
-
return re.sub("^:", "", regSenderName.findall(data)[0])
-
except IndexError:
-
return None
-
-
def getChannel(self, data, username=True):
-
""" Gets the channel a message was sent from."""
-
# make self.reply handle the checking of user/channel.
-
if not data.count("#") and username:
-
return self.getSenderName(data)
-
else:
-
try:
-
channel = data[data.index("#"):]
-
return channel[:channel.index(" ")]
-
except ValueError:
-
return None
-
-
def recv(self, amount):
-
""" Receives text from the IRC server."""
-
return self.sock.recv(amount)
-
-
def send(self, text):
-
""" Send a raw IRC command to the server."""
-
self.sock.send(text + "\n\r")
-
dbg("bot -> " + text)
-
-
# <code removed for sake of space......connection code
-
-
def baseAct(self, data):
-
""" Parses the received data and responds accordingly. It parses the data and calls
-
the necessary callbacks."""
-
# Call the callbacks.
-
channel = self.getChannel(data, False)
-
sender = self.getSenderName(data)
-
# messageFromChannel
-
if channel:
-
# Get senders name when message received from channel
-
if not sender:
-
sender = None
-
self.messageFromChannel(channel, sender, data[data.find(" :") + 2:])
-
# userEntered
-
elif sender and data[data.find(" ") + 1:].startswith("JOIN :"):
-
self.userEntered(sender, data[data.find("JOIN :") + 6:])
-
elif sender:
-
# Make sure this is a message (NOTICE or PRIVMSG).
-
ok = False
-
if data[data.find(" ") + 1:].startswith("NOTICE %s :" % self.nick):
-
ok = True
-
elif data[data.find(" ") + 1:].startswith("PRIVMSG %s :" % self.nick):
-
ok = True
-
# messageFromUser
-
if ok:
-
self.messageFromUser(sender, data[data.find(":") + 2:])
-
# Pass the data to self.act
-
self.act(data)
-
-
def main():
-
""" Called when this module is ran directly. """
-
class MrXbot(IRCBot):
-
def messageFromChannel(self, channel, user, message):
-
if message.startswith("hello"):
-
self.sendMessageToChannel(user, "Hello %s! How are you doing?" % user)
-
elif message.startswith("wtf"):
-
self.sendMessageToChannel(user, "That's not very nice behavior " + user )
-
-
# < I have a longer list of replies and comments but you get the idea......
-
-
bot.close()
-
-
Nevermind, I simplified the code and figured it out.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kostatus |
last post by:
I have a virtual function in a base class, which is then overwritten by a
function of the same name in a publically derived class. When I call the
function using a pointer to the derived class...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
by: Ian Stanley |
last post by:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the...
|
by: Jon Davis |
last post by:
If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...
BaseClass bc = new ChildClass();...
|
by: Ammar |
last post by:
Dear All,
I'm facing a small problem.
I have a portal web site, that contains articles, for each article, the end
user can send a comment about the article.
The problem is:
I the comment length...
|
by: Dany |
last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
|
by: Mike Collins |
last post by:
I cannot get the correct drop down list value from a drop down I have on my
web form. I get the initial value that was loaded in the list.
It was asked by someone else what the autopostback was...
|
by: =?Utf-8?B?am8uZWw=?= |
last post by:
Hello All,
I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM).
On some
devices the IM will not start. The IM appears in the IM-List but when it is
selected from the...
|
by: sherifbk |
last post by:
Problem description
==============
- I have 4 clients and 1 server (SQL server)
- 3 clients are Monitoring console 1 client is operation console
- Monitoring console collects some data from...
|
by: AceKnocks |
last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |