@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use JarvisPHP\TelegramBot\GenericCurl; |
6 | 6 | |
7 | 7 | //Very important! |
8 | -define('_JARVISPHP_URL','http://localhost:8000'); |
|
8 | +define('_JARVISPHP_URL', 'http://localhost:8000'); |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * A TelegramBot for JarvisPHP |
@@ -23,14 +23,14 @@ discard block |
||
23 | 23 | echo "JarvisPHP Telegram Bot started at ".date('Y-m-d H:i:s')."\n"; |
24 | 24 | echo "-----------------------------------------------------\n"; |
25 | 25 | |
26 | - while(true) { |
|
26 | + while (true) { |
|
27 | 27 | |
28 | 28 | $updates = $bot->apiRequestJson("getUpdates", array('offset'=>$offset)); |
29 | 29 | |
30 | - if($updates) { |
|
31 | - foreach($updates as $update) { |
|
30 | + if ($updates) { |
|
31 | + foreach ($updates as $update) { |
|
32 | 32 | |
33 | - if(isset($update->message->text)) { |
|
33 | + if (isset($update->message->text)) { |
|
34 | 34 | |
35 | 35 | //TODO check if $update->message->chat->id is in enabled list |
36 | 36 | |
@@ -40,28 +40,28 @@ discard block |
||
40 | 40 | |
41 | 41 | $message = $update->message->text; |
42 | 42 | |
43 | - $offset = $update->update_id + 1; |
|
43 | + $offset = $update->update_id+1; |
|
44 | 44 | |
45 | 45 | $response = ''; |
46 | 46 | |
47 | 47 | //Understand if the message is a telegram command (begins with "/") |
48 | - if(preg_match('$^/(.+)$', $message)) { |
|
48 | + if (preg_match('$^/(.+)$', $message)) { |
|
49 | 49 | |
50 | 50 | //Telegram command |
51 | - switch($message) { |
|
52 | - case '/start': $response='I am JarvisPHP, a private bot. \u1F510'; break; |
|
53 | - case '/info': $response='I am JarvisPHP, a private bot. \u1F510'; break; |
|
51 | + switch ($message) { |
|
52 | + case '/start': $response = 'I am JarvisPHP, a private bot. \u1F510'; break; |
|
53 | + case '/info': $response = 'I am JarvisPHP, a private bot. \u1F510'; break; |
|
54 | 54 | case '/register': |
55 | - $response='Ok, i registered your ID in registerIdLog.log'; |
|
56 | - file_put_contents('TelegramBot/registerIdLog.log', '['.date('Y-m-d H:i:s').'] ID:'.$update->message->from->id . '; FIRSTNAME:'. $update->message->from->first_name . '; LASTNAME:'. $update->message->from->last_name. '; USERNAME:'. $update->message->from->username.PHP_EOL , FILE_APPEND | LOCK_EX); |
|
55 | + $response = 'Ok, i registered your ID in registerIdLog.log'; |
|
56 | + file_put_contents('TelegramBot/registerIdLog.log', '['.date('Y-m-d H:i:s').'] ID:'.$update->message->from->id.'; FIRSTNAME:'.$update->message->from->first_name.'; LASTNAME:'.$update->message->from->last_name.'; USERNAME:'.$update->message->from->username.PHP_EOL, FILE_APPEND | LOCK_EX); |
|
57 | 57 | break; |
58 | - case '/say': $response='Use /say "sentence" (without quotes) to make JarvisPHP speak a sentence.'; break; |
|
58 | + case '/say': $response = 'Use /say "sentence" (without quotes) to make JarvisPHP speak a sentence.'; break; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | //"Say" Telegram command |
62 | - if(preg_match('$^/say (.+)$', $message, $matches)) { |
|
63 | - if($matches) { |
|
64 | - if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
62 | + if (preg_match('$^/say (.+)$', $message, $matches)) { |
|
63 | + if ($matches) { |
|
64 | + if (in_array($update->message->chat->id, $allowedClientIdList)) { |
|
65 | 65 | //Redirect message to JarvisPhp |
66 | 66 | $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/answer', array('sentence'=>$matches[0])); |
67 | 67 | |
@@ -73,12 +73,12 @@ discard block |
||
73 | 73 | } |
74 | 74 | |
75 | 75 | //Encode emoji |
76 | - $response = preg_replace_callback('/\\\\u([0-9a-fA-F]+)/', function ($match) { |
|
76 | + $response = preg_replace_callback('/\\\\u([0-9a-fA-F]+)/', function($match) { |
|
77 | 77 | return iconv('UCS-4LE', 'UTF-8', pack('V', hexdec($match[1]))); |
78 | 78 | }, $response); |
79 | 79 | |
80 | 80 | } else { |
81 | - if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
81 | + if (in_array($update->message->chat->id, $allowedClientIdList)) { |
|
82 | 82 | //Redirect message to JarvisPhp |
83 | 83 | $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/answer', array('command'=>$message, 'tts' => 'None_tts')); |
84 | 84 | |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | } |
89 | 89 | |
90 | 90 | } |
91 | - if($response) { |
|
91 | + if ($response) { |
|
92 | 92 | $bot->apiRequestJson("sendMessage", array('chat_id' => $update->message->chat->id, "text" => $response)); |
93 | 93 | } |
94 | 94 | } |
@@ -62,7 +62,7 @@ |
||
62 | 62 | |
63 | 63 | $_BOT_TOKEN = ''; |
64 | 64 | //Load API key from json config |
65 | - if(file_exists('TelegramBot/api-key.json')) { |
|
65 | + if (file_exists('TelegramBot/api-key.json')) { |
|
66 | 66 | //Create your own bot token and put it in api-key.json |
67 | 67 | // like {"bot_token": "<your-bot-token>"} |
68 | 68 | $json_config = json_decode(file_get_contents('TelegramBot/api-key.json')); |
@@ -13,8 +13,8 @@ discard block |
||
13 | 13 | |
14 | 14 | $fields_string = ""; |
15 | 15 | //url-ify the data for the POST |
16 | - if(count($fields)>0) { |
|
17 | - foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } |
|
16 | + if (count($fields) > 0) { |
|
17 | + foreach ($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } |
|
18 | 18 | rtrim($fields_string, '&'); |
19 | 19 | } |
20 | 20 | |
@@ -23,11 +23,11 @@ discard block |
||
23 | 23 | $ch = curl_init(); |
24 | 24 | |
25 | 25 | //set the url, number of POST vars, POST data |
26 | - curl_setopt($ch,CURLOPT_URL, $url); |
|
27 | - curl_setopt($ch,CURLOPT_POST, count($fields)); |
|
28 | - curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); |
|
26 | + curl_setopt($ch, CURLOPT_URL, $url); |
|
27 | + curl_setopt($ch, CURLOPT_POST, count($fields)); |
|
28 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); |
|
29 | 29 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
30 | - curl_setopt ($ch, CURLOPT_COOKIEFILE, "TelegramBot/JarvisPHPSession.cookie"); |
|
30 | + curl_setopt($ch, CURLOPT_COOKIEFILE, "TelegramBot/JarvisPHPSession.cookie"); |
|
31 | 31 | curl_setopt($ch, CURLOPT_COOKIEJAR, "TelegramBot/JarvisPHPSession.cookie"); |
32 | 32 | |
33 | 33 | //execute post |
@@ -40,6 +40,6 @@ discard block |
||
40 | 40 | |
41 | 41 | |
42 | 42 | |
43 | - return ($http_response_code==200) ? json_decode($result) : false; |
|
43 | + return ($http_response_code == 200) ? json_decode($result) : false; |
|
44 | 44 | } |
45 | 45 | } |
46 | 46 | \ No newline at end of file |
@@ -5,10 +5,10 @@ |
||
5 | 5 | * m1, m2, ... m8 male voices |
6 | 6 | * f1, f2, f3, f4 female voices |
7 | 7 | */ |
8 | -define('_ESPEAK_VOICE','m2'); |
|
8 | +define('_ESPEAK_VOICE', 'm2'); |
|
9 | 9 | |
10 | 10 | //Language of text-to-speech |
11 | 11 | define('_ESPEAK_LANGUAGE', _LANGUAGE); |
12 | 12 | |
13 | 13 | //Amplitude (espeak -a <amplitude>) |
14 | -define('_ESPEAK_AMPLITUDE','100'); |
|
15 | 14 | \ No newline at end of file |
15 | +define('_ESPEAK_AMPLITUDE', '100'); |
|
16 | 16 | \ No newline at end of file |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | */ |
5 | 5 | |
6 | 6 | //Set locale |
7 | -define('_LANGUAGE','en'); |
|
7 | +define('_LANGUAGE', 'en'); |
|
8 | 8 | |
9 | 9 | //Command session timeout, in seconds |
10 | 10 | define('_COMMAND_SESSION_TIMEOUT', 30); |
@@ -13,4 +13,4 @@ discard block |
||
13 | 13 | define('_JARVIS_TTS', 'None_tts'); |
14 | 14 | |
15 | 15 | //Define system's name |
16 | -define('_SYSTEM_NAME','JarvisPhp'); |
|
17 | 16 | \ No newline at end of file |
17 | +define('_SYSTEM_NAME', 'JarvisPhp'); |
|
18 | 18 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Stefano Bianchini |
13 | 13 | * @website http://www.stefanobianchini.net |
14 | 14 | */ |
15 | -class Info_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
|
15 | +class Info_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
|
16 | 16 | /** |
17 | 17 | * Priority of plugin |
18 | 18 | * @var int |
@@ -25,15 +25,15 @@ discard block |
||
25 | 25 | */ |
26 | 26 | function answer($command) { |
27 | 27 | $answer = ''; |
28 | - if(preg_match(JarvisLanguage::translate('preg_match_tell_more',get_called_class()), $command)) { |
|
28 | + if (preg_match(JarvisLanguage::translate('preg_match_tell_more', get_called_class()), $command)) { |
|
29 | 29 | //Testing session |
30 | 30 | JarvisPHP::getLogger()->debug('User says: '.$command); |
31 | - $answer = 'Ok, i am on '. php_uname(); |
|
31 | + $answer = 'Ok, i am on '.php_uname(); |
|
32 | 32 | JarvisSession::terminate(); |
33 | 33 | } |
34 | 34 | else { |
35 | 35 | JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
36 | - $answer = sprintf(JarvisLanguage::translate('my_name_is',get_called_class()),_SYSTEM_NAME, $_SERVER['SERVER_NAME'],$_SERVER['SERVER_ADDR']); |
|
36 | + $answer = sprintf(JarvisLanguage::translate('my_name_is', get_called_class()), _SYSTEM_NAME, $_SERVER['SERVER_NAME'], $_SERVER['SERVER_ADDR']); |
|
37 | 37 | |
38 | 38 | } |
39 | 39 | JarvisTTS::speak($answer); |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | * @return boolean |
55 | 55 | */ |
56 | 56 | function isLikely($command) { |
57 | - return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
57 | + return preg_match(JarvisLanguage::translate('preg_match_activate_plugin', get_called_class()), $command); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
@@ -63,8 +63,7 @@ |
||
63 | 63 | if(!$result) { |
64 | 64 | JarvisPHP::getLogger()->error('Curl error: '.curl_error($ch)); |
65 | 65 | $answer = JarvisLanguage::translate('command_not_sent',get_called_class()); |
66 | - } |
|
67 | - else { |
|
66 | + } else { |
|
68 | 67 | $answer = JarvisLanguage::translate('command_sent_to_light_switch',get_called_class()); |
69 | 68 | } |
70 | 69 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * @author Stefano Bianchini |
16 | 16 | * @website http://www.stefanobianchini.net |
17 | 17 | */ |
18 | -class Weather_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
|
18 | +class Weather_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
|
19 | 19 | /** |
20 | 20 | * Priority of plugin |
21 | 21 | * @var int |
@@ -38,40 +38,40 @@ discard block |
||
38 | 38 | $_OPENWEATHERMAP_API_KEY = ''; |
39 | 39 | |
40 | 40 | //Load API key from json config |
41 | - if(file_exists('Plugins/Weather_plugin/api-key.json')) { |
|
41 | + if (file_exists('Plugins/Weather_plugin/api-key.json')) { |
|
42 | 42 | //Create your own api key and put it in api-key.json |
43 | 43 | $json_config = json_decode(file_get_contents('Plugins/Weather_plugin/api-key.json')); |
44 | 44 | $_OPENWEATHERMAP_API_KEY = $json_config->openweathermap_key; |
45 | 45 | } |
46 | 46 | |
47 | 47 | try { |
48 | - $forecast = $owm->getWeatherForecast('Rimini', 'metric', _LANGUAGE, $_OPENWEATHERMAP_API_KEY,2); |
|
49 | - } catch(OWMException $e) { |
|
50 | - JarvisPHP::getLogger()->error('OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
|
51 | - $answer = JarvisLanguage::translate('weather_error',get_called_class()); |
|
52 | - } catch(\Exception $e) { |
|
53 | - JarvisPHP::getLogger()->error('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
|
54 | - $answer = JarvisLanguage::translate('weather_error',get_called_class()); |
|
48 | + $forecast = $owm->getWeatherForecast('Rimini', 'metric', _LANGUAGE, $_OPENWEATHERMAP_API_KEY, 2); |
|
49 | + } catch (OWMException $e) { |
|
50 | + JarvisPHP::getLogger()->error('OpenWeatherMap exception: '.$e->getMessage().' (Code '.$e->getCode().').'); |
|
51 | + $answer = JarvisLanguage::translate('weather_error', get_called_class()); |
|
52 | + } catch (\Exception $e) { |
|
53 | + JarvisPHP::getLogger()->error('General exception: '.$e->getMessage().' (Code '.$e->getCode().').'); |
|
54 | + $answer = JarvisLanguage::translate('weather_error', get_called_class()); |
|
55 | 55 | } |
56 | 56 | |
57 | - if(preg_match(JarvisLanguage::translate('preg_match_today',get_called_class()), $command)) { |
|
58 | - $answer = JarvisLanguage::translate('forecast_for_today',get_called_class()); |
|
57 | + if (preg_match(JarvisLanguage::translate('preg_match_today', get_called_class()), $command)) { |
|
58 | + $answer = JarvisLanguage::translate('forecast_for_today', get_called_class()); |
|
59 | 59 | foreach ($forecast as $weather) { |
60 | - if($weather->time->day->format('d.m.Y')==date('d.m.Y')) { |
|
61 | - $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
|
62 | - $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
|
60 | + if ($weather->time->day->format('d.m.Y') == date('d.m.Y')) { |
|
61 | + $answer .= JarvisLanguage::translate('from', get_called_class())." ".$weather->time->from->format('H:i')." ".JarvisLanguage::translate('to', get_called_class())." ".$weather->time->to->format('H:i').": "; |
|
62 | + $answer .= $weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature', get_called_class()), trim(str_replace('°C', '', $weather->temperature))); |
|
63 | 63 | } |
64 | 64 | } |
65 | - } else if(preg_match(JarvisLanguage::translate('preg_match_tomorrow',get_called_class()), $command)) { |
|
66 | - $answer = JarvisLanguage::translate('forecast_for_tomorrow',get_called_class()); |
|
65 | + } else if (preg_match(JarvisLanguage::translate('preg_match_tomorrow', get_called_class()), $command)) { |
|
66 | + $answer = JarvisLanguage::translate('forecast_for_tomorrow', get_called_class()); |
|
67 | 67 | foreach ($forecast as $weather) { |
68 | - if($weather->time->day->format('d.m.Y')==$tomorrow->format('d.m.Y')) { |
|
69 | - $answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
|
70 | - $answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
|
68 | + if ($weather->time->day->format('d.m.Y') == $tomorrow->format('d.m.Y')) { |
|
69 | + $answer .= JarvisLanguage::translate('from', get_called_class())." ".$weather->time->from->format('H:i')." ".JarvisLanguage::translate('to', get_called_class())." ".$weather->time->to->format('H:i').": "; |
|
70 | + $answer .= $weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature', get_called_class()), trim(str_replace('°C', '', $weather->temperature))); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | } else { |
74 | - $answer = JarvisLanguage::translate('not_understand_weather_day',get_called_class()); |
|
74 | + $answer = JarvisLanguage::translate('not_understand_weather_day', get_called_class()); |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @return boolean |
96 | 96 | */ |
97 | 97 | function isLikely($command) { |
98 | - return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
98 | + return preg_match(JarvisLanguage::translate('preg_match_activate_plugin', get_called_class()), $command); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Stefano Bianchini |
13 | 13 | * @website http://www.stefanobianchini.net |
14 | 14 | */ |
15 | -class Echo_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
|
15 | +class Echo_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
|
16 | 16 | /** |
17 | 17 | * Priority of plugin |
18 | 18 | * @var int |
@@ -25,12 +25,12 @@ discard block |
||
25 | 25 | */ |
26 | 26 | function answer($command) { |
27 | 27 | $answer = ''; |
28 | - if(JarvisSession::get('echo_not_first_passage')) { |
|
28 | + if (JarvisSession::get('echo_not_first_passage')) { |
|
29 | 29 | $answer = $command; |
30 | 30 | } else { |
31 | - JarvisSession::set('echo_not_first_passage',true); |
|
31 | + JarvisSession::set('echo_not_first_passage', true); |
|
32 | 32 | JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
33 | - $answer = JarvisLanguage::translate('let_s_play',get_called_class()); |
|
33 | + $answer = JarvisLanguage::translate('let_s_play', get_called_class()); |
|
34 | 34 | } |
35 | 35 | JarvisTTS::speak($answer); |
36 | 36 | $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * @return boolean |
52 | 52 | */ |
53 | 53 | function isLikely($command) { |
54 | - return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
54 | + return preg_match(JarvisLanguage::translate('preg_match_activate_plugin', get_called_class()), $command); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * @author Stefano Bianchini |
13 | 13 | * @website http://www.stefanobianchini.net |
14 | 14 | */ |
15 | -class Movie_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
|
15 | +class Movie_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
|
16 | 16 | /** |
17 | 17 | * Priority of plugin |
18 | 18 | * @var int |
@@ -28,14 +28,14 @@ discard block |
||
28 | 28 | JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
29 | 29 | |
30 | 30 | //Understand the movie |
31 | - preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command, $matches); |
|
31 | + preg_match(JarvisLanguage::translate('preg_match_activate_plugin', get_called_class()), $command, $matches); |
|
32 | 32 | |
33 | 33 | $movie_title = end($matches); |
34 | 34 | |
35 | 35 | $_THEMOVIEDB_API_KEY = ''; |
36 | 36 | |
37 | 37 | //Load API key from json config |
38 | - if(file_exists('Plugins/Movie_plugin/api-key.json')) { |
|
38 | + if (file_exists('Plugins/Movie_plugin/api-key.json')) { |
|
39 | 39 | //Create your own api key and put it in api-key.json |
40 | 40 | $json_config = json_decode(file_get_contents('Plugins/Movie_plugin/api-key.json')); |
41 | 41 | $_THEMOVIEDB_API_KEY = $json_config->themoviedb_key; |
@@ -46,11 +46,11 @@ discard block |
||
46 | 46 | |
47 | 47 | $result = $client->getSearchApi()->searchMovies($movie_title, ['language'=>_LANGUAGE, 'page'=>1]); |
48 | 48 | |
49 | - if(count($result['results'])>0) { |
|
50 | - $answer = ($result['results'][0]['overview']) ? ($result['results'][0]['overview']) : JarvisLanguage::translate('no_results',get_called_class()); |
|
49 | + if (count($result['results']) > 0) { |
|
50 | + $answer = ($result['results'][0]['overview']) ? ($result['results'][0]['overview']) : JarvisLanguage::translate('no_results', get_called_class()); |
|
51 | 51 | } else { |
52 | 52 | //No results |
53 | - $answer = JarvisLanguage::translate('no_results',get_called_class()); |
|
53 | + $answer = JarvisLanguage::translate('no_results', get_called_class()); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | JarvisTTS::speak($answer); |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * @return boolean |
73 | 73 | */ |
74 | 74 | function isLikely($command) { |
75 | - return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
75 | + return preg_match(JarvisLanguage::translate('preg_match_activate_plugin', get_called_class()), $command); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |