祝大家一切顺利,我想创建一个机器人,我使用longman/telegram-bot 我通过示例创建命令。
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;
class TestCommand extends UserCommand
{
protected $name = 'test'; // Your command's name
protected $description = 'A command for test'; // Your command description
protected $usage = '/test'; // Usage of your command
protected $version = '1.0.0'; // Version of your command
public function execute()
{
$message = $this->getMessage(); // Get Message object
$chat_id = $message->getChat()->getId(); // Get the current Chat ID
$data = [ // Set up the new message data
'chat_id' => $chat_id, // Set Chat ID to send the message to
'text' => 'This is just a Test...', // Set message to send
];
return Request::sendMessage($data); // Send message!
}
}
和
$bot_api_key = 'your:bot_api_key';
$bot_username = 'username_bot';
$admin_users = [
// 123,
];
$commands_paths = [
// __DIR__ . '/Commands/',
];
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
// Add commands paths containing your custom commands
$telegram->addCommandsPaths($commands_paths);
// Enable admin users
$telegram->enableAdmins($admin_users);
// Handle telegram webhook request
$telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Silence is golden!
//echo $e;
// Log telegram errors
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Silence is golden!
// Uncomment this to catch log initialisation errors
//echo $e;
}
当我在机器人中发送/测试时,我不明白如何调用我的TestCommand。我在寻找方法并意识到方法$ telegram-> handleGetUpdates();向我返回响应的对象。实际上,问题是如何做到这一点,以便机器人能够回答我的命令?