Skip to content

Instantly share code, notes, and snippets.

@cshirky
Created December 13, 2013 19:58
Show Gist options
  • Save cshirky/7950325 to your computer and use it in GitHub Desktop.
Save cshirky/7950325 to your computer and use it in GitHub Desktop.
from socketIO_client import SocketIO
import re, random
LIVE = False
M8 = "Magic ?M(eat|8)ball"
FROGE = "Froge of Procrastinat"
FOX = "What does the fox say"
AG = "Animated Gif"
def line_from_file(filename, query_type = "random"):
data = [ line.rstrip() for line in open(filename) if not line.isspace() ]
query_type = query_type.lower()
if (query_type == "all"):
return data # Return file as an array
elif (query_type == "string"):
return "".join(data) # Return file as a string
elif (query_type == "first"):
return data[0] # Return first line
elif (query_type == "random" or query_type == "rand"):
return random.choice(data) # Return random line
if (LIVE):
ADDRESS = 'https://chat.meatspac.es'
APIKEY = line_from_file("chat-meatspace-api", "first")
else:
ADDRESS = 'http://chat-staging.meatspac.es'
APIKEY = line_from_file("chat-staging-meatspace-api", "first")
FINGERPRINT = ""
B64_HEADER = "data:image/gif;base64,"
class Bot(object):
def __init__(self):
print "Listening to %s" % ADDRESS
with SocketIO(ADDRESS) as socketIO_listen:
socketIO_listen.on('message', self.on_message)
socketIO_listen.wait()
def on_message(self, *args):
data = args[0]
message = data['chat']['value']['message']
key = data['chat']['key']
def send_message():
SocketIO(ADDRESS).emit('message', {
'apiKey': APIKEY,
'message': REPLY,
'picture': B64_HEADER+B64_DATA,
'fingerprint': FINGERPRINT })
if re.search(M8, message, re.IGNORECASE):
REPLY = line_from_file("meatball-answers.txt", "random")
FINGERPRINT = line_from_file("meatball-fingerprint.txt", "first")
B64_DATA = line_from_file("meatball.b64", "string")
print "M8:", message, "/", REPLY
send_message()
elif re.search(FROGE, message, re.IGNORECASE):
B64_DATA = line_from_file("frog.gif.b64", "string")
FINGERPRINT = line_from_file("froge-fingerprint.txt", "first")
if re.search("remember", message, re.IGNORECASE):
message = re.sub('.*remember\s*', "", message, re.IGNORECASE)
froge_file = open("froge-data.txt", 'a+')
froge_file.write(message)
REPLY = "The Froge hears and obeys."
else:
REPLY = line_from_file("froge-headers.txt", "random")+" "+line_from_file("froge-data.txt", "random")
print "FROGE:", message, "/", REPLY
send_message()
elif re.search(FOX, message, re.IGNORECASE):
B64_DATA = line_from_file("fox.gif.b64", "string")
FINGERPRINT = line_from_file("fox-fingerprint.txt", "first")
REPLY = "The fox says \""+line_from_file("quotes.txt", "random")+"\""
print "FOX:", message, "/", REPLY
send_message()
elif re.search(AG, message, re.IGNORECASE):
img_name, B64_DATA = line_from_file("img_archive.b64", "random").split(":")
FINGERPRINT = line_from_file("ag-fingerprint.txt", "first")
REPLY = ""
print "AG:", message, "/", REPLY, img_name
send_message()
if __name__ == '__main__':
bot = Bot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment