diff --git a/classes/Commands/GenericmessageCommand.php b/classes/Commands/GenericmessageCommand.php index 6e8b5d1..c80c493 100644 --- a/classes/Commands/GenericmessageCommand.php +++ b/classes/Commands/GenericmessageCommand.php @@ -167,6 +167,54 @@ private function processVoice($fileId, $chat, $tBot) { } } + private function appendCaptionToFileEmbed($message, $fileEmbed) + { + $caption = ''; + + if (method_exists($message, 'getProperty')) { + $caption = trim((string)$message->getProperty('caption', '')); + } + + if ($caption === '' && isset($message->raw_data) && is_array($message->raw_data) && isset($message->raw_data['caption'])) { + $caption = trim((string)$message->raw_data['caption']); + } + + if ($caption === '') { + $caption = trim((string)$message->getCaption()); + } + + if ($caption !== '' && trim((string)$fileEmbed) !== '') { + return $caption . ' ' . $fileEmbed; + } + + return $fileEmbed; + } + + private function getAnimationExtension($message) + { + $extension = 'gif'; + $animation = $message->getAnimation(); + + if (is_object($animation)) { + $fileName = method_exists($animation, 'getFileName') ? (string)$animation->getFileName() : ''; + $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); + + if ($fileExtension !== '') { + return $fileExtension; + } + + $mimeType = method_exists($animation, 'getMimeType') ? strtolower((string)$animation->getMimeType()) : ''; + + if ($mimeType === 'video/mp4') { + $extension = 'mp4'; + } elseif ($mimeType === 'image/gif') { + $extension = 'gif'; + } + } + + return $extension; + } + /** * Command execute method * @@ -186,7 +234,7 @@ public function execute(): ServerResponse if ($type === 'message' || $type === 'text') { $text = trim($message->getText(true)); - } elseif ($type === 'photo' || $type === 'video' || $type === 'voice' || $type === 'sticker' || $type === 'document' || $type === 'audio') { + } elseif ($type === 'photo' || $type === 'video' || $type === 'voice' || $type === 'sticker' || $type === 'document' || $type === 'audio' || $type === 'animation') { $text = ''; } elseif ($type === 'location') { $text = 'https://www.google.com/maps/@'.$message->getLocation()->getLatitude().','.$message->getLocation()->getLongitude().',15z'; @@ -210,19 +258,21 @@ public function execute(): ServerResponse if ($chat instanceof \erLhcoreClassModelChat) { if ($type === 'photo') { - $text = $this->processPhoto($chat, $message, $tBot); + $text = $this->appendCaptionToFileEmbed($message, $this->processPhoto($chat, $message, $tBot)); + } elseif ($type === 'animation') { + $text = $this->appendCaptionToFileEmbed($message, $this->processObject($message->getAnimation()->getFileId(), $chat, $tBot, array('ext' => $this->getAnimationExtension($message)))); } elseif ($type === 'document') { - $text = $this->processObject($message->getDocument()->getFileId(), $chat, $tBot); + $text = $this->appendCaptionToFileEmbed($message, $this->processObject($message->getDocument()->getFileId(), $chat, $tBot)); } elseif ($type === 'video') { - $text = $this->processObject($message->getVideo()->getFileId(), $chat, $tBot, array('ext' => 'mp4')); + $text = $this->appendCaptionToFileEmbed($message, $this->processObject($message->getVideo()->getFileId(), $chat, $tBot, array('ext' => 'mp4'))); } elseif ($message->getVideoNote()) { $text = $this->processObject($message->getVideoNote()->getFileId(), $chat, $tBot, array('ext' => 'mp4')); } elseif ($type === 'voice') { - $text = $this->processVoice($message->getVoice()->getFileId(), $chat, $tBot); + $text = $this->appendCaptionToFileEmbed($message, $this->processVoice($message->getVoice()->getFileId(), $chat, $tBot)); } elseif ($type === 'sticker') { $text = $this->processObject($message->getSticker()->getFileId(), $chat, $tBot, array('ext' => 'webp')); } elseif ($type === 'audio') { - $text = $this->processObject($message->getAudio()->getFileId(), $chat, $tBot); + $text = $this->appendCaptionToFileEmbed($message, $this->processObject($message->getAudio()->getFileId(), $chat, $tBot)); } $ignoreMessage = false; diff --git a/doc/telegram/rest-api.json b/doc/telegram/rest-api.json index 5c9329f..ca2a319 100644 --- a/doc/telegram/rest-api.json +++ b/doc/telegram/rest-api.json @@ -1 +1 @@ -{"name":"TelegramIntegration","description":"","configuration":"{\"host\":\"https:\/\/api.telegram.org\",\"ecache\":false,\"parameters\":[{\"method\":\"POST\",\"authorization\":\"\",\"api_key_location\":\"header\",\"query\":[],\"header\":[],\"conditions\":[],\"postparams\":[],\"userparams\":[],\"output\":[{\"key\":\"\",\"value\":\"\",\"id\":\"temp1706685738487\",\"success_name\":\"Success\",\"success_header\":\"200\"}],\"id\":\"temp1695212526903\",\"name\":\"Send\",\"suburl\":\"\/bot{{args.chat.incoming_chat.incoming_dynamic_array.access_token}}\/sendMessage\",\"body_request_type\":\"raw\",\"body_raw\":\"{\\n \\\"chat_id\\\":{{args.chat.incoming_chat.chat_external_id}},\\\"parse_mode\\\":\\\"HTML\\\",\\n \\\"text\\\":{{msg_html_nobr}}\\n{interactive_api}\\n,\\\"reply_markup\\\":{\\n \\\"resize_keyboard\\\":true,\\n\\\"inline_keyboard\\\":[\\n{button_template}\\n [{\\n \\\"text\\\": {{button_title}},\\n \\\"{is_url}url{\/is_url}{is_button}callback_data{\/is_button}\\\":{{button_payload}}\\n }]\\n{\/button_template}\\n]\\n}\\n\\n{\/interactive_api}\\n}\",\"body_request_type_content\":\"json\",\"remote_message_id\":\"result:message_id\",\"suburl_file\":\"\/bot{{args.chat.incoming_chat.incoming_dynamic_array.access_token}}\/{api_by_ext__tgs}sendSticker{\/api_by_ext}{api_by_ext__ogg}sendVoice{\/api_by_ext}{api_by_ext__mp3_m4a}sendAudio{\/api_by_ext}{api_by_ext__mp4}sendVideo{\/api_by_ext}{image_api}sendPhoto{\/image_api}{file_api}sendDocument{\/file_api}\",\"body_raw_file\":\"{\\n \\\"chat_id\\\":{{args.chat.incoming_chat.chat_external_id}},\\n \\\"{api_by_ext__tgs}sticker{\/api_by_ext}{api_by_ext__ogg}voice{\/api_by_ext}{api_by_ext__mp3_m4a}audio{\/api_by_ext}{api_by_ext__mp4}video{\/api_by_ext}{file_api}document{\/file_api}{image_api}photo{\/image_api}\\\":{{file_url}}\\n}\",\"check_not_empty\":\"{{msg_html_nobr}}\",\"suburl_file_convert\":\"tgs,file_api,mp3_m4a,ogg\"}]}"} \ No newline at end of file +{"name":"TelegramIntegration","description":"","configuration":"{\"host\":\"https:\/\/api.telegram.org\",\"ecache\":false,\"parameters\":[{\"method\":\"POST\",\"authorization\":\"\",\"api_key_location\":\"header\",\"query\":[],\"header\":[],\"conditions\":[],\"postparams\":[],\"userparams\":[],\"output\":[{\"key\":\"\",\"value\":\"\",\"id\":\"temp1706685738487\",\"success_name\":\"Success\",\"success_header\":\"200\"}],\"id\":\"temp1695212526903\",\"name\":\"Send\",\"suburl\":\"\/bot{{args.chat.incoming_chat.incoming_dynamic_array.access_token}}\/sendMessage\",\"body_request_type\":\"raw\",\"body_raw\":\"{\\n \\\"chat_id\\\":{{args.chat.incoming_chat.chat_external_id}},\\\"parse_mode\\\":\\\"HTML\\\",\\n \\\"text\\\":{{msg_html_nobr}}\\n{interactive_api}\\n,\\\"reply_markup\\\":{\\n \\\"resize_keyboard\\\":true,\\n\\\"inline_keyboard\\\":[\\n{button_template}\\n [{\\n \\\"text\\\": {{button_title}},\\n \\\"{is_url}url{\/is_url}{is_button}callback_data{\/is_button}\\\":{{button_payload}}\\n }]\\n{\/button_template}\\n]\\n}\\n\\n{\/interactive_api}\\n}\",\"body_request_type_content\":\"json\",\"remote_message_id\":\"result:message_id\",\"suburl_file\":\"\/bot{{args.chat.incoming_chat.incoming_dynamic_array.access_token}}\/{api_by_ext__tgs}sendSticker{\/api_by_ext}{api_by_ext__ogg}sendVoice{\/api_by_ext}{api_by_ext__mp3_m4a}sendAudio{\/api_by_ext}{api_by_ext__mp4}sendVideo{\/api_by_ext}{image_api}sendPhoto{\/image_api}{file_api}sendDocument{\/file_api}\",\"body_raw_file\":\"{\\n \\\"chat_id\\\":{{args.chat.incoming_chat.chat_external_id}},\\n \\\"{api_by_ext__tgs}sticker{\/api_by_ext}{api_by_ext__ogg}voice{\/api_by_ext}{api_by_ext__mp3_m4a}audio{\/api_by_ext}{api_by_ext__mp4}video{\/api_by_ext}{file_api}document{\/file_api}{image_api}photo{\/image_api}\\\":{{file_url}}\\n{api_by_ext__ogg},\\\"caption\\\":{{msg_clean}}{\/api_by_ext}{api_by_ext__mp3_m4a},\\\"caption\\\":{{msg_clean}}{\/api_by_ext}{api_by_ext__mp4},\\\"caption\\\":{{msg_clean}}{\/api_by_ext}{file_api},\\\"caption\\\":{{msg_clean}}{\/file_api}{image_api},\\\"caption\\\":{{msg_clean}}{\/image_api}\\n}\",\"check_not_empty\":\"{{msg_html_nobr}}\",\"suburl_file_convert\":\"tgs,file_api,mp3_m4a,ogg\",\"suburl_file_skip_ext\":\"tgs\"}]}"} \ No newline at end of file