Created
May 11, 2012 01:20
-
-
Save anroots/2656913 to your computer and use it in GitHub Desktop.
Have a conversation with a PandaBot AI
This file contains hidden or 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
#!/usr/bin/python -tt | |
# Have a conversation with a PandaBot AI | |
# Author A.Roots | |
import urllib, urllib2 | |
import sys | |
from xml.dom import minidom | |
from xml.sax.saxutils import unescape | |
def main(): | |
human_input = raw_input('You: ') | |
if human_input == 'exit': | |
sys.exit(0) | |
base_url = 'http://www.pandorabots.com/pandora/talk-xml' | |
data = urllib.urlencode([('botid', 'ebbf27804e3458c5'), ('input', human_input)]) | |
# Submit POST data and download response XML | |
req = urllib2.Request(base_url) | |
fd = urllib2.urlopen(req, data) | |
# Take Bot's response out of XML | |
xmlFile = fd.read() | |
dom = minidom.parseString(xmlFile) | |
objektid = dom.getElementsByTagName('that') | |
bot_response = objektid[0].toxml() | |
bot_response = bot_response[6:] | |
bot_response = bot_response[:-7] | |
# Some nasty unescaping | |
bot_response = unescape(bot_response, {"'": "'", """: '"'}) | |
print 'Getter:',str(bot_response) | |
# Repeat until terminated | |
while 1: | |
main() | |
if __name__ == '__main__': | |
print 'Hi. You can now talk to Getter. Type "exit" when done.' | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment