@@ -14,87 +14,87 @@ |
||
14 | 14 | */ |
15 | 15 | class JarvisPHPTelegramBot { |
16 | 16 | |
17 | - public static function run($allowedClientIdList) { |
|
17 | + public static function run($allowedClientIdList) { |
|
18 | 18 | |
19 | - $bot = new TelegramBotApiWrapper(); |
|
19 | + $bot = new TelegramBotApiWrapper(); |
|
20 | 20 | |
21 | - $offset = 0; |
|
21 | + $offset = 0; |
|
22 | 22 | |
23 | - echo "JarvisPHP Telegram Bot started at ".date('Y-m-d H:i:s')."\n"; |
|
24 | - echo "-----------------------------------------------------\n"; |
|
23 | + echo "JarvisPHP Telegram Bot started at ".date('Y-m-d H:i:s')."\n"; |
|
24 | + echo "-----------------------------------------------------\n"; |
|
25 | 25 | |
26 | - while(true) { |
|
26 | + while(true) { |
|
27 | 27 | |
28 | - $updates = $bot->apiRequestJson("getUpdates", array('offset'=>$offset)); |
|
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 | - //TODO check if $update->message->chat->id is in enabled list |
|
35 | + //TODO check if $update->message->chat->id is in enabled list |
|
36 | 36 | |
37 | - $bot->apiRequestJson("sendChatAction", array('chat_id' => $update->message->chat->id, 'action' => 'typing')); |
|
37 | + $bot->apiRequestJson("sendChatAction", array('chat_id' => $update->message->chat->id, 'action' => 'typing')); |
|
38 | 38 | |
39 | - echo "Processing message ->".$update->message->text."\n"; |
|
39 | + echo "Processing message ->".$update->message->text."\n"; |
|
40 | 40 | |
41 | - $message = $update->message->text; |
|
41 | + $message = $update->message->text; |
|
42 | 42 | |
43 | - $offset = $update->update_id + 1; |
|
43 | + $offset = $update->update_id + 1; |
|
44 | 44 | |
45 | - $response = ''; |
|
45 | + $response = ''; |
|
46 | 46 | |
47 | - //Understand if the message is a telegram command (begins with "/") |
|
48 | - if(preg_match('$^/(.+)$', $message)) { |
|
47 | + //Understand if the message is a telegram command (begins with "/") |
|
48 | + if(preg_match('$^/(.+)$', $message)) { |
|
49 | 49 | |
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; |
|
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); |
|
57 | - break; |
|
58 | - case '/say': $response='Use /say "sentence" (without quotes) to make JarvisPHP speak a sentence.'; break; |
|
59 | - } |
|
60 | - |
|
61 | - //"Say" Telegram command |
|
62 | - if(preg_match('$^/say (.+)$', $message, $matches)) { |
|
63 | - if($matches) { |
|
64 | - if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
65 | - //Redirect message to JarvisPhp |
|
66 | - $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/say', array('sentence'=>$matches[1])); |
|
67 | - |
|
68 | - $response = $JarvisResponse->answer; |
|
69 | - } else { |
|
70 | - $response = 'You are not allowed to speak with me.'; |
|
71 | - } |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - //Encode emoji |
|
76 | - $response = preg_replace_callback('/\\\\u([0-9a-fA-F]+)/', function ($match) { |
|
77 | - return iconv('UCS-4LE', 'UTF-8', pack('V', hexdec($match[1]))); |
|
78 | - }, $response); |
|
79 | - |
|
80 | - } else { |
|
81 | - if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
82 | - //Redirect message to JarvisPhp |
|
83 | - $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/answer', array('command'=>$message, 'tts' => 'None_tts')); |
|
84 | - |
|
85 | - $response = $JarvisResponse->answer; |
|
86 | - } else { |
|
87 | - $response = 'You are not allowed to speak with me.'; |
|
88 | - } |
|
89 | - |
|
90 | - } |
|
91 | - if($response) { |
|
92 | - $bot->apiRequestJson("sendMessage", array('chat_id' => $update->message->chat->id, "text" => $response)); |
|
93 | - } |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - sleep(1); |
|
98 | - } |
|
99 | - } |
|
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; |
|
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); |
|
57 | + break; |
|
58 | + case '/say': $response='Use /say "sentence" (without quotes) to make JarvisPHP speak a sentence.'; break; |
|
59 | + } |
|
60 | + |
|
61 | + //"Say" Telegram command |
|
62 | + if(preg_match('$^/say (.+)$', $message, $matches)) { |
|
63 | + if($matches) { |
|
64 | + if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
65 | + //Redirect message to JarvisPhp |
|
66 | + $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/say', array('sentence'=>$matches[1])); |
|
67 | + |
|
68 | + $response = $JarvisResponse->answer; |
|
69 | + } else { |
|
70 | + $response = 'You are not allowed to speak with me.'; |
|
71 | + } |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + //Encode emoji |
|
76 | + $response = preg_replace_callback('/\\\\u([0-9a-fA-F]+)/', function ($match) { |
|
77 | + return iconv('UCS-4LE', 'UTF-8', pack('V', hexdec($match[1]))); |
|
78 | + }, $response); |
|
79 | + |
|
80 | + } else { |
|
81 | + if(in_array($update->message->chat->id, $allowedClientIdList)) { |
|
82 | + //Redirect message to JarvisPhp |
|
83 | + $JarvisResponse = GenericCurl::exec(_JARVISPHP_URL.'/answer', array('command'=>$message, 'tts' => 'None_tts')); |
|
84 | + |
|
85 | + $response = $JarvisResponse->answer; |
|
86 | + } else { |
|
87 | + $response = 'You are not allowed to speak with me.'; |
|
88 | + } |
|
89 | + |
|
90 | + } |
|
91 | + if($response) { |
|
92 | + $bot->apiRequestJson("sendMessage", array('chat_id' => $update->message->chat->id, "text" => $response)); |
|
93 | + } |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + sleep(1); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |
101 | 101 | \ No newline at end of file |