[go: up one dir, main page]

Skip to content

How to create a group and send a message to it#

Installation#

Before adding the green-api package, you need to install composer (php dependency manager).

composer require green-api/whatsapp-api-client-php

Import#

require './vendor/autoload.php';

Examples#

You may see the full example at: createGroupAndSendMessage.php

How to initialize an object#

$greenApi = new GreenApiClient( ID_INSTANCE, API_TOKEN_INSTANCE );

Please note that keys can be obtained from environment variables:

<?php
require './vendor/autoload.php';

define( "ID_INSTANCE", getenv("ID_INSTANCE" ));
define( "API_TOKEN_INSTANCE", getenv("API_TOKEN_INSTANCE") );

How to create a group and send a message to it#

IMPORTANT:

If one tries to create a group with a non-existent number, WhatsApp may block the sender's number. The number in the example is non-existent.

$chatIds = [
    '11001234567@c.us'
];
$resultCreate = $greenApi->groups->createGroup('GroupName', $chatIds );

if ($resultCreate->code == 200)
    $resultSend = $greenApi->sending->sendMessage($resultCreate->data->chatId, 
        'Message text');

Running index.php#

php -S localhost:8080

The full list of examples#

Description Module
Example of sending text sendTextMessage.php
Example of sending a picture by URL sendPictureByLink.php
Example of sending a picture by uploading from the disk sendPictureByUpload.php
Example of a group creation and sending a message to the group createGroupAndSendMessage.php
Example of incoming webhooks receiving receiveNotification.php