setName('talk:bot:setup')
->setDescription('Add a bot to a conversation')
->addArgument(
'bot-id',
InputArgument::REQUIRED,
'The ID of the bot to set up in a conversation'
)
->addArgument(
'token',
InputArgument::IS_ARRAY,
'Conversation tokens to set the bot up for'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$botId = (int)$input->getArgument('bot-id');
$tokens = $input->getArgument('token');
try {
$botServer = $this->botServerMapper->findById($botId);
} catch (DoesNotExistException) {
$output->writeln('Bot could not be found by id: ' . $botId . '');
return 1;
}
$returnCode = 0;
foreach ($tokens as $token) {
try {
$room = $this->roomManager->getRoomByToken($token);
if ($room->isFederatedConversation()) {
$output->writeln('Federated conversations can not have bots: ' . $token . '');
$returnCode = 2;
continue;
}
} catch (RoomNotFoundException) {
$output->writeln('Conversation could not be found by token: ' . $token . '');
$returnCode = 2;
continue;
}
$bot = new BotConversation();
$bot->setBotId($botId);
$bot->setToken($token);
$bot->setState(Bot::STATE_ENABLED);
try {
$this->botConversationMapper->insert($bot);
$output->writeln('Successfully set up for conversation ' . $token . '');
$event = new BotEnabledEvent($room, $botServer);
$this->dispatcher->dispatchTyped($event);
} catch (\Exception $e) {
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$output->writeln('Bot is already set up for the conversation ' . $token . '');
$returnCode = 3;
} else {
$output->writeln('' . get_class($e) . ': ' . $e->getMessage() . '');
$returnCode = 4;
}
}
}
return $returnCode;
}
}