baks-dev/products-stocks-telegram
Installation:
composer require baks-dev/products-stocks-telegram
php bin/console baks:assets:install
Run Migrations:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
product_stocks, telegram_notifications) are created.Configure Telegram Bot:
config/products-stocks-telegram.php to set:
bot_token: Your Telegram bot token (from @BotFather).admin_chat_ids: Array of Telegram user IDs to notify (e.g., [12345678, 87654321]).low_stock_threshold: Trigger value (e.g., 5) for stock alerts.First Use Case:
use BaksDev\ProductsStocksTelegram\Services\StockService;
$stockService = app(StockService::class);
$stockService->updateStock($productId, $quantity);
$stockService->checkAndNotifyLowStock();
Stock Management:
StockService facade (or inject StockService) to manage product quantities:
// Update stock via API/CLI
StockService::updateStock($productId, $newQuantity);
// Get stock for a product
$stock = StockService::getStock($productId);
StockService::bulkUpdate([$productId1 => $qty1, $productId2 => $qty2]);
Telegram Notifications:
checkAndNotifyLowStock() via Laravel’s scheduler (app/Console/Kernel.php):
$schedule->command('baks:stocks:check')->dailyAt('9:00');
StockService::notifyAdmin($productId, $message);
Integration with E-Commerce:
order.placed) to deduct stock:
use BaksDev\ProductsStocksTelegram\Listeners\DeductStockOnOrder;
// Register in EventServiceProvider
protected $listen = [
'order.placed' => [DeductStockOnOrder::class],
];
StockService::syncWithInventory() to reconcile stock with a third-party system.API Endpoints:
use BaksDev\ProductsStocksTelegram\Http\Controllers\StockController;
Route::get('/api/stock/{productId}', [StockController::class, 'getStock']);
Telegram Bot Commands:
app/Commands/StockCommands.php):
protected $commands = [
'stock' => 'BaksDev\ProductsStocksTelegram\Commands\StockCommand',
'update' => 'App\Commands\CustomUpdateCommand', // Your custom command
];
Telegram Bot Token Security:
bot_token in config. Use Laravel’s .env:
TELEGRAM_BOT_TOKEN=your_token_here
Race Conditions:
DB::transaction(function () use ($productId, $quantity) {
StockService::updateStock($productId, $quantity);
});
Low Stock Threshold:
stock_threshold column.Timezone Issues:
.env:
APP_TIMEZONE=UTC
Missing Product Handling:
StockService::updateStock() assumes the product exists. Add validation:
if (!$product = Product::find($productId)) {
throw new \InvalidArgumentException("Product not found");
}
Telegram Notifications:
php bin/console baks:telegram:send-test
config/logging.php for baks-dev/products-stocks-telegram.Stock Mismatches:
$stock = StockService::getStock($productId);
$physicalCount = InventoryService::getPhysicalCount($productId);
Command Failures:
try {
StockService::processCommand($update);
} catch (\Exception $e) {
\Log::error("Telegram command failed: " . $e->getMessage());
}
Custom Notifications:
NotificationService to send emails/SMS alongside Telegram:
class CustomNotificationService extends \BaksDev\ProductsStocksTelegram\Services\NotificationService
{
public function sendLowStockAlert($product, $stockLevel)
{
parent::sendLowStockAlert($product, $stockLevel);
Mail::to('admin@example.com')->send(new LowStockMail($product));
}
}
config/products-stocks-telegram.php:
'notification_service' => \App\Services\CustomNotificationService::class,
Additional Stock Sources:
StockSourceInterface:
class ErpStockSource implements StockSourceInterface
{
public function getStock($productId): int
{
return $this->erpClient->fetchStock($productId);
}
}
$this->app->bind(StockSourceInterface::class, ErpStockSource::class);
UI Integration:
php artisan vendor:publish --tag=products-stocks-telegram-views
resources/views/vendor/baks-dev/products-stocks-telegram/.Webhook for Real-Time Updates:
Route::post('/webhook/stock', function (Request $request) {
$data = $request->validate(['product_id' => 'required', 'quantity' => 'required']);
StockService::updateStock($data['product_id'], $data['quantity']);
});
Localization:
php artisan vendor:publish --tag=products-stocks-telegram-lang
resources/lang/en/products-stocks-telegram.php.How can I help you explore Laravel packages today?