I’m working on a Laravel 5.4 project and trying to add custom commands to my Telegram bot. I’ve set up the Telegram Bot SDK, but I’m running into issues when adding a new command.
I created a StartCommand class in app/StartCommand.php, following the documentation. However, when I try to use it, I get this error:
Command class "Vendor\App\Commands\StartCommand" not found!
I thought this would work based on the documentation, which says commands can be stored in any directory as long as they can be autoloaded. What am I missing here? How can I properly add and use custom commands in my Telegram bot?
Have you considered using the Laravel-specific Telegram Bot package? It’s called ‘telegram-bot-sdk’ by irazasyed, and it integrates seamlessly with Laravel’s ecosystem. I’ve found it much easier to work with than setting up everything from scratch.
To use it, you’d install it via Composer, then publish the config file. Your commands would go in app/Telegram/Commands, and you’d register them in the config file under ‘telegram.commands’.
One key advantage is that it handles a lot of the boilerplate for you, including proper namespacing and autoloading. This might solve your current issue without you having to dig too deep into Laravel’s autoloading quirks.
Just a thought based on my experience - it could save you some time and frustration in the long run.
I’ve encountered similar issues when setting up custom commands for Telegram bots in Laravel. One thing that helped me was double-checking the namespace in my StartCommand.php file. Make sure it matches your project structure, something like:
namespace App\Commands;
Also, verify that your StartCommand class extends the Telegram\Bot\Commands\Command class. If you’ve done all that, try clearing your application cache with php artisan cache:clear and php artisan config:cache. Sometimes Laravel caches old configurations, and this can cause unexpected behavior with newly added commands.
If you’re still stuck, you might want to consider using a package like ‘irazasyed/telegram-bot-sdk’ which streamlines a lot of the Telegram bot setup process in Laravel. It saved me a ton of headaches when I was first getting started with Telegram bots.
hey luna, sounds like ur namespace might be off. try changing ur command class to just App\Commands\StartCommand instead of Vendor\App\Commands\StartCommand. also make sure ur file is actually in the right folder (app/Commands). if that dont work, try runnin composer dump-autoload to refresh the autoloader. good luck!