Browse code

allows using specific language with Cleverbot

Pierre-David BeĢlanger authored on 26/04/2016 00:37:24
Showing 1 changed files
... ...
@@ -55,7 +55,7 @@
55 55
     
56 56
     abstract class ChatterBot
57 57
     {
58
-        public function createSession()
58
+        public function createSession($lang = 'en')
59 59
         {
60 60
             return null;
61 61
         }
... ...
@@ -128,21 +128,24 @@
128 128
             $this->endIndex = $endIndex;
129 129
         }        
130 130
 
131
-        public function createSession()
131
+        public function createSession($lang = 'en')
132 132
         {
133
-            return new _CleverbotSession($this);
133
+            return new _CleverbotSession($this, $lang);
134 134
         }
135 135
     }
136 136
     
137 137
     class _CleverbotSession extends ChatterBotSession
138 138
     {
139 139
         private $bot;
140
+        private $headers;
140 141
         private $cookies;
141 142
         private $vars;
142 143
 
143
-        public function __construct($bot)
144
+        public function __construct($bot, $lang)
144 145
         {
145 146
             $this->bot = $bot;
147
+            $this->headers = array();
148
+            $this->headers['Accept-Language'] = "$lang;q=1.0";
146 149
             $this->vars = array();
147 150
             $this->vars['start'] = 'y';
148 151
             $this->vars['icognoid'] = 'wsf';
... ...
@@ -151,7 +154,7 @@
151 154
             $this->vars['islearning'] = '1';
152 155
             $this->vars['cleanslate'] = 'false';
153 156
             $this->cookies = array();
154
-            _utils_request($this->bot->getBaseUrl(), $this->cookies, null);
157
+            _utils_request($this->bot->getBaseUrl(), $this->cookies, null, $this->headers);
155 158
         }
156 159
 
157 160
         public function thinkThought($thought)
... ...
@@ -161,7 +164,7 @@
161 164
             $dataToDigest = substr($data, 9, $this->bot->getEndIndex());
162 165
             $dataDigest = md5($dataToDigest);
163 166
             $this->vars['icognocheck'] = $dataDigest;
164
-            $response = _utils_request($this->bot->getServiceUrl(), $this->cookies, $this->vars);
167
+            $response = _utils_request($this->bot->getServiceUrl(), $this->cookies, $this->vars, $this->headers);
165 168
             $responseValues = explode("\r", $response);
166 169
             //self.vars['??'] = _utils_string_at_index($responseValues, 0);
167 170
             $this->vars['sessionid'] = _utils_string_at_index($responseValues, 1);
... ...
@@ -230,7 +233,7 @@
230 233
             $this->botid = $botid;
231 234
         }        
232 235
         
233
-        public function createSession()
236
+        public function createSession($lang = 'en')
234 237
         {
235 238
             return new _PandorabotsSession($this);
236 239
         }
... ...
@@ -271,7 +274,7 @@
271 274
     # Utils
272 275
     #################################################
273 276
 
274
-    function _utils_request($url, &$cookies, $params)
277
+    function _utils_request($url, &$cookies, $params, $headers = null)
275 278
     {
276 279
         $contextParams = array();
277 280
         $contextParams['http'] = array();
... ...
@@ -302,6 +305,20 @@
302 305
                 $contextParams['http']['header'] = $cookieHeader;
303 306
             }
304 307
         }
308
+        if (!is_null($headers))
309
+        {
310
+            foreach ($headers as $headerName => $headerValue)
311
+            {
312
+                if (isset($contextParams['http']['header']))
313
+                {
314
+                    $contextParams['http']['header'] .= "$headerName: $headerValue\r\n";
315
+                }
316
+                else
317
+                {
318
+                    $contextParams['http']['header'] = "$headerName: $headerValue\r\n";
319
+                }
320
+            }
321
+        }
305 322
         $context = stream_context_create($contextParams);
306 323
         $fp = fopen($url, 'rb', false, $context);
307 324
         $response = stream_get_contents($fp);