I am working with a framework to build a telegram bot, since I am not a pro in PHP, I am trying to understand the logic behind the codes; there is one thing I cannot understand. then, I decided to ask it here. The code is much bigger; so I cut some parts of it. I have a specific problem with this method. I don't exactly get how they specified a method for getMessage(). It didn't have any functions with this name in this class.
<?php
/**
* Class Command
*
* Base class for commands. It includes some helper methods that can fetch data directly from the Update object.
*
* @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc.
abstract class Command
{
/**
* Constructor
*
* @param Telegram $telegram
* @param Update|null $update
*/
public function __construct(Telegram $telegram, ?Update $update = null)
{
$this->telegram = $telegram;
if ($update !== null) {
$this->setUpdate($update);
}
$this->config = $telegram->getCommandConfig($this->name);
}
/**
* Set update object
*
* @param Update $update
*
* @return Command
*/
public function setUpdate(Update $update): Command
{
$this->update = $update;
return $this;
}
/**
* Pre-execute command
*
* @return ServerResponse
* @throws TelegramException
*/
public function preExecute(): ServerResponse
{
if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) {
return $this->executeNoDb();
}
if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) {
$message = $this->getMessage();
if ($user = $message->getFrom()) {
return Request::sendMessage([
'chat_id' => $user->getId(),
'parse_mode' => 'Markdown',
'text' => sprintf(
"/%s command is only available in a private chat.\n(`%s`)",
$this->getName(),
$message->getText()
),
]);
}
return Request::emptyResponse();
}
I just don't get it how this->getMessage()
is working. Is there somebody help me out?
This is a base class, and some other class is extending this class. Since this is an abstract class, it cannot be instantiated.
So you need to look for another class that extends this class (i.e class SomeCommand extends Command
). The SomeCommand class in this case will declare the getMessage function.
(Note that SomeCommand is an example, I don't know the real name of the other class.)
Edit: That function was removed in this commit in favor of using the magic method __call instead.