Связаться с поддержкой:
Связаться с поддержкой
Выберите язык Русский Английский (статья не переведена)

    Примеры запросов отправки сообщений

    В API можно отправлять сообщения типов text и file. Для WABA (WhatsApp Business API) доступны шаблонные сообщения: template texttemplate file. Для лучшего понимания, рассмотрим несколько вариантов отправки сообщений:

    Примечание: Все примеры выполнения запросов приведены на языке PHP с использованием Guzzle http-client.

    Отправка сообщения типа text:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/text",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'json' => [
                    'text' => 'Hello world!',
                ],
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }

    Отправка сообщения типа text с кнопками:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/text",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'json' => [
                    'text' => 'Hello world!',
                    'buttons' => [
                        'items' => [
                            [
                                'text' => 'Button 1'
                            ],
                            [
                                'text' => 'Button 2'
                            ]
                        ]
                    ],
                ],
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }

    Отправка сообщения типа file через публичную ссылку на файл:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    $fileLink = 'https://download.samplelib.com/png/sample-green-400x300.png'; // публичная ссылка на файл
    $fileName = 'sample-green-400x300.png'; // имя файла обязательно с расширением
    $caption = 'Test'; // подпись к файлу необязательно
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/file",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'json' => [
                    'file' => $fileLink,
                    'fileName' => $fileName,
                    'caption' => $caption
                ],
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }
    

    Отправка сообщения типа file через multipart/form-data:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    $fileName = 'logo-64.png'; // имя файла обязательно с расширением
    $caption = 'Test'; // подпись к файлу необязательно
    $filePath = '/source/public/images/logo-64.png'; // путь до файла на диске
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/file",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'multipart' => [
                    [
                        'name' => 'fileName',
                        'contents' => $fileName
                    ],
                    [
                        'name' => 'file',
                        'contents' => fopen($filePath, 'r'),
                        'filename' => $fileName
                    ],
                    [
                        'name' => 'caption',
                        'contents' => $caption
                    ]
                ]
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }
    

    Отправка сообщения типа template text:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    $templateId = '0c20b30c-2b92-436d-a6b1-4a1b8f9c0614';
    $templateParams = ['test 1', 'test 2', 'test 3'];
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/template",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'json' => [
                    'template' => [
                        'id' => $templateId,
                        'params' => $templateParams,
                    ]
                ],
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }
    

    Отправка сообщения типа template file через публичную ссылку на файл:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    $templateId = '33cd9d37-4375-4f7f-838c-228c1be32299';
    $templateParams = [];
    $fileLink = 'https://download.samplelib.com/png/sample-green-400x300.png';
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/template",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'json' => [
                    'template' => [
                        'id' => $templateId,
                        'params' => $templateParams,
                    ],
                    'file' => $fileLink
                ],
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }
    

    Отправка сообщения типа template file через multipart/form-data:

    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    $templateId = '33cd9d37-4375-4f7f-838c-228c1be32299';
    $templateParams = [];
    $fileName = 'logo-64.png'; // имя файла обязательно с расширением
    $filePath = '/source/public/images/logo-64.png'; // путь до файла на диске
    try {
        $response = $client->post(
            "https://api.chatapp.online/v1/licenses/$licenseId/messengers/$messengerType/chats/$chatId/messages/template",
            [
                'headers' => [
                    'Authorization' => $accessToken,
                ],
                'multipart' => [
                    [
                        'name' => 'template[id]',
                        'contents' => $templateId
                    ],
                    [
                        'name' => 'file',
                        'contents' => fopen($filePath, 'r'),
                        'filename' => $fileName
                    ],
                    /*[
                        'name' => 'template[params][0]',
                        'contents' => 'Test'
                    ],
                    [
                        'name' => 'template[params][1]',
                        'contents' => 'Test2'
                    ],*/
                ]
            ]
        );
        $body = $response->getBody();
        echo '<pre>';
        print_r(json_decode((string)$body));
    } catch (Exception $e) {
        echo '<pre>';
        print_r([$e->getCode(), $e->getMessage()]);
    }