Longman\TelegramBot\Entities\Message::getType PHP Method

getType() public method

Detect type based on properties.
public getType ( ) : string | null
return string | null
    public function getType()
    {
        $types = ['text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', 'location', 'venue', 'new_chat_member', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id', 'migrate_from_chat_id', 'pinned_message'];
        foreach ($types as $type) {
            if ($this->getProperty($type)) {
                if ($type === 'text' && $this->getCommand()) {
                    return 'command';
                }
                return $type;
            }
        }
        return 'message';
    }

Usage Example

 /**
  * SendBack
  *
  * Received a message, the bot can send a copy of it to another chat/channel.
  * You don't have to care about the type of the message, the function detect it and use the proper
  * REQUEST:: function to send it.
  * $data include all the var that you need to send the message to the proper chat
  *
  * @todo This method will be moved at an higher level maybe in AdminCommand or Command
  * @todo Looking for a more significative name
  *
  * @param Entities\Message $message
  * @param array            $data
  *
  * @return Entities\ServerResponse
  */
 protected function sendBack(Message $message, array $data)
 {
     $type = $message->getType();
     $type = $type == 'command' ? 'Message' : $type;
     if ($type == 'Message') {
         $data['text'] = $message->getText(true);
     } elseif ($type == 'Audio') {
         $data['audio'] = $message->getAudio()->getFileId();
         $data['duration'] = $message->getAudio()->getDuration();
         $data['performer'] = $message->getAudio()->getPerformer();
         $data['title'] = $message->getAudio()->getTitle();
     } elseif ($type == 'Document') {
         $data['document'] = $message->getDocument()->getFileId();
     } elseif ($type == 'Photo') {
         $data['photo'] = $message->getPhoto()[0]->getFileId();
     } elseif ($type == 'Sticker') {
         $data['sticker'] = $message->getSticker()->getFileId();
     } elseif ($type == 'Video') {
         $data['video'] = $message->getVideo()->getFileId();
     } elseif ($type == 'Voice') {
         $data['voice'] = $message->getVoice()->getFileId();
     } elseif ($type == 'Location') {
         $data['latitude'] = $message->getLocation()->getLatitude();
         $data['longitude'] = $message->getLocation()->getLongitude();
     }
     $callback_path = 'Longman\\TelegramBot\\Request';
     $callback_function = 'send' . $type;
     if (!method_exists($callback_path, $callback_function)) {
         throw new TelegramException('Methods: ' . $callback_function . ' not found in class Request.');
     }
     return call_user_func_array($callback_path . '::' . $callback_function, [$data]);
 }