Installation:
composer require banckle/chat-sdk-php banckle/chat-bundle
Add the bundle to AppKernel.php:
new Banckle\ChatBundle\BanckleChatBundle(),
Configuration:
Add to config.yml:
banckle_chat:
apiKey: "YOUR_API_KEY"
banckleAccountUri: "https://apps.banckle.com/api/v2"
banckleChatUri: "https://chat.banckle.com/v3"
First Use Case: Generate a token for authentication:
$banckleChat = $this->get('bancklechat.api');
$token = $banckleChat->getToken('user@example.com', 'password');
bancklechat.api as a service.BanckleChatBundle/Resources/config/services.yml for service definitions.Authentication Flow:
// Generate token
$token = $this->get('bancklechat.api')->getToken($email, $password);
// Create API instance (e.g., DepartmentsApi)
$departmentsApi = $this->get('bancklechat.api')->createInstance('DepartmentsApi', $token);
// Fetch data
$departments = $departmentsApi->getDepartments();
Dependency Injection: Inject the service directly into controllers/services:
use Banckle\ChatBundle\Service\BanckleChatService;
class ChatController {
public function __construct(private BanckleChatService $banckleChat) {}
public function index() {
$token = $this->banckleChat->getToken('user@example.com', 'password');
// ...
}
}
Token Management: Cache tokens to avoid repeated authentication (e.g., using Symfony’s cache system):
$cache = $this->get('bancklechat.cache');
$token = $cache->get('banckle_token') ?: $this->get('bancklechat.api')->getToken($email, $password);
$cache->set('banckle_token', $token, 3600); // Cache for 1 hour
# config.yml
services:
app.banckle_token_listener:
class: AppBundle\EventListener\BanckleTokenListener
tags:
- { name: kernel.event_listener, event: banckle.token.expired, method: onTokenExpired }
class CustomChatClient extends \Banckle\Chat\Api\ApiClient {
public function fetchUserConversations($token, $userId) {
$conversationsApi = $this->createInstance('ConversationsApi', $token);
return $conversationsApi->getConversations($userId);
}
}
$form = $this->createFormBuilder()
->add('department', EntityType::class, [
'class' => 'AppBundle\Entity\Department',
'choices' => $this->getDepartmentsFromBanckle($token),
])
->getForm();
Token Expiration:
banckle.token.expired.try {
$result = $api->getDepartments($token);
} catch (\Banckle\Chat\Exception\UnauthorizedException $e) {
$token = $this->refreshToken(); // Custom method
return $api->getDepartments($token);
}
Rate Limiting:
use Symfony\Component\Stopwatch\Stopwatch;
$stopwatch = new Stopwatch();
$event = $stopwatch->start('banckle_request');
try {
$result = $api->getData($token);
} catch (\Banckle\Chat\Exception\RateLimitException $e) {
$event->stop();
$duration = $event->getDuration();
sleep($duration * 2); // Exponential backoff
retry;
}
Configuration Overrides:
apiKey or URIs. Use environment variables or parameter bags:
# config/services.yaml
parameters:
banckle.api_key: '%env(BANCKLE_API_KEY)%'
services:
bancklechat.api:
arguments:
$apiKey: '%banckle.api_key%'
Enable SDK Debugging:
Set the debug option in the SDK client to log requests/responses:
$client = new \Banckle\Chat\Api\ApiClient(['debug' => true]);
Common Errors:
apiKey and token generation.banckleChatUri and endpoint paths (e.g., /v3/departments).Custom API Responses: Transform Banckle responses into domain objects:
class BanckleResponseTransformer {
public function transformDepartment(array $data): Department {
return (new Department())
->setId($data['id'])
->setName($data['name']);
}
}
Middleware: Add request/response middleware to the SDK client:
$client->setMiddleware(new class implements \Banckle\Chat\Api\Middleware {
public function handle($request, \Closure $next) {
$request->setHeader('X-Custom-Header', 'value');
return $next($request);
}
});
Testing: Mock the Banckle service in tests:
$this->container->set('bancklechat.api', $this->createMock(BanckleChatService::class));
$mockApi->method('getToken')->willReturn('mock_token');
How can I help you explore Laravel packages today?