Thread: Interface with a ChatBot

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    Interface with a ChatBot

    Before you get on my butt, I know this site for c++ and chatbots are not really "bots", but I figure I give ppl something to use so they can stop asking for or telling on how to make a chatbot...

    This alows a way to interface with the cleverbot found over at Cleverbot.com - a clever bot - speak to an AI with some Actual Intelligence?

    The program was written in python that can be ran under c/c++ with a bit programming (Yes you'll have to program that half your self <evil face).

    cleverbot.py
    Code:
    """
    Example of how to use the bindings:
    
    >>> import cleverbot
    >>> cb=cleverbot.Session()
    >>> print cb.Ask("Hello there")
    'Hello.'
    
    """
    
    import urllib2
    import md5
    import re
    
    class ServerFullError(Exception):
    	pass
    
    ReplyFlagsRE = re.compile('<INPUT NAME=(.+?) TYPE=(.+?) VALUE="(.*?)">', re.IGNORECASE | re.MULTILINE)
    
    class Session:
    	keylist=['stimulus','start','sessionid','vText8','vText7','vText6','vText5','vText4','vText3','vText2','icognoid','icognocheck','prevref','emotionaloutput','emotionalhistory','asbotname','ttsvoice','typing','lineref','sub','islearning','cleanslate']
    	headers={}
    	headers['User-Agent']='Mozilla/5.0 (X11; U; Linux x86_64; it; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8'
    	headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    	headers['Accept-Language']='it-it,it;q=0.8,en-us;q=0.5,en;q=0.3'
    	headers['X-Moz']='prefetch'
    	headers['Accept-Charset']='ISO-8859-1,utf-8;q=0.7,*;q=0.7'
    	headers['Referer']='http://www.cleverbot.com'
    	headers['Cache-Control']='no-cache, no-cache'
    	headers['Pragma']='no-cache'
    
    	def __init__(self):
    		self.arglist=['','y','','','','','','','','','wsf','','','','','','','','','Say','1','false']
    		self.MsgList=[]
    
    	def Send(self):
    		data=encode(self.keylist,self.arglist)
    		digest_txt=data[9:29]
    		hash=md5.new(digest_txt).hexdigest()
    		self.arglist[self.keylist.index('icognocheck')]=hash
    		data=encode(self.keylist,self.arglist)
    		req=urllib2.Request("http://www.cleverbot.com/webservicefrm",data,self.headers)
    		f=urllib2.urlopen(req)
    		reply=f.read()
    		return reply
    
    	def Ask(self,q):
    		self.arglist[self.keylist.index('stimulus')]=q
    		if self.MsgList: self.arglist[self.keylist.index('lineref')]='!0'+str(len(self.MsgList)/2)
    		asw=self.Send()
    		if '<meta name="description" content="Jabberwacky server maintenance">' in asw: raise ServerFullError, "The Cleverbot server answered with full load error"
    		self.MsgList.append(q)
    		answ_dict=GetAnswerArgs(asw)
    		for k in self.keylist:
    			if k in answ_dict: self.arglist[self.keylist.index(k)]=answ_dict[k]
    		self.arglist[self.keylist.index('emotionaloutput')]=''
    		reply_i=asw.find('<!-- Begin Response !-->')+25
    		reply_s=asw.find('<!-- End Response !-->')-1
    		text=asw[reply_i:reply_s]
    		self.MsgList.append(text)
    		return text
    
    def GetAnswerArgs(text):
    	results=ReplyFlagsRE.findall(text)
    	list={}
    	for r in results: list[r[0]]=r[2]
    	return list
    
    def encode(keylist,arglist):
    	text=''
    	for i in range(len(keylist)):
    		k=keylist[i]; v=quote(arglist[i])
    		text+='&'+k+'='+v
    	text=text[1:]
    	return text
    
    always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                   'abcdefghijklmnopqrstuvwxyz'
                   '0123456789' '_.-')
    def quote(s, safe = '/'):   #quote('abc def') -> 'abc%20def'
    	safe += always_safe
    	safe_map = {}
    	for i in range(256):
    		c = chr(i)
    		safe_map[c] = (c in safe) and c or  ('%%%02X' % i)
    	res = map(safe_map.__getitem__, s)
    	return ''.join(res)
    I am surpized there isn't really that many topics under AI... This topic always seem to be a high pop when I was on the forum back in 2005... I guess people haven't full understand that AI can also fall into automatting things base on someones needs and wants also...

    For example:
    When I get home... I want the lights on... a simple thing idea would be to wear a object that I wouldn't want to take off such as a color band...

    The house would be setup with a IR looking for such color band/wave. When the color is found it would auto turn on the lights on at the house and also say, "hello Adr ^^ welcome home!"

    code wise... (no this wont work)
    Code:
    import IR
    import Lights
    from TaskMgr import *
    
    IR.SetColor = "Green"
    
    taskMgr.doMethodLater(1, Check_For_Color, "Check_For_Color_Task")
    
    def Check_For_Color():
        if IR.GetColor:
            Lights.Turn_On()
        if Lights.Is_On()
            Lights.Turn_Off()
    
    run()
    Simple program with bugs and logic behind it... but I figure I spit something else out there in hopes this topic grows again ^^

    Anyways, I hope this works fine... It's my way of giving back for being away for 6+ years o.o; ... I will be perty much relearning c++ again xD
    Last edited by adr; 12-07-2011 at 03:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple chatbot problems
    By Whyrusleeping in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2011, 01:04 PM
  2. C++ Interface
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 11:09 PM
  3. interface for my app
    By JordanCason in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2007, 03:02 PM
  4. What would you like in a interface?
    By commanderrulz in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-29-2002, 05:29 AM
  5. PCI ISA bus Interface
    By Mohsinzb in forum C Programming
    Replies: 0
    Last Post: 03-27-2002, 01:12 PM