Skip to content

SW-2544: Added sendPoll request and example #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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

Expand Down Expand Up @@ -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/) |
Expand Down
11 changes: 11 additions & 0 deletions README_RUS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## Список примеров

| Описание | Модуль |
Expand All @@ -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) |

## Полный список методов библиотеки

Expand Down Expand Up @@ -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/) |
Expand Down
14 changes: 14 additions & 0 deletions examples/sendPoll.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require './vendor/autoload.php';

use GreenApi\RestApi\GreenApiClient;

define( "ID_INSTANCE", "1101712345" );
define( "API_TOKEN_INSTANCE", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" );

$greenApi = new GreenApiClient(ID_INSTANCE, API_TOKEN_INSTANCE);

$result = $greenApi->sending->sendPoll('11001234567@c.us', 'Please choose the color:',
array(array('optionName'=>'green'), array('optionName'=>'red'), array('optionName'=>'blue')));

print_r($result->data);
224 changes: 117 additions & 107 deletions src/tools/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
38 changes: 38 additions & 0 deletions src/tools/Sending.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}