Sending messages

Sending messages

You can send text and file messages to the API. Template messages are available for WABA (WhatsApp Business API): template text, template file. For a better understanding, consider several options for sending messages:

Note: All request execution examples are in PHP using the Guzzle http-client.

  1. Sending a message of type text:
$client = new \GuzzleHttp\Client();
$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()]);
}

2. Sending a message of type file via a public link to a file:

$client = new \GuzzleHttp\Client();
$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'; // file name must be with extension
$caption = 'Test'; // file signature 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()]);
}

3. Sending a file type message via multipart/form-data:

$client = new \GuzzleHttp\Client();
$licenseId = 12345;
$messengerType = 'grWhatsApp';
$chatId = '70000000000'; // phone or chatId
$accessToken = '$2y$10$loFiiY4XsYIDv5lfuJ9qROnpfxe000fsvjR/F1qWYiTgJEXaDiMfa';
$fileName = 'logo-64.png'; // file name must be with extension
$caption = 'Test'; // file signature optional
$filePath = '/source/public/images/logo-64.png'; // path to file 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()]);
}

4. Sending a template text message:

$client = new \GuzzleHttp\Client();
$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()]);
}

5. Sending a template file message via a public link to the file:

$client = new \GuzzleHttp\Client();
$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()]);
}

6. Sending a template file message via multipart/form-data:

$client = new \GuzzleHttp\Client();
$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 be with extension
$filePath = '/source/public/images/logo-64.png'; // path to file 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()]);
}

 

Help from integrators