PHP code example of brandchat / api-php

1. Go to this page and download the library: Download brandchat/api-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brandchat / api-php example snippets



$apiKey = 'PUT_YOUR_API_KEY_HERE';
$botIdentifier = 'PUT_YOUR_BOT_IDENTIFIER_HERE';

$brandChat = \BrandChatApi\BrandChatApi::instance();
$brandChat->init($apiKey, $botIdentifier);


$brandChat = \BrandChatApi\BrandChatApi::instance();

// create our text handler
$textHandler = function($event) {
    /** @var \BrandChatApi\Event\MessageEvent $event */
    /** @var \BrandChatApi\Message\TextMessage $textMessage */
    $textMessage = $event->getMessage();
    $text = $textMessage->getText(); // the message from the user
    $userId = $textMessage->getUserId(); // the user's unique ID

    // and respond with a message
    $responseMessage = new \BrandChatApi\Message\TextMessage();
    $responseMessage->setText('Hello world!')
                    ->setUserId($userId);

    $event->respond([$responseMessage]);
};

// register our handler for inbound text messages
$brandChat->onMessageText($textHandler);

// and process the inbound events
$brandChat->run();


// create text message
$userId = 1337; // typically obtained from an event (like a message from a user)
$text = 'Hello world!'; // the message you want to send to the user

$textMessage = new \BrandChatApi\Message\TextMessage();
$textMessage->setUserId($userId)->setText($text);
$messageList[] = $textMessage;

// and send it!
$request = new \BrandChatApi\Request\SendMessageListRequest();
$response = $request->setMessageList([$textMessage])->execute();

if ($response->isSuccess()) {
    // Woohoo -- it was sent!
} else {
    // Hmmm, sending failed... Probably a bad key or bot identifier?
    $reason = $response->getReason(); // human readable reason for failure
}


$code = $_GET['code'];
$request = new \BrandChatApi\Request\OneTimeCodeLookupRequest();
$response = $request->setCode($code)->execute();

if ($response->isSuccess()) {
    // You can get various profile fields from the response, as follows:
    $displayName = $response->getUserProfile()->getDisplayName();
    $platformIdentifier = $response->getUserProfile()->getPlatformIdentifier();
    // todo: do something
    echo "<html><body><p>Hello, $displayName!</p></body></html>";
} else {
    die("Oops, we couldn't authenticate you :(");
}
bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
bash
php composer.phar