I have a love/hate relationship against the hello messages shown in all the world languages on flickr. That’s why this morning I was talking with a friend on IRC about this and a bad idea jumped in my mind: do an IRC bot that says hello in many languages when someone joins a channel.
It’s from several years that I don’t do anything IRC related, but this time I had two special weapons in my backpack: 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 follows the bot source code; note that this is in italian but could be easily translated in your language:
#!/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()
0 Comments.