Permalink
Browse files

finally merged IRC branch

  • Loading branch information...
2 parents 7a075d4 + 0454e2a commit 7c405f78bee56b1b72f85cff69be4065d4d269e6 @econchick committed Mar 19, 2013
View
@@ -8,4 +8,4 @@ Permission is granted to anyone to use this software for any purpose, including
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-3. This notice may not be removed or altered from any source distribution.
+3. This notice may not be removed or altered from any source distribution.
View
@@ -0,0 +1,47 @@
+TalkBackBot
+================================
+
+Are you tired of "That's what she said" jokes? Then this bot is for you!
+It will join a specified channel and respond to the configured trigger phrases
+with what she really said, i.e. a quotation from a notable woman. It will also
+respond to any direct message with a quotation.
+
+Many quotes taken from this excellent resource:
+http://womenshistory.about.com/library/qu/blqulist.htm
+
+Setup
+------------
+
+I highly recommend both virtualenv and virtualenvwrapper to manage the
+environments for your different python projects.
+
+::
+
+ # Create a virtualenv
+ mkvirtualenv talkbackbot
+
+ # Install requirements
+ workon talkbackbot
+ pip install -r requirements.txt
+
+Usage
+-----
+
+::
+
+ # Activate your virtualenv
+ workon talkbackbot
+
+ # Copy settings.py.EXAMPLE to settings.py and edit to suit yourself
+ cp settings.py.EXAMPLE settings.py
+ vim settings.py
+
+ # Run the bot
+ twistd twsrs
+
+ # Stop the bot
+ kill `cat twistd.pid`
+
+ # Run unit tests
+ nosetests
+
@@ -0,0 +1,5 @@
+coverage
+Mock
+nose
+pyopenssl
+twisted
@@ -0,0 +1,17 @@
+# IRC settings
+HOST = "irc.freenode.net"
+PORT = 6667
+USE_SSL = False
+NICKNAME = "whatshereallysaid"
+REALNAME = "bot: provides quotations from notable women"
+
+CHANNEL = "#channel_name"
+
+# Trigger phrases, in lowercase
+TRIGGERS = (
+ "that's what she said",
+ )
+
+# Process settings
+LOG_FILE = "./talkbackbot.log"
+QUOTES_FILE = "talkback/quotes.txt"
No changes.
@@ -0,0 +1,72 @@
+import logging
+
+from twisted.words.protocols import irc
+from twisted.internet import reactor, protocol
+
+from quotation_selector import QuotationSelector
+
+
+class TalkBackBot(irc.IRCClient):
+
+ def connectionMade(self):
+ irc.IRCClient.connectionMade(self)
+ logging.info("connectionMade")
+
+ def connectionLost(self, reason):
+ irc.IRCClient.connectionLost(self, reason)
+ logging.info("connectionLost")
+
+ # callbacks for events
+
+ def signedOn(self):
+ """Called when bot has successfully signed on to server."""
+ logging.info("Signed on")
+ self.join(self.factory.channel)
+
+ def joined(self, channel):
+ """This will get called when the bot joins the channel."""
+ logging.info("[%s has joined %s]"
+ % (self.nickname, self.factory.channel))
+
+ def privmsg(self, user, channel, msg):
+ """This will get called when the bot receives a message."""
+
+ trigger_found = False
+ send_to = channel
+ if self.factory.settings.NICKNAME.startswith(channel) or \
+ channel.startswith(self.factory.settings.NICKNAME):
+ trigger_found = True
+ send_to = user.split('!')[0]
+ else:
+ for trigger in self.factory.settings.TRIGGERS:
+ if msg.lower().find(trigger) >= 0:
+ trigger_found = True
+ break
+
+ if trigger_found:
+ quote = self.factory.quotation.select()
+ self.msg(send_to, quote)
+ logging.info("sent message to %s:\n\t%s" % (send_to, quote))
+
+
+class TalkBackBotFactory(protocol.ClientFactory):
+
+ def __init__(self, settings):
+ self.settings = settings
+ self.channel = self.settings.CHANNEL
+ self.quotation = QuotationSelector(self.settings.QUOTES_FILE)
+
+ def buildProtocol(self, addr):
+ bot = TalkBackBot()
+ bot.factory = self
+ bot.nickname = self.settings.NICKNAME
+ bot.realname = self.settings.REALNAME
+ return bot
+
+ def clientConnectionLost(self, connector, reason):
+ logging.info("connection lost, reconnecting")
+ connector.connect()
+
+ def clientConnectionFailed(self, connector, reason):
+ logging.info("connection failed: %s" % (reason))
+ reactor.stop()
@@ -0,0 +1,11 @@
+from random import choice
+
+
+class QuotationSelector(object):
+
+ def __init__(self, quotes_filename):
+ with open(quotes_filename) as quotes_file:
+ self.quotes = quotes_file.readlines()
+
+ def select(self):
+ return choice(self.quotes).strip()
Oops, something went wrong.

0 comments on commit 7c405f7

Please sign in to comment.