1- from cloudbot .util import cleverbot
1+ """
2+ chatbot.py
3+
4+ Ask Cleverbot something via CloudBot! This one is way shorter!
5+
6+ Created By:
7+ - Foxlet <http://furcode.tk/>
8+
9+ License:
10+ GNU General Public License (Version 3)
11+ """
12+
213from cloudbot import hook
14+ import requests
15+ import urllib .parse
16+ import hashlib
17+ import collections
18+ import html
19+
20+ SESSION = collections .OrderedDict ()
21+ API_URL = "http://www.cleverbot.com/webservicemin/"
322
4- import urllib
23+ @hook .onload ()
24+ def init_vars ():
25+ SESSION ['stimulus' ] = ""
26+ SESSION ['sessionid' ] = ""
27+ SESSION ['start' ] = 'y'
28+ SESSION ['icognoid' ] = 'wsf'
29+ SESSION ['fno' ] = '0'
30+ SESSION ['sub' ] = 'Say'
31+ SESSION ['islearning' ] = '1'
32+ SESSION ['cleanslate' ] = 'false'
533
34+ def cb_think (text ):
35+ SESSION ['stimulus' ] = text
36+ payload = urllib .parse .urlencode (SESSION )
37+ digest = hashlib .md5 (payload [9 :35 ].encode ('utf-8' )).hexdigest ()
38+ target_url = "{}&icognocheck={}" .format (payload , digest )
39+ print (target_url )
40+ parsed = requests .post (API_URL , data = target_url )
41+ data = parsed .text .split ('\r ' )
42+ SESSION ['sessionid' ] = data [1 ]
43+ return html .unescape (data [0 ])
644
745@hook .command ("ask" , "cleverbot" , "cb" )
846def ask (text ):
947 """ <question> -- Asks Cleverbot <question> """
10- session = cleverbot .Session ()
11- attempt = 0
12-
13- try :
14- answer = session .ask (text )
15- while answer .startswith ("\n " ) and attempt < 3 :
16- # Cleverbot tried to advert us
17- answer = session .ask (text )
18- attempt += 1
19- except urllib .error .HTTPError :
20- return "Could not get response. Cleverbot is angry :("
21-
22- return answer
48+ return cb_think (text )
0 commit comments