Python Twisted IRC Bot: Lazy Coder
Photo by Colin Watts on Unsplash
IRC Bot that repsonds to the word "why" with lazy coder responses
A friend pointed me to this simple yet humorous website yesterday which essentially gives a new lazy coder excuse whenever the page is refreshed.
I couldn't help but whip out a bot to plug in to our IRC channel. My lazy coder bot will give a random excuse whenever someone mentions the word "why".
I used my Rollbot script as a base to write this up quickly.
requirements.txt
1Twisted==13.1.02beautifulsoup4==4.2.13requests==1.2.3
because.py
1from bs4 import BeautifulSoup2import requests3from twisted.words.protocols import irc4from twisted.internet import protocol, reactor56NICK = '_lazy_coder_'7CHANNEL = '#yourchannel'8PASSWORD = 'channel_password'910class MyBot(irc.IRCClient):11 def _get_nickname(self):12 return self.factory.nickname13 nickname = property(_get_nickname)1415 def signedOn(self):16 self.join(self.factory.channel)17 print "Signed on as {}.".format(self.nickname)1819 def joined(self, channel):20 print "Joined %s." % channel2122 def privmsg(self, user, channel, msg):23 """24 Whenever someone says "why" give a lazy programmer response25 """26 if 'why' in msg.lower():27 # get lazy response28 because = self._get_because()2930 # post message31 self.msg(CHANNEL, because)3233 def _get_because(self):34 req = requests.get('http://developerexcuses.com/')35 soup = BeautifulSoup(req.text)36 elem = soup.find('a')37 return elem.text.encode('ascii', 'ignore')3839class MyBotFactory(protocol.ClientFactory):40 protocol = MyBot4142 def __init__(self, channel, nickname=NICK):43 self.channel = channel44 self.nickname = nickname4546 def clientConnectionLost(self, connector, reason):47 print "Lost connection (%s), reconnecting." % reason48 connector.connect()4950 def clientConnectionFailed(self, connector, reason):51 print "Could not connect: %s" % reason5253if __name__ == "__main__":54 channel = CHANNEL55 if PASSWORD:56 channel += ' {}'.format(PASSWORD)57 reactor.connectTCP('irc.freenode.net', 6667, MyBotFactory(channel))58 reactor.run()
UPDATE:
I've made some minor modifications and posted the project on Github