From 017c13a85cd1f405afddfb340af96d6ea871e457 Mon Sep 17 00:00:00 2001 From: kolcfa Date: Fri, 5 Jun 2026 17:08:31 +0500 Subject: [PATCH] feat: add sendTyping and typingTime support --- README.md | 18 +++++++++++++++ README_RUS.md | 18 +++++++++++++++ examples/sendTyping.php | 17 ++++++++++++++ src/tools/Sending.php | 45 ++++++++++++++++++++++++++++++------ src/tools/ServiceMethods.php | 28 ++++++++++++++++++++++ 5 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 examples/sendTyping.php diff --git a/README.md b/README.md index 8db2746..5e300e1 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,22 @@ define( "ID_INSTANCE", getenv("ID_INSTANCE" )); define( "API_TOKEN_INSTANCE", getenv("API_TOKEN_INSTANCE") ); ``` +### Sending a typing notification + +``` +$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000); +``` + +Example url: [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) + +### Sending an audio recording notification + +``` +$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000, 'recording'); +``` + +Example url: [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) + ### Sending an image via URL ``` @@ -120,6 +136,7 @@ This method will be called when an incoming message is received. Next, process m | Description | Module | |----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------| | Example of sending text | [sendTextMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTextMessage.php) | +| Example of sending typing or audio recording notification | [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) | | Example of sending a picture by URL | [sendPictureByLink.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByLink.php) | | Example of sending a picture by uploading from the disk | [sendPictureByUpload.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByUpload.php) | | Example of a group creation and sending a message to the group | [createGroupAndSendMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/createGroupAndSendMessage.php) | @@ -179,6 +196,7 @@ This method will be called when an incoming message is received. Next, process m | `serviceMethods.archiveChat` | The method archives the chat | [ArchiveChat](https://green-api.com/en/docs/api/service/archiveChat/) | | `serviceMethods.unarchiveChat` | The method unarchives the chat | [UnarchiveChat](https://green-api.com/en/docs/api/service/unarchiveChat/) | | `serviceMethods.setDisappearingChat` | The method is designed to change the settings of disappearing messages in chats | [SetDisappearingChat](https://green-api.com/en/docs/api/service/SetDisappearingChat/) | +| `serviceMethods.sendTyping` | The method is designed to send a typing or audio recording notification to a chat | [SendTyping](https://green-api.com/en/docs/api/service/SendTyping/) | | `webhooks.startReceivingNotifications` | The method is designed to start receiving new notifications | | | `webhooks.stopReceivingNotifications` | The method is designed to stop receiving new notifications | | | `statuses.sendTextStatus` | The method is aimed for sending a text status | [SendTextStatus](https://green-api.com/en/docs/api/statuses/SendTextStatus/) | diff --git a/README_RUS.md b/README_RUS.md index cb25bd4..2d13eac 100644 --- a/README_RUS.md +++ b/README_RUS.md @@ -56,6 +56,22 @@ define( "ID_INSTANCE", getenv("ID_INSTANCE" )); define( "API_TOKEN_INSTANCE", getenv("API_TOKEN_INSTANCE") ); ``` +### Отправка уведомления о наборе текста + +``` +$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000); +``` + +Ссылка на пример: [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) + +### Отправка уведомления о записи аудио + +``` +$result = $greenApi->serviceMethods->sendTyping('11001234567@c.us', 5000, 'recording'); +``` + +Ссылка на пример: [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) + ### Отправка картинки по URL ``` @@ -118,6 +134,7 @@ body | тело сообщения (json) | Описание | Модуль | |------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------| | Пример отправки текста | [sendTextMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTextMessage.php) | +| Пример отправки уведомления о наборе текста или записи аудио | [sendTyping.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendTyping.php) | | Пример отправки картинки по URL | [sendPictureByLink.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByLink.php) | | Пример отправки картинки загрузкой с диска | [sendPictureByUpload.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPictureByUpload.php) | | Пример создание группы и отправка сообщения в группу | [createGroupAndSendMessage.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/createGroupAndSendMessage.php) | @@ -177,6 +194,7 @@ body | тело сообщения (json) | `serviceMethods.archiveChat` | Метод архивирует чат | [ArchiveChat](https://green-api.com/docs/api/service/archiveChat/) | | `serviceMethods.unarchiveChat` | Метод разархивирует чат | [UnarchiveChat](https://green-api.com/docs/api/service/unarchiveChat/) | | `serviceMethods.setDisappearingChat` | Метод предназначен для изменения настроек исчезающих сообщений в чатах | [SetDisappearingChat](https://green-api.com/docs/api/service/SetDisappearingChat/) | +| `serviceMethods.sendTyping` | Метод предназначен для отправки уведомления о наборе текста или записи аудио в чат | [SendTyping](https://green-api.com/docs/api/service/SendTyping/) | | `webhooks.startReceivingNotifications` | Метод предназначен для старта получения новых уведомлений | | | `webhooks.stopReceivingNotifications` | Метод предназначен для остановки получения новых уведомлений | | | `statuses.sendTextStatus` | Метод предназначен для отправки текстового статуса | [SendTextStatus](https://green-api.com/docs/api/statuses/SendTextStatus/) | diff --git a/examples/sendTyping.php b/examples/sendTyping.php new file mode 100644 index 0000000..89ebb10 --- /dev/null +++ b/examples/sendTyping.php @@ -0,0 +1,17 @@ +serviceMethods->sendTyping( + '11001234567@c.us', + 5000, + 'recording' +); + +print_r( $result ); \ No newline at end of file diff --git a/src/tools/Sending.php b/src/tools/Sending.php index e65c437..be7034b 100644 --- a/src/tools/Sending.php +++ b/src/tools/Sending.php @@ -69,7 +69,7 @@ public function sendButtons( * @return stdClass * @link https://green-api.com/en/docs/api/sending/SendContact/ */ - public function sendContact( string $chatId, array $contact, string $quotedMessageId = null ): stdClass { + public function sendContact( string $chatId, array $contact, string $quotedMessageId = null, ?int $typingTime = null ): stdClass { $requestBody = [ 'chatId' => $chatId, @@ -80,6 +80,10 @@ public function sendContact( string $chatId, array $contact, string $quotedMessa $requestBody['quotedMessageId'] = $quotedMessageId; } + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + return $this->greenApi->request( 'POST', '{{host}}/waInstance{{idInstance}}/SendContact/{{apiTokenInstance}}', $requestBody ); } @@ -101,7 +105,7 @@ public function sendContact( string $chatId, array $contact, string $quotedMessa * @link https://green-api.com/en/docs/api/sending/SendFileByUpload/ */ public function sendFileByUpload( - string $chatId, string $path, string $fileName = null, string $caption = null, string $quotedMessageId = null + string $chatId, string $path, string $fileName = null, string $caption = null, string $quotedMessageId = null, ?int $typingTime = null, ?string $typingType = null ): stdClass { if ( ! $fileName ) { @@ -122,6 +126,14 @@ public function sendFileByUpload( $requestBody['quotedMessageId'] = $quotedMessageId; } + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + + if ( $typingType !== null ) { + $requestBody['typingType'] = $typingType; + } + return $this->greenApi->request( 'POST', '{{media}}/waInstance{{idInstance}}/SendFileByUpload/{{apiTokenInstance}}', $requestBody, true ); } @@ -146,7 +158,7 @@ public function sendFileByUpload( */ public function sendFileByUrl( string $chatId, string $urlFile, string $fileName = null, string $caption = null, string $quotedMessageId = null, - bool $archiveChat = false + bool $archiveChat = false, ?int $typingTime = null, ?string $typingType = null ): stdClass { if ( ! $fileName ) { @@ -171,6 +183,14 @@ public function sendFileByUrl( $requestBody['archiveChat'] = $archiveChat; } + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + + if ( $typingType !== null ) { + $requestBody['typingType'] = $typingType; + } + return $this->greenApi->request( 'POST', '{{host}}/waInstance{{idInstance}}/SendFileByUrl/{{apiTokenInstance}}', $requestBody ); } @@ -271,8 +291,8 @@ public function sendListMessage( * @link https://green-api.com/en/docs/api/sending/SendLocation/ */ public function sendLocation( - string $chatId, float $latitude, float $longitude, string $nameLocation = null, string $address = null, - string $quotedMessageId = null + string $chatId, float $latitude, float $longitude, string $nameLocation = null, string $address = null, + string $quotedMessageId = null, ?int $typingTime = null ): stdClass { $requestBody = [ @@ -290,6 +310,9 @@ public function sendLocation( if ( $quotedMessageId ) { $requestBody['quotedMessageId'] = $quotedMessageId; } + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } return $this->greenApi->request( 'POST', '{{host}}/waInstance{{idInstance}}/SendLocation/{{apiTokenInstance}}', $requestBody ); @@ -311,7 +334,7 @@ public function sendLocation( * @link https://green-api.com/en/docs/api/sending/SendMessage/ */ public function sendMessage( - string $chatId, string $message, string $quotedMessageId = null, bool $archiveChat = false + string $chatId, string $message, string $quotedMessageId = null, bool $archiveChat = false, ?int $typingTime = null ): stdClass { $requestBody = [ @@ -327,6 +350,10 @@ public function sendMessage( $requestBody['archiveChat'] = $archiveChat; } + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + return $this->greenApi->request( 'POST', '{{host}}/waInstance{{idInstance}}/SendMessage/{{apiTokenInstance}}', $requestBody ); } @@ -387,7 +414,7 @@ public function sendTemplateButtons( * @return stdClass * @link https://green-api.com/en/docs/api/sending/ForwardMessages/ */ - public function forwardMessages( string $chatId, string $chatIdFrom, array $messages ): stdClass { + public function forwardMessages( string $chatId, string $chatIdFrom, array $messages, ?int $typingTime = null ): stdClass { $requestBody = [ 'chatId' => $chatId, @@ -395,6 +422,10 @@ public function forwardMessages( string $chatId, string $chatIdFrom, array $mess 'messages' => $messages ]; + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + return $this->greenApi->request( 'POST', '{{host}}/waInstance{{idInstance}}/ForwardMessages/{{apiTokenInstance}}', $requestBody ); } diff --git a/src/tools/ServiceMethods.php b/src/tools/ServiceMethods.php index bcd961f..af11bc4 100644 --- a/src/tools/ServiceMethods.php +++ b/src/tools/ServiceMethods.php @@ -107,6 +107,34 @@ public function archiveChat( string $chatId ): stdClass { } + /** + * The method is intended for sending a typing or audio recording notification to a chat. + * + * @param string $chatId + * @param int|null $typingTime + * @param string|null $typingType + * + * @return stdClass + * @link https://green-api.com/en/docs/api/service/SendTyping/ + */ + public function sendTyping( string $chatId, ?int $typingTime = null, ?string $typingType = null ): stdClass { + + $requestBody = [ + 'chatId' => $chatId, + ]; + + if ( $typingTime !== null ) { + $requestBody['typingTime'] = $typingTime; + } + + if ( $typingType !== null ) { + $requestBody['typingType'] = $typingType; + } + + return $this->greenApi->request( 'POST', + '{{host}}/waInstance{{idInstance}}/sendTyping/{{apiTokenInstance}}', $requestBody ); + } + /** * The method deletes a message from a chat. *