diff --git a/README.md b/README.md index 681c37e..5303048 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,15 @@ Message body types and formats [here](https://green-api.com/en/docs/api/receivin This method will be called when an incoming message is received. Next, process messages according to the business logic of your system. +### Sending a message with a poll to a WhatsApp number + +``` +$result = $greenApi->sending->sendPoll('11001234567@c.us', 'Message text', + array(array('optionName'=>'green'), array('optionName'=>'red'), array('optionName'=>'blue'))); +``` + +Example url: [sendPoll.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPoll.php) + ## Examples list | Description | Module | @@ -124,6 +133,7 @@ This method will be called when an incoming message is received. Next, process m | 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) | | Example of incoming webhooks receiving | [receiveNotification.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/receiveNotification.php) | +| Example of sending a message with a poll | [sendPoll.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPoll.php) | ## The full list of the library methods @@ -166,6 +176,7 @@ This method will be called when an incoming message is received. Next, process m | `sending.sendContact` | The method is for sending a message with a contact | [SendContact](https://green-api.com/en/docs/api/sending/SendContact/) | | `sending.sendLink` | The method is designed to send a message with a link that will add an image preview, title and description | [SendLink](https://green-api.com/en/docs/api/sending/SendLink/) | | `sending.forwardMessages` | The method is designed for forwarding messages to a personal or group chat | [ForwardMessages](https://green-api.com/en/docs/api/sending/ForwardMessages/) | +| `sending.sendPoll` | The method is designed for sending message with a poll to a personal or group | [SendPoll](https://green-api.com/en/docs/api/sending/SendPoll/) | | `serviceMethods.checkWhatsapp` | The method checks if there is a WhatsApp account on the phone number | [CheckWhatsapp](https://green-api.com/en/docs/api/service/CheckWhatsapp/) | | `serviceMethods.getAvatar` | The method returns the avatar of the correspondent or group chat | [GetAvatar](https://green-api.com/en/docs/api/service/GetAvatar/) | | `serviceMethods.getContacts` | The method is designed to get a list of contacts of the current account | [GetContacts](https://green-api.com/en/docs/api/service/GetContacts/) | diff --git a/README_RUS.md b/README_RUS.md index a838e00..a7953aa 100644 --- a/README_RUS.md +++ b/README_RUS.md @@ -113,6 +113,15 @@ body | тело сообщения (json) Этот метод будет вызываться при получении входящего сообщения. Далее обрабатываете сообщения согласно бизнес-логике вашей системы. +### Отправка сообщения с опросом на номер WhatsApp + +``` +$result = $greenApi->sending->sendPoll('11001234567@c.us', 'Message text', + array(array('optionName'=>'green'), array('optionName'=>'red'), array('optionName'=>'blue'))); +``` + +Ссылка на пример: [sendPoll.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPoll.php) + ## Список примеров | Описание | Модуль | @@ -122,6 +131,7 @@ body | тело сообщения (json) | Пример отправки картинки загрузкой с диска | [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) | | Пример получения входящих уведомлений | [receiveNotification.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/receiveNotification.php) | +| Пример отправки сообщения с опросом | [sendPoll.php](https://github.com/green-api/whatsapp-api-client-php/blob/master/examples/sendPoll.php) | ## Полный список методов библиотеки @@ -164,6 +174,7 @@ body | тело сообщения (json) | `sending.sendContact` | Метод предназначен для отправки сообщения с контактом | [SendContact](https://green-api.com/docs/api/sending/SendContact/) | | `sending.sendLink` | Метод предназначен для отправки сообщения со ссылкой, по которой будут добавлены превью изображения, заголовок и описание | [SendLink](https://green-api.com/docs/api/sending/SendLink/) | | `sending.forwardMessages` | Метод предназначен для пересылки сообщений в личный или групповой чат | [ForwardMessages](https://green-api.com/docs/api/sending/ForwardMessages/) | +| `sending.sendPoll` | Метод предназначен для отправки сообщения с опросом в личный или групповой | [SendPoll](https://green-api.com/docs/api/sending/SendPoll/) | | `serviceMethods.checkWhatsapp` | Метод проверяет наличие аккаунта WhatsApp на номере телефона | [CheckWhatsapp](https://green-api.com/docs/api/service/CheckWhatsapp/) | | `serviceMethods.getAvatar` | Метод возвращает аватар корреспондента или группового чата | [GetAvatar](https://green-api.com/docs/api/service/GetAvatar/) | | `serviceMethods.getContacts` | Метод предназначен для получения списка контактов текущего аккаунта | [GetContacts](https://green-api.com/docs/api/service/GetContacts/) | diff --git a/examples/sendPoll.php b/examples/sendPoll.php new file mode 100644 index 0000000..5eabc7a --- /dev/null +++ b/examples/sendPoll.php @@ -0,0 +1,14 @@ +sending->sendPoll('11001234567@c.us', 'Please choose the color:', + array(array('optionName'=>'green'), array('optionName'=>'red'), array('optionName'=>'blue'))); + +print_r($result->data); diff --git a/src/tools/Account.php b/src/tools/Account.php index f72305d..d7c614c 100644 --- a/src/tools/Account.php +++ b/src/tools/Account.php @@ -5,27 +5,30 @@ use GreenApi\RestApi\GreenApiClient; use stdClass; -class Account { - private $greenApi; - - /** - * @param GreenApiClient $greenApi - */ - public function __construct( GreenApiClient $greenApi ) { - $this->greenApi = $greenApi; - } - - /** - * The method is aimed for getting the current account settings. - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/GetSettings/ - */ - public function getSettings(): stdClass { - - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/GetSettings/{{apiTokenInstance}}' ); - } +class Account +{ + private $greenApi; + + /** + * @param GreenApiClient $greenApi + */ + public function __construct(GreenApiClient $greenApi) + { + $this->greenApi = $greenApi; + } + + /** + * The method is aimed for getting the current account settings. + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/GetSettings/ + */ + public function getSettings(): stdClass + { + + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/GetSettings/{{apiTokenInstance}}'); + } /** * The method is aimed for getting the current account settings. @@ -48,89 +51,96 @@ public function getWaSettings(): stdClass { */ public function getStateInstance(): stdClass { - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/GetStateInstance/{{apiTokenInstance}}' ); - } - - /** - * The method is aimed for getting the status of the account instance socket connection with WhatsApp. - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/GetStatusInstance/ - */ - public function getStatusInstance(): stdClass { - - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/GetStatusInstance/{{apiTokenInstance}}' ); - } - - /** - * The method is aimed for logging out an account. - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/Logout/ - */ - public function logout(): stdClass { - - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/Logout/{{apiTokenInstance}}' ); - } - - /** - * The method is aimed for getting QR code. To authorize your account, you have to scan a QR code from - * application WhatsApp Business on your phone. You can also get a QR code and authorize your account in your profile. - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/QR/ - */ - public function qr(): stdClass { - - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/QR/{{apiTokenInstance}}' ); - } - - /** - * The method is aimed for rebooting an account. - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/Reboot/ - */ - public function reboot(): stdClass { - - return $this->greenApi->request( 'GET', - '{{host}}/waInstance{{idInstance}}/Reboot/{{apiTokenInstance}}' ); - } - - - /** - * The method is aimed for setting an account picture. - * - * @param string $path - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/SetProfilePicture/ - */ - public function setProfilePicture( string $path ): stdClass { - - $requestBody = [ - 'file' => curl_file_create( $path ), - ]; - $requestBody['file']->mime = 'image/jpeg'; - - return $this->greenApi->request( 'POST', - '{{host}}/waInstance{{idInstance}}/SetProfilePicture/{{apiTokenInstance}}', $requestBody, true ); - } - - /** - * The method is aimed for setting account settings. When this method is requested, the account is rebooted. - * - * @param array $requestBody - * - * @return stdClass - * @link https://green-api.com/en/docs/api/account/SetSettings/ - */ - public function setSettings( array $requestBody ): stdClass { - - return $this->greenApi->request( 'POST', - '{{host}}/waInstance{{idInstance}}/SetSettings/{{apiTokenInstance}}', $requestBody ); - } + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/GetStateInstance/{{apiTokenInstance}}'); + } + + /** + * The method is aimed for getting the status of the account instance socket connection with WhatsApp. + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/GetStatusInstance/ + */ + public function getStatusInstance(): stdClass + { + + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/GetStatusInstance/{{apiTokenInstance}}'); + } + + /** + * The method is aimed for logging out an account. + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/Logout/ + */ + public function logout(): stdClass + { + + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/Logout/{{apiTokenInstance}}'); + } + + /** + * The method is aimed for getting QR code. To authorize your account, you have to scan a QR code from + * application WhatsApp Business on your phone. You can also get a QR code and authorize your account in your profile. + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/QR/ + */ + public function qr(): stdClass + { + + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/QR/{{apiTokenInstance}}'); + } + + /** + * The method is aimed for rebooting an account. + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/Reboot/ + */ + public function reboot(): stdClass + { + + return $this->greenApi->request('GET', + '{{host}}/waInstance{{idInstance}}/Reboot/{{apiTokenInstance}}'); + } + + + /** + * The method is aimed for setting an account picture. + * + * @param string $path + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/SetProfilePicture/ + */ + public function setProfilePicture(string $path): stdClass + { + + $requestBody = [ + 'file' => curl_file_create($path), + ]; + $requestBody['file']->mime = 'image/jpeg'; + + return $this->greenApi->request('POST', + '{{host}}/waInstance{{idInstance}}/SetProfilePicture/{{apiTokenInstance}}', $requestBody, true); + } + + /** + * The method is aimed for setting account settings. When this method is requested, the account is rebooted. + * + * @param array $requestBody + * + * @return stdClass + * @link https://green-api.com/en/docs/api/account/SetSettings/ + */ + public function setSettings(array $requestBody): stdClass + { + + return $this->greenApi->request('POST', + '{{host}}/waInstance{{idInstance}}/SetSettings/{{apiTokenInstance}}', $requestBody); + } +} \ No newline at end of file diff --git a/src/tools/Sending.php b/src/tools/Sending.php index e65c437..851ae30 100644 --- a/src/tools/Sending.php +++ b/src/tools/Sending.php @@ -413,4 +413,42 @@ public function uploadFile( return $this->greenApi->request( 'POST_BINARY', '{{media}}/waInstance{{idInstance}}/UploadFile/{{apiTokenInstance}}', null, false, null, $path ); } + + /** + * The method is aimed for sending a message with poll to a personal or a group chat. + * The message will be added to the send queue. Checking whatsapp authorization on the phone (i.e. availability in + * linked devices) is not performed. The message will be kept for 24 hours in the queue and will be sent immediately + * after phone authorization. The rate at which messages are sent from the queue is managed by Message sending delay + * parameter. + * + * @param string $chatId + * @param string $message + * @param array $options + * @param bool $multipleAnswers + * @param string $quotedMessageId + * + * @return stdClass + * @link https://green-api.com/en/docs/api/sending/SendPoll/ + */ + public function sendPoll( + string $chatId, string $message, array $options, bool $multipleAnswers = false, + string $quotedMessageId = null + ): stdClass { + + $requestBody = [ + 'chatId' => $chatId, + 'message' => $message, + 'options' => $options, + ]; + + if ( $multipleAnswers ) { + $requestBody['multipleAnswers'] = $multipleAnswers; + } + if ( $quotedMessageId ) { + $requestBody['quotedMessageId'] = $quotedMessageId; + } + + return $this->greenApi->request( 'POST', + '{{host}}/waInstance{{idInstance}}/SendPoll/{{apiTokenInstance}}', $requestBody ); + } }