Python IRC bot using Twisted

ddtm dr.death.tm at gmail.com
Tue Jul 3 15:44:34 CEST 2007


On 3    , 16:01, Jean-Paul Calderone <exar... at divmod.com> wrote:
> On Tue, 03 Jul 2007 09:46:59 -0000, ddtm <dr.death... at gmail.com> wrote:
> >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)
>
> 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/current/api/twisted.internet.inter...http://twistedmatrix.com/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?




More information about the Python-list mailing list