Python IRC bot using Twisted

D

ddtm

I'm using an example of IRC bot (_ttp://twistedmatrix.com/projects/
words/documentation/examples/ircLogBot.py) to create my own bot. But I
have a problem. I'm trying to make my bot send messages periodically.
But I can't find a way of adding Timer or something similar to my code
so that it could work. Could somebody modify an example to make IRC
bot send anything to chat every 20 seconds?

P.S. Timer should not lock the main program (I think it should work in
other thread or so)
P.P.S. Could somebody write a code of delay between messages too? In
pseudocode it looks like this:
sleep(20)
sendMessage(channel,'lopata')
This delay should be non-locking too.
P.P.P.S. Sorry for my bad English (and for a noob question too)
 
D

ddtm

You can use reactor.callLater to schedule a one-time event to happen at
some future point:

reactor.callLater(20, sendMessage, channel, 'lopata')

There is also a utility class, twisted.internet.task.LoopingCall, which
you can use to schedule an event to occur repeatedly at some interval:

call = LoopingCall(sendMessage, channel, 'lopata')
loopDeferred = call.start(20)

You can read more about these APIs in the scheduling howto:

http://twistedmatrix.com/projects/core/documentation/howto/time.html

Or you can refer to the generated API documentation:

http://twistedmatrix.com/documents/...m/documents/current/api/twisted.internet.task....

Hope this helps,

Jean-Paul

Thank you very much! It's a very useful information. One more
question: can I cancel the DelayedCall using its ID (it is returned
from callLater(...)) from another function? In example bot there are
two functions:
def joined(self, channel):
...
def privmsg(self, user, channel, msg):
...
For example, I add callLater(...) to joined(...) function and I'd like
to cancel this in privmsg(...) function. What should I do?
 
D

ddtm

Thank you very much! It's a very useful information. One more
question: can I cancel the DelayedCall using its ID (it is returned
from callLater(...)) from another function? In example bot there are
two functions:
def joined(self, channel):
...
def privmsg(self, user, channel, msg):
...
For example, I add callLater(...) to joined(...) function and I'd like
to cancel this in privmsg(...) function. What should I do?

Yep. The object callLater returns has a `cancel' method (some others, too)
which will prevent the function from being called at the scheduled time.

Jean-Paul

I know what you are talking about, but the question is: How can I
cancel scheduled task in function that differs from
function where I scheduled the task? Demo bot on Twisted website has a
class with a bunch of predefined functions joined(...), privmsg(...)
and so on. I can't see any method of communicating these functions.
I'm new to Python and to Twisted framework.
The task is:
to call callLater(...) in joined(...)
to cancel the task in privmsg(...) on special condition
 
D

ddtm

[snip]
Thank you very much! It's a very useful information. One more
question: can I cancel the DelayedCall using its ID (it is returned
from callLater(...)) from another function? In example bot there are
two functions:
def joined(self, channel):
...
def privmsg(self, user, channel, msg):
...
For example, I add callLater(...) to joined(...) function and I'd like
to cancel this in privmsg(...) function. What should I do?
Yep. The object callLater returns has a `cancel' method (some others, too)
which will prevent the function from being called at the scheduled time.
Jean-Paul
I know what you are talking about, but the question is: How can I
cancel scheduled task in function that differs from
function where I scheduled the task? Demo bot on Twisted website has a
class with a bunch of predefined functions joined(...), privmsg(...)
and so on. I can't see any method of communicating these functions.
I'm new to Python and to Twisted framework.
The task is:
to call callLater(...) in joined(...)
to cancel the task in privmsg(...) on special condition

You need to preserve a reference to the object. For example:

class IRCBot(Whatever):
def joined(...):
self.announceJoinedCall = reactor.callLater(...)

def privmsg(...):
self.announceJoinedCall.cancel()
self.announceJoinedCall = None

This skips over a bunch of details (what happens on multiple calls to
joined, what happens if privmsg is called after announceJoinedCall has
run, etc) but I hope it demonstrates the basic idea.

Jean-Paul

Thank you! Everything works great!
 
D

ddtm

I have another problem with my IRC bot. There is privmsg(self, user,
channel, msg) function (this function handles the incoming IRC data)
in the code that was mentioned above. I have a special condition in
this function that if a user sends to bot a private message (in other
words: if channel == self.nickname) bot sends it to the main channel.
Everything works fine except sending message to the main channel (for
example #www). I write something like this:
if channel == self.nickname:
self.msg('www', msg)
This code doesn't work. But if try to send private message back to
user:
if channel == self.nickname:
self.msg(user, msg)
everything works fine. I really don't know what to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top