Contact support:
Contact support
Select Language Русский English

    Examples of message sending requests

    In the API, you can send messages of the following types: text and file. For WABA (WhatsApp Business API), template messages are available: template text, template file. For better understanding, let’s look at several message sending options:
    Note: All request examples are written in PHP using the Guzzle HTTP client.

    Sending a message of type 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()]);
    }

    Sending a message of type text with buttons:

    $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()]);
    }
    Sending a message of type file via a public file URL:
    $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'; // public file link
    $fileName = 'sample-green-400x300.png'; // file name must include extension
    $caption = 'Test'; // caption is optional
    
    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()]);
    }
    Sending a message of type file via multipart/form-data:
    $client = new GuzzleHttpClient();
    $licenseId = 12345;
    $messengerType = 'grWhatsApp';
    $chatId = '70000000000'; // phone or chatId
    $accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
    
    $fileName = 'logo-64.png'; // file name must include extension
    $caption = 'Test'; // caption is optional
    $filePath = '/source/public/images/logo-64.png'; // file path on disk
    
    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()]);
    }
    Sending a message of type 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()]);
    }
    Sending a message of type template file via a public file URL:
    $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()]);
    }
    Sending a message of type template file via 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'; // file name must include extension
    $filePath = '/source/public/images/logo-64.png'; // file path on disk
    
    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()]);
    }