Last active
December 30, 2015 09:09
-
-
Save cshirky/7807720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from socketIO_client import SocketIO | |
import re, random | |
### The basics: | |
# Messages FROM meatspace are a nested dict in the form | |
# {u'chat': | |
# {u'value': | |
# {u'media': | |
# u'data:image/gif;base64,<wow so base64 such text-string>', | |
# u'message': '<witicism here>', | |
# u'ttl': 600000, | |
# u'created': 1385499795344, | |
# u'fingerprint': u'93d944673197120b6d611d3014d81949' | |
# }, | |
# u'key': u'1385499795344!f72be0da-83d7-4bff-8c6e-562928ae6162' | |
# } | |
# } | |
# Messages TO meatspace are a dict in the form | |
# {'apiKey': APIKEY, # From Edna | |
# 'message': REPLY, # String | |
# 'picture': B64_HEADER+B64_DATA, # "data:image/gif;base64,"+ b64 string | |
# 'fingerprint': FINGERPRINT # Generate your own unique fingerprint; MD5 hash the filename or whatevs | |
# } | |
# Variables | |
FINGERPRINT = "" | |
B64_HEADER = "data:image/gif;base64," | |
LIVE = True | |
# Match strings for the inbound messages | |
# Bots | |
M8 = "Magic ?M(eat|8)ball" | |
FROGE = "Froge of Procrastinat" | |
# Easter eggs | |
FOX = "What does the foxe? say" | |
AG = "Animated Gif" | |
ERROR = "Error Message" | |
# API Key, Fingerprints, b64 data, and pre-fab messages are all stored in the file system | |
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 | |
# Switch servers | |
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") | |
# Let the wild rumpus begin... | |
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() | |
# Listen to meatspace | |
def on_message(self, *args): | |
data = args[0] | |
message = data['chat']['value']['message'] | |
key = data['chat']['key'] | |
# Reply | |
def send_message(): | |
SocketIO(ADDRESS).emit('message', { | |
'apiKey': APIKEY, | |
'message': REPLY, | |
'picture': B64_HEADER+B64_DATA, | |
'fingerprint': FINGERPRINT }) | |
# Each 'if' statement is a string match | |
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+"\n") | |
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() | |
elif re.search(ERROR, message, re.IGNORECASE): | |
B64_DATA = line_from_file("oops.gif.b64", "string") | |
FINGERPRINT = line_from_file("error-fingerprint.txt", "first") | |
REPLY = line_from_file("error-haiku.txt", "random") | |
print "ERROR:", message, "/", REPLY | |
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
Prettified