vocalinfo.plugin.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. /*
  3. @name Informations vocales
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @description Permet la récuperations d'informations locales ou sur le web comme la météo, les séries TV, l'heure, la date et l'état des GPIO
  9. */
  10. define('VOCALINFO_COMMAND_FILE','cmd.json');
  11. function vocalinfo_vocal_command(&$response,$actionUrl){
  12. global $conf;
  13. $commands = json_decode(file_get_contents(__ROOT__.'/'.Plugin::path().'/'.VOCALINFO_COMMAND_FILE),true);
  14. foreach($commands as $key=>$command){
  15. if($command['disabled']=='true') continue;
  16. $response['commands'][] = array(
  17. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' '.$command['command'],
  18. 'url'=>$actionUrl.$command['url'],
  19. 'confidence'=>($command['confidence']+$conf->get('VOCAL_SENSITIVITY'))
  20. );
  21. }
  22. $response['commands'][] = array(
  23. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' imite le bruit de la poule',
  24. 'callback'=>'vocalinfo_chicken',
  25. 'parameters' => array('un','deux'),
  26. 'confidence'=>0.8);
  27. $response['commands'][] = array(
  28. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' Définit le mot',
  29. 'callback'=>'vocalinfo_define_word',
  30. 'confidence'=>0.8);
  31. $response['commands'][] = array(
  32. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' présente toi',
  33. 'callback'=>'vocalinfo_give_me_all',
  34. 'confidence'=>0.8);
  35. $response['commands'][] = array(
  36. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' enerve toi',
  37. 'callback'=>'vocalinfo_emotion_angry',
  38. 'confidence'=>0.8);
  39. $response['commands'][] = array(
  40. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' montre toi',
  41. 'callback'=>'vocalinfo_show_you',
  42. 'confidence'=>0.8);
  43. $response['commands'][] = array(
  44. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' lance le programme',
  45. 'callback'=>'vocalinfo_launch_program',
  46. 'confidence'=>0.8);
  47. $response['commands'][] = array(
  48. 'command'=>$conf->get('VOCAL_ENTITY_NAME').' test des variables',
  49. 'callback'=>'vocalinfo_test_variables',
  50. 'confidence'=>0.8);
  51. }
  52. function vocalinfo_test_variables($text,$confidence,$parameters,$myUser){
  53. global $conf;
  54. $cli = new Client();
  55. $cli->connect();
  56. $cli->talk("Utilisateur: ".$myUser->getLogin());
  57. $cli->talk("configuration: ".$conf->get('UPDATE_URL'));
  58. $cli->disconnect();
  59. }
  60. function vocalinfo_define_word($text,$confidence,$parameters){
  61. $cli = new Client();
  62. $cli->connect();
  63. if($text=='bistro'){
  64. $cli->talk("Un bistro est un lieu de cultes, ou les sages de ce siècle vont se recueillir");
  65. }else{
  66. $json = json_decode(file_get_contents('https://fr.wikipedia.org/w/api.php?action=opensearch&search='.$text),true);
  67. $define = $json[2][0];
  68. if($json==false || trim($define)==""){$cli->talk("Le mot ".$text." ne fait pas partie de mot vocabulaire, essayez plutot avec le mot bistro");
  69. $cli->disconnect();
  70. return;
  71. }
  72. $cli->talk($define);
  73. }
  74. $cli->disconnect();
  75. //Client::execute("D:\Programme_installes\Qt\Tools\QtCreator\bin\qtcreator.exe");
  76. }
  77. function vocalinfo_show_you($text,$confidence,$parameters){
  78. $cli = new Client();
  79. $cli->connect();
  80. $cli->image(YANA_URL."/plugins/vocal_infos/img/yana.jpg");
  81. $cli->talk("Est ce que tu me trouve jolie?");
  82. $cli->disconnect();
  83. }
  84. function vocalinfo_emotion_angry($text,$confidence,$parameters){
  85. $cli = new Client();
  86. $cli->connect();
  87. $cli->talk("Tu vois ce qui se passe quand tu me prend la tête ?");
  88. $cli->emotion("angry");
  89. $cli->disconnect();
  90. }
  91. function vocalinfo_chicken($text,$confidence,$parameters){
  92. $cli = new Client();
  93. $cli->connect();
  94. $cli->sound("C:/poule.wav");
  95. $cli->disconnect();
  96. }
  97. function vocalinfo_launch_program($text,$confidence,$parameters){
  98. $cli = new Client();
  99. $cli->connect();
  100. switch($text){
  101. case 'sublime texte':
  102. $cli->execute("C:\Program Files\Sublime Text 2\sublime_text.exe");
  103. $cli->talk("Programme en cours de lancement");
  104. break;
  105. default:
  106. $cli->talk("Je ne connais pas le programme : ".$text);
  107. break;
  108. }
  109. $cli->disconnect();
  110. }
  111. function vocalinfo_give_me_all($text,$confidence,$parameters){
  112. $cli = new Client();
  113. $cli->connect();
  114. $cli->talk("Je peux parler, evidemment, et t\'écouter plus précisément qu\'avant");
  115. $cli->talk("Je peux eprouver et montrer des sentiments");
  116. $cli->talk("Comme la colère");
  117. $cli->emotion("angry");
  118. $cli->talk("Ou la timidité");
  119. $cli->emotion("shy");
  120. $cli->talk("Et tout un tas d\'autres lubies humaines");
  121. $cli->talk("Je peux aussi exécuter un programme");
  122. $cli->execute("D:\Programme_installes\Qt\Tools\QtCreator\bin\qtcreator.exe");
  123. $cli->talk("ou un son");
  124. $cli->sound("C:/poule.wav");
  125. $cli->talk("ou te montrer des images");
  126. $cli->image("yana.jpg");
  127. $cli->talk("ou executer une commande domotique");
  128. //system('gpio write 1 1');
  129. //$cli->talk("ou executer un humain");
  130. //$cli->talk("non je déconne.");
  131. $cli->disconnect();
  132. }
  133. function vocalinfo_action(){
  134. global $_,$conf;
  135. switch($_['action']){
  136. case 'plugin_vocalinfo_save':
  137. $commands = json_decode(file_get_contents(Plugin::path().'/'.VOCALINFO_COMMAND_FILE),true);
  138. foreach($_['config'] as $key=>$config){
  139. $commands[$key]['confidence'] = $config['confidence'];
  140. $commands[$key]['disabled'] = $config['disabled'];
  141. }
  142. file_put_contents(Plugin::path().'/'.VOCALINFO_COMMAND_FILE,json_encode($commands));
  143. echo 'Enregistré';
  144. break;
  145. case 'vocalinfo_plugin_setting':
  146. $conf->put('plugin_vocalinfo_place',$_['weather_place']);
  147. $conf->put('plugin_vocalinfo_woeid',$_['woeid']);
  148. header('location:setting.php?section=preference&block=vocalinfo');
  149. break;
  150. case 'vocalinfo_sound':
  151. global $_;
  152. $response = array('responses'=>array(
  153. array('type'=>'sound','file'=>$_['sound'])
  154. )
  155. );
  156. $json = json_encode($response);
  157. echo ($json=='[]'?'{}':$json);
  158. break;
  159. case 'vocalinfo_devmod':
  160. $response = array('responses'=>array(
  161. array('type'=>'command','program'=>'C:\Program Files\Sublime Text 2\sublime_text.exe'),
  162. array('type'=>'talk','sentence'=>'Sublim text lancé.')
  163. )
  164. );
  165. $json = json_encode($response);
  166. echo ($json=='[]'?'{}':$json);
  167. break;
  168. case 'vocalinfo_gpio_diag':
  169. $sentence = '';
  170. $gpio = array('actif'=>array(),'inactif'=>array());
  171. for ($i=0;$i<26;$i++) {
  172. $commands = array();
  173. exec("/usr/local/bin/gpio read ".$i,$commands,$return);
  174. if(trim($commands[0])=="1"){
  175. $gpio['actif'][] = $i;
  176. }else{
  177. $gpio['inactif'][] = $i;
  178. }
  179. }
  180. if(count($gpio['actif'])==0){
  181. $sentence .= 'Tous les GPIO sont inactifs.';
  182. }else if(count($gpio['inactif'])==0){
  183. $sentence .= 'Tous les GPIO sont actifs.';
  184. }else{
  185. $sentence .= 'GPIO actifs: '.implode(', ', $gpio['actif']).'. GPIO inactifs: '.implode(', ', $gpio['inactif']).'.';
  186. }
  187. $response = array('responses'=>array(
  188. array('type'=>'talk','sentence'=>$sentence)
  189. )
  190. );
  191. $json = json_encode($response);
  192. echo ($json=='[]'?'{}':$json);
  193. break;
  194. case 'vocalinfo_commands':
  195. $actionUrl = 'http://'.$_SERVER['SERVER_ADDR'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
  196. $actionUrl = substr($actionUrl,0,strpos($actionUrl , '?'));
  197. $commands = array();
  198. Plugin::callHook("vocal_command", array(&$commands,$actionUrl));
  199. $sentence ='Je répond aux commandes suivantes: ';
  200. foreach ($commands['commands'] as $command) {
  201. $sentence .=$command['command'].'. ';
  202. }
  203. $response = array('responses'=>array(
  204. array('type'=>'talk','sentence'=>$sentence)
  205. )
  206. );
  207. $json = json_encode($response);
  208. echo ($json=='[]'?'{}':$json);
  209. break;
  210. case 'vocalinfo_meteo':
  211. global $_;
  212. if($conf->get('plugin_vocalinfo_woeid')!=''){
  213. $contents = file_get_contents('http://weather.yahooapis.com/forecastrss?w='.$conf->get('plugin_vocalinfo_woeid').'&u=c');
  214. $xml = simplexml_load_string($contents);
  215. if( (isset($_['today'])))
  216. {
  217. $weekdays = $xml->xpath('/rss/channel/item/yweather:condition');
  218. }
  219. else
  220. {
  221. $weekdays = $xml->xpath('/rss/channel/item/yweather:forecast');
  222. }
  223. //Codes disponibles ici: http://developer.yahoo.com/weather/#codes
  224. $textTranslate = array(
  225. 'Showers'=>'des averses',
  226. 'Tornado' => 'Attention: Tornade!',
  227. 'Hurricane' => 'Attention: Ouragan!',
  228. 'Severe thunderstorms' => 'Orages violents',
  229. 'Mixed rain and snow' => 'Pluie et neiges',
  230. 'Mixed rain and sleet' => 'Pluie et neige fondue',
  231. 'Mixed snow and sleet' => 'Neige et neige fondue',
  232. 'Freezing drizzle' => 'Bruine verglassant',
  233. 'Drizzle' => 'Bruine',
  234. 'Freezing rain' => 'Pluie verglassant',
  235. 'Showers' => 'Averse',
  236. 'Snow flurries' => 'Bourrasque de neige',
  237. 'Light snow showers' => 'Averse de neige lègére',
  238. 'Blowing snow' => 'Chasse neige',
  239. 'Snow' => 'Neige',
  240. 'Hail' => 'Grêle',
  241. 'Sleet' => 'Neige fondue',
  242. 'Dust' => 'Poussière',
  243. 'Foggy' => 'Brouillard',
  244. 'Smoky' => 'Fumée',
  245. 'Blustery' => 'Froid et venteux',
  246. 'Windy' => 'Venteux',
  247. 'Cold' => 'Froid',
  248. 'Cloudy' => 'Nuageux',
  249. 'Fair' => 'Ciel dégagé',
  250. 'Mixed rain and hail' => 'Pluie et grêle',
  251. 'Hot' => 'Chaud',
  252. 'Isolated thunderstorms' => 'Orages isolées',
  253. 'Scattered showers' => 'Averse éparse',
  254. 'Heavy snow' => 'Fortes chutes de neige',
  255. 'Scattered snow showers' => 'Averse de neige éparse',
  256. 'Thunderstorms' => 'Orages',
  257. 'Thundershowers' => 'Grain sous orage violents',
  258. 'Isolated thundershowers' => 'Grain sous orage isolées',
  259. 'Not available' => 'Non disponible',
  260. 'Scattered Thunderstorms' => 'Orages éparses',
  261. 'Partly Cloudy'=>'Partiellement nuageux',
  262. 'Mostly Sunny'=>'plutot ensoleillé',
  263. 'Mostly Cloudy'=>'plutot Nuageux',
  264. 'Light Rain'=>'Pluie fine',
  265. 'Clear'=>'Temps clair',
  266. 'Sunny'=>'ensoleillé',
  267. 'Rain/Wind'=>'Pluie et vent',
  268. 'Rain'=>'Pluie',
  269. 'Wind'=>'Vent',
  270. 'Partly Cloudy/Wind'=>'Partiellement nuageux avec du vent'
  271. );
  272. $dayTranslate = array('Wed'=>'mercredi',
  273. 'Sat'=>'samedi',
  274. 'Mon'=>'lundi',
  275. 'Tue'=>'mardi',
  276. 'Thu'=>'jeudi',
  277. 'Fri'=>'vendredi',
  278. 'Sun'=>'dimanche');
  279. $affirmation = '';
  280. foreach($weekdays as $day){
  281. if (substr($day['text'],0,2) == "AM")
  282. {
  283. $sub_condition = substr($day['text'],3);
  284. $condition = (isset($textTranslate[''.$sub_condition])?$textTranslate[''.$sub_condition]:$sub_condition)." dans la matinée";
  285. }
  286. elseif (substr($day['text'],0,2) == "PM") {
  287. $sub_condition = substr($day['text'],3);
  288. $condition = (isset($textTranslate[''.$sub_condition])?$textTranslate[''.$sub_condition]:$sub_condition)." dans l'après midi";
  289. }
  290. elseif (substr($day['text'],-4) == "Late") {
  291. $sub_condition = substr($day['text'],0,-5);
  292. $condition = (isset($textTranslate[''.$sub_condition])?$textTranslate[''.$sub_condition]:$sub_condition)." en fin de journée";
  293. }
  294. else
  295. {
  296. $condition = isset($textTranslate[''.$day['text']])?$textTranslate[''.$day['text']]:$day['text'];
  297. }
  298. if( (isset($_['today'])))
  299. {
  300. $affirmation .= 'Aujourd\'hui '.$day['temp'].' degrés, '.$condition.', ';
  301. }
  302. else
  303. {
  304. $affirmation .= $dayTranslate[''.$day['day']].' de '.$day['low'].' à '.$day['high'].' degrés, '.$condition.', ';
  305. }
  306. }
  307. }else{
  308. $affirmation = 'Vous devez renseigner votre ville dans les préférences de l\'interface oueb, je ne peux rien vous dire pour le moment.';
  309. }
  310. $response = array('responses'=>array(
  311. array('type'=>'talk','sentence'=>$affirmation)
  312. )
  313. );
  314. $json = json_encode($response);
  315. echo ($json=='[]'?'{}':$json);
  316. break;
  317. case 'vocalinfo_tv':
  318. global $_;
  319. libxml_use_internal_errors(true);
  320. $contents = file_get_contents('http://webnext.fr/epg_cache/programme-tv-rss_'.date('Y-m-d').'.xml');
  321. $xml = simplexml_load_string($contents);
  322. $emissions = $xml->xpath('/rss/channel/item');
  323. $focus = array();
  324. $time = time();
  325. $date = date('m/d/Y ',$time);
  326. $focusedCanals = array('TF1','France 2','France 3','France 4','Canal+','Arte','France 5','M6');
  327. foreach($emissions as $emission){
  328. $item = array();
  329. list($item['canal'],$item['hour'],$item['title']) = explode(' | ',$emission->title);
  330. $itemTime = strtotime($date.$item['hour']);
  331. if($itemTime>=$time-3600 && $itemTime<=$time+3600 && in_array($item['canal'], $focusedCanals)){
  332. if( (isset($_['category']) && $_['category']==''.$emission->category) || !isset($_['category']) ){
  333. $item['category'] = ''.$emission->category;
  334. $item['description'] = strip_tags(''.$emission->description);
  335. $focus[$item['title'].$item['canal']][] = $item;
  336. }
  337. }
  338. }
  339. $affirmation = '';
  340. $response = array();
  341. foreach($focus as $emission){
  342. $nb = count($emission);
  343. $emission = $emission[0];
  344. $affirmation = array();
  345. $affirmation['type'] = 'talk';
  346. //$affirmation['style'] = 'slow';
  347. $affirmation['sentence'] = ($nb>1?$nb.' ':'').ucfirst($emission['category']).', '.$emission['title'].' à '.$emission['hour'].' sur '.$emission['canal'];
  348. $response['responses'][] = $affirmation;
  349. }
  350. $json = json_encode($response);
  351. echo ($json=='[]'?'{}':$json);
  352. break;
  353. case 'vocalinfo_hour':
  354. global $_;
  355. $affirmation = 'Il est '.date('H:i');
  356. $response = array('responses'=>array(
  357. array('type'=>'talk','sentence'=>$affirmation)
  358. )
  359. );
  360. $json = json_encode($response);
  361. echo ($json=='[]'?'{}':$json);
  362. break;
  363. case 'vocalinfo_day':
  364. global $_;
  365. $affirmation = 'Nous sommes le '.date('d/m/Y');
  366. $response = array('responses'=>array(
  367. array('type'=>'talk','sentence'=>$affirmation)
  368. )
  369. );
  370. $json = json_encode($response);
  371. echo ($json=='[]'?'{}':$json);
  372. break;
  373. case 'vocalinfo_wikipedia':
  374. global $_;
  375. $url = 'http://fr.wikipedia.org/w/api.php?action=parse&page='.$_['word'].'&format=json&prop=text&section=0';
  376. $ch = curl_init($url);
  377. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  378. curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr-FR; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" ); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked)
  379. $c = curl_exec($ch);
  380. $json = json_decode($c);
  381. $content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML)
  382. $affirmation = strip_tags(str_replace('&#160;', ' ', $content)); // '&#160;' is a space, but is not recognized by yana trying to read "160"
  383. $response = array('responses'=>array(
  384. array('type'=>'talk','sentence'=>$affirmation)
  385. )
  386. );
  387. $json = json_encode($response);
  388. echo ($json=='[]'?'{}':$json);
  389. break;
  390. case 'vocalinfo_mood':
  391. global $_;
  392. $possible_answers = array(
  393. 'parfaitement'
  394. ,'ça pourrait aller mieux'
  395. ,'ça roule mon pote !'
  396. ,'nickel'
  397. ,'pourquoi cette question ?'
  398. );
  399. $affirmation = $possible_answers[rand(0,count($possible_answers)-1)];
  400. $response = array('responses'=>array(
  401. array('type'=>'talk','sentence'=>$affirmation)
  402. )
  403. );
  404. $json = json_encode($response);
  405. echo ($json=='[]'?'{}':$json);
  406. break;
  407. }
  408. }
  409. function vocalinfo_event(&$response){
  410. if(date('d/m H:i')=='24/01 15:00'){
  411. if(date('s')<45){
  412. $response['responses']= array(
  413. array('type'=>'sound','file'=>'sifflement.wav'),
  414. array('type'=>'talk','sentence'=>'C\'est l\'anniversaire de mon créateur, pensez à lui offrir une bière!')
  415. );
  416. }
  417. }
  418. }
  419. function vocalinfo_plugin_preference_menu(){
  420. global $_;
  421. echo '<li '.(@$_['block']=='vocalinfo'?'class="active"':'').'><a href="setting.php?section=preference&block=vocalinfo"><i class="fa fa-angle-right"></i> Informations Vocales</a></li>';
  422. }
  423. function vocalinfo_plugin_preference_page(){
  424. global $myUser,$_,$conf;
  425. if((isset($_['section']) && $_['section']=='preference' && @$_['block']=='vocalinfo' ) ){
  426. if($myUser!=false){
  427. Plugin::addjs("/js/woeid.js",true);
  428. Plugin::addJs('/js/main.js',true);
  429. $commands = json_decode(file_get_contents(Plugin::path().'/'.VOCALINFO_COMMAND_FILE),true);
  430. ?>
  431. <div class="span9 userBloc">
  432. <legend>Commandes</legend>
  433. <table class="table table-striped table-bordered">
  434. <tr>
  435. <th></th>
  436. <th>Commande</th>
  437. <th>Confidence</th>
  438. </tr>
  439. <?php foreach($commands as $key=>$command){ ?>
  440. <tr class="command" data-id="<?php echo $key; ?>"><td><input type="checkbox" <?php echo $command['disabled']=='true'?'':'checked="checked"' ?> class="enabled"></td><td><?php echo $conf->get('VOCAL_ENTITY_NAME').' '.$command['command']; ?></td><td><input type="text" class="confidence" value="<?php echo $command['confidence']; ?>"/></td></tr>
  441. <?php } ?>
  442. <tr>
  443. <td colspan="3"><div class="btn" onclick="plugin_vocalinfo_save();">Enregistrer</div></td>
  444. </tr>
  445. </table>
  446. <form class="form-inline" action="action.php?action=vocalinfo_plugin_setting" method="POST">
  447. <legend>Météo</legend>
  448. <label>Tapez le nom de votre ville et votre pays</label>
  449. <input type="text" class="input-xlarge" name="weather_place" value="<?php echo $conf->get('plugin_vocalinfo_place');?>" placeholder="Votre ville">
  450. <span id="weather_query" class="btn">Chercher</span>
  451. <br/><br/><label>Votre Identifiant WOEID</label>
  452. <input type="text" class="input-large" name="woeid" value="<?php echo $conf->get('plugin_vocalinfo_woeid');?>" placeholder="Votre WOEID">
  453. <button type="submit" class="btn">Sauvegarder</button>
  454. </form>
  455. </div>
  456. <?php }else{ ?>
  457. <div id="main" class="wrapper clearfix">
  458. <article>
  459. <h3>Vous devez être connecté</h3>
  460. </article>
  461. </div>
  462. <?php
  463. }
  464. }
  465. }
  466. Plugin::addHook("preference_menu", "vocalinfo_plugin_preference_menu");
  467. Plugin::addHook("preference_content", "vocalinfo_plugin_preference_page");
  468. Plugin::addHook("get_event", "vocalinfo_event");
  469. Plugin::addHook("action_post_case", "vocalinfo_action");
  470. Plugin::addHook("vocal_command", "vocalinfo_vocal_command");
  471. ?>