Source

twisted-meetup / ircbot.py

Full commit
#!/usr/bin/env python

from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.python import log

HOST, PORT = 'bpm.tm.tl', 6667

class IRCHelloBot(irc.IRCClient):
    nickname = 'olivo'
    password = '01iv0'

    def signedOn(self):
        log.msg("Signed on")
        for channel in self.factory.channels:
            log.msg("Joining %s" % (channel,))
            self.join(channel)

    def joined(self, channel):
        log.msg("Joined %s" % (channel,))
        self.msg(channel, "Hello, %s" % (channel,))

    def command_ping(self, rest):
        return 'Pong.'


class IRCHelloBotFactory(protocol.ReconnectingClientFactory):
    protocol = IRCHelloBot
    channels = ['#testing']

def main():
    import sys
    from twisted.internet import reactor
    reactor.connectTCP(HOST, PORT, IRCHelloBotFactory())
    log.startLogging(sys.stdout)
    reactor.run()

def get_application():
    from twisted.application import service, internet

    application = service.Application('IRCHelloBot')
    ircc = internet.TCPClient(HOST, PORT, IRCHelloBotFactory())
    ircc.setServiceParent(application)
    return application

application = get_application()

if __name__ == '__main__':
    main()