Now you know how to say hello in english

I have a love/hate rela­tion­ship against the hello mes­sages shown in all the world lan­guages on flickr. That’s why this morn­ing I was talk­ing with a friend on IRC about this and a bad idea jumped in my mind: do an IRC bot that says hello in many lan­guages when some­one joins a channel.

It’s from sev­eral years that I don’t do any­thing IRC related, but this time I had two spe­cial weapons in my back­pack: python and twisted. The final bot is ~90 lines of code, half of the which are for the hello list and the entire coding process took less than 20 minutes.

Here fol­lows the bot source code; note that this is in ital­ian but could be easily trans­lated in your lan­guage:

#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

from twisted.words.protocols import irc
from twisted.internet import reactor, protocol

import random

class HelloBot(irc.IRCClient):
    nickname = "Hola"
    channel = "channel"

    ciao = (
            ('arabo', 'hala'),
            ('bulgaro', 'zdrasti'),
            ('ceco', 'ahoj'),
            ('chamorro', 'hafa adai'),
            ('chichewa', 'moni bambo'),
            ('coreano', 'ahn nyeong'),
            ('croato', 'boke'),
            ('ensperanto', 'saluton'),
            ('francese', 'salut'),
            ('georgiano', 'gamardjoba'),
            ('greco', 'yia sou'),
            ('hawaiano', 'aloha'),
            ('inglese', 'hello'),
            ('irlandese', 'fáilte'),
            ('italiano', 'ciao'),
            ('klingon', 'nuqneH'),
            ('lussemburghese', 'moïen'),
            ('maori', 'kia ora'),
            ('navajo', 'ya\'at\'eeh'),
            ('nepalese', 'namaste'),
            ('polacco', 'czesc'),
            ('portoghese', 'olá'),
            ('rumeno', 'bun? ziua'),
            ('russo', 'pree-vyet'),
            ('serbo', 'dobar dan'),
            ('sloveno', 'živjo'),
            ('spagnolo', 'holà'),
            ('svedese', 'hej'),
            ('taitiano', 'ia orana'),
            ('tedesco', 'hallo'),
            ('turco', 'merhaba'),
            ('ucraino', 'pryvit'),
            ('vietnamese', 'xin chào'),
            ('zulu', 'sawubona'),
    )

    def connectionMade(self):
        irc.IRCClient.connectionMade(self)

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)

    def signedOn(self):
        self.join(self.channel)

    def privmsg(self, user, channel, msg):
        user = user.split('!', 1)[0]
        # private message?
        if channel == self.nickname:
            self.msg(user, "I can't")
            return

    def irc_JOIN(self, user, channel):
        user = user.split('!')[0]
        channel = channel[0]

        x = random.randint(0, len(self.ciao) - 1)
        if user == self.nickname:
            msg = "%s! (ora sapete come dire ciao in %s)" % (self.ciao[x][1], self.ciao[x][0])
        else:
            msg = "%s %s! (ora sai come dire ciao in %s)" % (self.ciao[x][1], user, self.ciao[x][0])

        self.msg(channel, msg)

class StartBot(protocol.ClientFactory):
    protocol = HelloBot

    def clientConnectionLost(self, connector, reason):
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed: ", reason
        reactor.stop()

if __name__ == '__main__':
    random.seed()
    f = StartBot()
    reactor.connectTCP("irc.server.com", 6667, f)
    reactor.run()

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">