1
|
|
|
<?php |
2
|
|
|
namespace JarvisPHP\TelegramBot; |
3
|
|
|
|
4
|
|
|
use JarvisPHP\TelegramBot\TelegramBotApiWrapper; |
5
|
|
|
use JarvisPHP\TelegramBot\GenericCurl; |
6
|
|
|
|
7
|
|
|
//Very important! |
8
|
|
|
define('_JARVISPHP_URL','http://localhost:8000'); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A TelegramBot for JarvisPHP |
12
|
|
|
* @author Stefano Bianchini |
13
|
|
|
* @website http://www.stefanobianchini.net |
14
|
|
|
*/ |
15
|
|
|
class JarvisPHPTelegramBot { |
16
|
|
|
|
17
|
|
|
public static function run($allowedClientIdList) { |
18
|
|
|
|
19
|
|
|
$bot = new TelegramBotApiWrapper(); |
20
|
|
|
|
21
|
|
|
$offset = 0; |
22
|
|
|
|
23
|
|
|
echo "JarvisPHP Telegram Bot started at ".date('Y-m-d H:i:s')."\n"; |
24
|
|
|
echo "-----------------------------------------------------\n"; |
25
|
|
|
|
26
|
|
|
while(true) { |
27
|
|
|
|
28
|
|
|
$updates = $bot->apiRequestJson("getUpdates", array('offset'=>$offset)); |
29
|
|
|
|
30
|
|
|
if($updates) { |
31
|
|
|
foreach($updates as $update) { |
32
|
|
|
|
33
|
|
|
if(isset($update->message->text)) { |
34
|
|
|
|
35
|
|
|
//TODO check if $update->message->chat->id is in enabled list |
36
|
|
|
|
37
|
|
|
$bot->apiRequestJson("sendChatAction", array('chat_id' => $update->message->chat->id, 'action' => 'typing')); |
38
|
|
|
|
39
|
|
|
echo "Processing message ->".$update->message->text."\n"; |
40
|
|
|
|
41
|
|
|
$message = $update->message->text; |
42
|
|
|
|
43
|
|
|
$offset = $update->update_id + 1; |
44
|
|
|
|
45
|
|
|
$response = ''; |
46
|
|
|
|
47
|
|
|
//Understand if the message is a telegram command (begins with "/") |
48
|
|
|
if(preg_match('$^/(.+)$', $message)) { |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
} 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
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.