diff --git a/example.php b/example.php index ce51040..2ec406a 100644 --- a/example.php +++ b/example.php @@ -19,14 +19,14 @@ 'message' => array ( 'message_id' => 12345, 'from' => array ( - 'id' => 123, + 'id' => 100, 'is_bot' => false, 'first_name' => 'Yehuda', 'last_name' => 'Eisenberg', 'language_code' => 'en', ), 'chat' => array ( - 'id' => 123, + 'id' => 101, 'first_name' => 'Yehuda', 'last_name' => 'Eisenberg', 'type' => 'private', @@ -47,8 +47,8 @@ $bot->setUpdate($update); -var_dump($bot->getUpdate()->update_id); -var_dump($bot->getUpdate()->message); +// var_dump($bot->getUpdate()->update_id); +var_dump($bot->getUpdate()); // $bot->sendMessage($user, $text); diff --git a/src/Objects/TelegramObject.php b/src/Objects/TelegramObject.php index e05cae8..850f61b 100644 --- a/src/Objects/TelegramObject.php +++ b/src/Objects/TelegramObject.php @@ -18,12 +18,18 @@ class TelegramObject{ protected $fields = []; + // public function __construct(... $values){ + // foreach(array_combine(array_keys($this->fields), $values) as $field => $arg){ + // $this->field = $arg; + // } + // } + public function __get($fieldName){ if(isset($this->fields[$fieldName])){ return $this->fields[$fieldName]; } else{ - throw new TelegramException("Field '$fieldName' not defined in class " . __CLASS__); + throw new TelegramException("Field '$fieldName' not defined in class " . get_class($this)); } } @@ -32,7 +38,8 @@ public function __set($fieldName, $value){ $this->fields[$fieldName] = $value; } else{ - throw new TelegramException("Field '$fieldName' not defined in class " . __CLASS__); + var_dump($this); + throw new TelegramException("Field '$fieldName' not defined in class " . get_class($this)); } } diff --git a/src/Parser.php b/src/Parser.php index ac37e83..e496973 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -48,7 +48,7 @@ public function walker($arr){ $this->currentObj->$subarrK = $newObject; // creat new sub object to update key - $tmp = $this->currentObj->$subarrK; // put the pointer to previus level in tmp + $tmp = $this->currentObj; // put pointer to previus level in tmp $this->currentObj = $this->currentObj->$subarrK; // set current object to dipper $this->walker($subarrV); // walk over the sub object diff --git a/src/TelegramBot.php b/src/TelegramBot.php index d87dabf..24028fb 100644 --- a/src/TelegramBot.php +++ b/src/TelegramBot.php @@ -19,10 +19,11 @@ class TelegramBot{ public function __construct($config){ if (PHP_SAPI === 'cli'){ - print "cli mode. using getUpdates method"; #TODO add async heandling + print "cli mode. using getUpdates method" . PHP_EOL; #TODO add async heandling }else{ // using webhook method + $this->setUpdate(json_decode("php://input"), true); } } @@ -30,6 +31,10 @@ public function __construct($config){ public function setUpdate($update){ if(gettype($update) == "string" || gettype($update) == "array"){ $this->update = new Parser($update); + }elseif(gettype($update) == "object" && get_class($update) == "YehudaEi\TelegramBots\Objects\Update"){ + $this->update = $update; + }else{ + throw new TelegramException("Invalid update. update must be an array, json or object"); } } @@ -88,5 +93,22 @@ public function forwardMessage($destChat, $fromChat, $message){ $data["disable_notification"] = $this->bot->config->notification; $data["message_id"] = $message->message_id; return $this->exec("forwardMessage", $data); + } + public function sendDocument($chat, $document, $reply_to_message_id, $reply_markup, $caption){ + if(get_class($chat) !== "User") + throw new TelegramException("\$chat must be a User"); + elseif(get_class($document) !== "Document") + if(!filter_var($document, FILTER_VALIDATE_URL) && !is_link($document) && get_class($document) !== "CURLFile") + throw new TelegramException("\$document must be a Document or an URL or a CURLFile object"); + + $data['chat'] = $chat; + if(get_class($document) == "Document") + $data['document'] = $document->file_id; + else + $data['document'] = $document; + + + + } }