You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用irazasyed/telegram-bot-sdk开发Bot遇命名冲突及兼容问题

Fixing Two Issues with irazasyed/telegram-bot-sdk dev-develop Branch in Laravel 5.5

Hey there, let's work through these two errors you're facing with your Telegram bot step by step:

1. Resolve "Cannot use Telegram\Bot\Objects\Message as Message because the name is already in use"

This error pops up because of a naming conflict in the SDK's Payments.php file—another class named Message is already in scope when that use statement runs.

Instead of just commenting out the line (which might cause hidden issues later if the SDK actually needs that import), you can use an alias to avoid the conflict cleanly:

  • Open D:\wamp\www\botshop\vendor\irazasyed\telegram-bot-sdk\src\Methods\Payments.php
  • Replace line 5: use Telegram\Bot\Objects\Message; with use Telegram\Bot\Objects\Message as TelegramMessage;
  • Update any references to Message within this file to TelegramMessage

If you prefer not to modify vendor files (a best practice), you can also check if there's an existing issue in the SDK's repo, or wait for a fix in the dev branch. Your quick fix of commenting the line works for now, but using an alias is more robust.

2. Fix "Declaration of App\Commands\StartCommand::handle($arguments) must be compatible with Telegram\Bot\Commands\Command::handle()"

The problem here is that your StartCommand's handle method has a different signature than the parent Command class from the SDK. In the dev-develop branch, the base Command::handle() method doesn't accept any arguments, but your implementation adds $arguments.

To fix this, update your StartCommand code to match the parent method's signature:

class StartCommand extends Command 
{ 
    protected $name = "start"; 
    protected $description = "Start Command to get you started"; 

    public function handle() 
    { 
        $this->replyWithMessage(['text' => 'Hello! Welcome to our bot, Here are our available commands:']); 
    } 
}

If you need access to command arguments, you can retrieve them using the command's built-in properties instead of passing them directly to handle(). For example, you can use $this->arguments (check the SDK's dev branch docs for the exact method if this property isn't available).

内容的提问来源于stack exchange,提问作者Ahmad Badpey

火山引擎 最新活动