Please take a look at this straightforward code:
$result = Request::sendMessage(['chat_id' => $chatId, 'text' => $message]);
$response = $result->getResult();
echo $response->message_id;
This results to the following error:
PHP Fatal Error 'yiibaseErrorException' with message 'Cannot access protected property LongmanTelegramBotEntitiesMessage::$message_id'
How is it possible to access server response data?
Thanks so much.
P.S.: Did not help: https://github.com/akalongman/php-telegram-bot/issues/161
$response->getMessageId() is what you want to use.
You always have to get values using appropriate getter functions.
You can print_r the result to see what data is available.
Thanks so much @jacklul
I didn't think that its required to use a separate getter for each property, but it worked well, thanks.
I'm still unable to get objects inside the response object, for example there is LongmanTelegramBotEntitiesChat Object inside the response and there is no getChat() method.
How is it possible to get that objects?
getResult() will return Chat object, in that case you just use appropriate getters for Chat object like getFrom(). It should work like this. If it still has no chat object might want to verify with print_r.
Hey please explain with an example, i'm not following
$result = Request::sendXXX(); will send the request type of XXX and write the response to $result var
$result->getResult(); will contain the object you just sent if $result->isOk() is true
So if you sendMessage it will contain sent Message object and it's id in the current chat (for reference in future editing)
How about accessing it before we actually send it ?
That wouldn't make sense, all data for the request will be provided by you in array, so before sending that's all you will have. Extra fields like messages Id will be returned by Telegram after sending, this data can't be 'predicted'.
You can access by:
$telegram = new \Longman\TelegramBot\Telegram($token, 'testname');
$r = new \Longman\TelegramBot\Request($telegram);
$response = $r::getMe();
if($response->isOk())
{
//$response->reflect() is data array
echo $response->reflect()['result']['username'];
}
@redflasher Where do you get that code from? Have you implemented some custom functionality into your library?
@noplanman I just try use all public methods of Entity class, that is base class for ServerResponse.
It isn't my library, i just use this lib.
What is the point of that array when you can call getResult() on response and get response object following Telegram API docs?
@redflasher Just wondering where you get the reflect() method from. And also the Request class can't be instantiated like that. Did you test that code and does it work for you? :thinking:
@noplanman I just add "longman/telegram-bot": "*", into _composer.json_ some days ago and thats all.
And in _vendor/longman/telegram-bot/src/Entities/Entity.php_
finded
...
public function reflect($object = null)
....
Its work for me.
Now it look like:
`
// Create Telegram API object
$telegram = new LongmanTelegramBotTelegram($token, 'testname');
$r = new \Longman\TelegramBot\Request($telegram);
$response = $r::getMe();
if($response->isOk())
{
$model->name = $response->reflect()['result']['username'];`
Ah, you must have an old version of the library then!
The reflect method got removed in this commit: https://github.com/php-telegram-bot/core/commit/6009277bc1fb7704175b8143420f9445413efcce
Could you check what version you have in Telegram.php please?
"longman/telegram-bot": "*"
This is a back practice, you should define the minimum version required and bump it when you use features of newer releases.