From f2352804e26010f49ce29b0fba0311f7be6d37cf Mon Sep 17 00:00:00 2001 From: naftali100 <48069590+naftali100@users.noreply.github.com> Date: Mon, 6 Jul 2020 14:53:19 +0300 Subject: [PATCH 1/2] fix parser and minor error info --- example.php | 8 ++++---- src/Objects/TelegramObject.php | 5 +++-- src/Parser.php | 2 +- src/TelegramBot.php | 5 ++++- 4 files changed, 12 insertions(+), 8 deletions(-) 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..477ceab 100644 --- a/src/Objects/TelegramObject.php +++ b/src/Objects/TelegramObject.php @@ -23,7 +23,7 @@ public function __get($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 +32,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..232fe89 100644 --- a/src/TelegramBot.php +++ b/src/TelegramBot.php @@ -19,7 +19,7 @@ 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 @@ -89,4 +89,7 @@ public function forwardMessage($destChat, $fromChat, $message){ $data["message_id"] = $message->message_id; return $this->exec("forwardMessage", $data); } + public function sendDocument($chat, $document){ + + } } From 10700a4efa24193ae151bc7969fd4ff3a2dc188c Mon Sep 17 00:00:00 2001 From: naftali100 <48069590+naftali100@users.noreply.github.com> Date: Sun, 12 Jul 2020 18:26:12 +0300 Subject: [PATCH 2/2] some start of functions and to deside objects constract --- src/Objects/TelegramObject.php | 6 ++++++ src/TelegramBot.php | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Objects/TelegramObject.php b/src/Objects/TelegramObject.php index 477ceab..850f61b 100644 --- a/src/Objects/TelegramObject.php +++ b/src/Objects/TelegramObject.php @@ -18,6 +18,12 @@ 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]; diff --git a/src/TelegramBot.php b/src/TelegramBot.php index 232fe89..24028fb 100644 --- a/src/TelegramBot.php +++ b/src/TelegramBot.php @@ -23,6 +23,7 @@ public function __construct($config){ }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"); } } @@ -89,7 +94,21 @@ public function forwardMessage($destChat, $fromChat, $message){ $data["message_id"] = $message->message_id; return $this->exec("forwardMessage", $data); } - public function sendDocument($chat, $document){ - + 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; + + + + } }