mirror of
https://github.com/nextcloud/spreed.git
synced 2025-12-17 21:12:20 +01:00
feat(bots): Add talk:bot:create command
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
This commit is contained in:
parent
15a2710e32
commit
d5acc82ed8
7 changed files with 153 additions and 1 deletions
|
|
@ -87,6 +87,7 @@
|
|||
|
||||
<commands>
|
||||
<command>OCA\Talk\Command\Bot\Install</command>
|
||||
<command>OCA\Talk\Command\Bot\Create</command>
|
||||
<command>OCA\Talk\Command\Bot\ListBots</command>
|
||||
<command>OCA\Talk\Command\Bot\Remove</command>
|
||||
<command>OCA\Talk\Command\Bot\State</command>
|
||||
|
|
|
|||
19
docs/occ.md
19
docs/occ.md
|
|
@ -21,6 +21,25 @@ Install a new bot on the server
|
|||
| `--no-setup` | Prevent moderators from setting up the bot in a conversation | no | no | no | `false` |
|
||||
| `--feature\|-f` | Specify the list of features for the bot - webhook: The bot receives posted chat messages as webhooks - response: The bot can post messages and reactions as a response - event: The bot reads posted messages from local events - reaction: The bot is notified about adding and removing of reactions - none: When all features should be disabled for the bot | yes | yes | yes | *Required* |
|
||||
|
||||
## talk:bot:create
|
||||
|
||||
Creates a new bot on the server with 'response' feature only.
|
||||
|
||||
### Usage
|
||||
|
||||
* `talk:bot:create [--output [OUTPUT]] [-s|--secret SECRET] [--no-setup] [--] <name> [<description>]`
|
||||
|
||||
| Arguments | Description | Is required | Is array | Default |
|
||||
|---|---|---|---|---|
|
||||
| `name` | The name under which the messages will be posted (min. 1 char, max. 64 chars) | yes | no | *Required* |
|
||||
| `description` | Optional description shown in the admin settings (max. 4000 chars) | no | no | `NULL` |
|
||||
|
||||
| Options | Description | Accept value | Is value required | Is multiple | Default |
|
||||
|---|---|---|---|---|---|
|
||||
| `--output` | Output format (plain, json or json_pretty, default is plain) | yes | no | no | `'plain'` |
|
||||
| `--secret\|-s` | Secret used to validate API calls (min. 40 chars, max. 128 chars). When none is provided, a random 64 chars string is generated and output. | yes | yes | no | *Required* |
|
||||
| `--no-setup` | Prevent moderators from setting up the bot in a conversation | no | no | no | `false` |
|
||||
|
||||
## talk:bot:list
|
||||
|
||||
List all installed bots of the server or a conversation
|
||||
|
|
|
|||
116
lib/Command/Bot/Create.php
Normal file
116
lib/Command/Bot/Create.php
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Talk\Command\Bot;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Talk\Model\Bot;
|
||||
use OCA\Talk\Model\BotServer;
|
||||
use OCA\Talk\Model\BotServerMapper;
|
||||
use OCA\Talk\Service\BotService;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\DB\Exception;
|
||||
use OCP\Security\ISecureRandom;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class Create extends Base {
|
||||
public function __construct(
|
||||
private BotService $botService,
|
||||
private BotServerMapper $botServerMapper,
|
||||
private ISecureRandom $secureRandom,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
$this
|
||||
->setName('talk:bot:create')
|
||||
->setDescription('Creates a new bot on the server with \'response\' feature only.')
|
||||
->addArgument(
|
||||
'name',
|
||||
InputArgument::REQUIRED,
|
||||
'The name under which the messages will be posted (min. 1 char, max. 64 chars)'
|
||||
)
|
||||
->addArgument(
|
||||
'description',
|
||||
InputArgument::OPTIONAL,
|
||||
'Optional description shown in the admin settings (max. 4000 chars)'
|
||||
)
|
||||
->addOption(
|
||||
'secret',
|
||||
's',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Secret used to validate API calls (min. 40 chars, max. 128 chars). When none is provided, a random 64 chars string is generated and output.'
|
||||
)
|
||||
->addOption(
|
||||
'no-setup',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Prevent moderators from setting up the bot in a conversation'
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$name = $input->getArgument('name');
|
||||
$description = $input->getArgument('description') ?? '';
|
||||
$noSetup = $input->getOption('no-setup');
|
||||
$featureFlags = Bot::FEATURE_RESPONSE;
|
||||
|
||||
$secret = $input->getOption('secret') ?? $this->secureRandom->generate(64);
|
||||
$url = Bot::URL_RESPONSE_ONLY_PREFIX . bin2hex(random_bytes(16));
|
||||
|
||||
try {
|
||||
$this->botService->validateBotParameters($name, $secret, $url, $description);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->botServerMapper->findByUrl($url);
|
||||
$output->writeln('<error>Bot with the same URL is already registered</error>');
|
||||
return 2;
|
||||
} catch (DoesNotExistException) {
|
||||
}
|
||||
|
||||
$bot = new BotServer();
|
||||
$bot->setName($name);
|
||||
$bot->setSecret($secret);
|
||||
$bot->setUrl($url);
|
||||
$bot->setUrlHash(sha1($url));
|
||||
$bot->setDescription($description);
|
||||
$bot->setState($noSetup ? Bot::STATE_NO_SETUP : Bot::STATE_ENABLED);
|
||||
$bot->setFeatures($featureFlags);
|
||||
try {
|
||||
$botEntity = $this->botServerMapper->insert($bot);
|
||||
} catch (\Exception $e) {
|
||||
if ($e instanceof Exception && $e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
|
||||
$output->writeln('<error>Bot with the same secret is already registered</error>');
|
||||
return 3;
|
||||
} else {
|
||||
$output->writeln('<error>' . get_class($e) . ': ' . $e->getMessage() . '</error>');
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('<info>Bot installed</info>');
|
||||
$output->writeln('ID: ' . $botEntity->getId());
|
||||
|
||||
if ($input->getOption('secret') === null) {
|
||||
$output->writeln('Secret: ' . $secret);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -77,6 +77,11 @@ class State extends Base {
|
|||
|
||||
$bot->setState($state);
|
||||
if ($featureFlags !== null) {
|
||||
if (str_starts_with($bot->getUrl(), Bot::URL_RESPONSE_ONLY_PREFIX)) {
|
||||
$output->writeln('<error>Feature flags of response-only bots cannot be changed</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$bot->setFeatures($featureFlags);
|
||||
}
|
||||
$this->botServerMapper->update($bot);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ class Bot {
|
|||
public const FEATURE_LABEL_EVENT = 'event';
|
||||
public const FEATURE_LABEL_REACTION = 'reaction';
|
||||
public const URL_APP_PREFIX = 'nextcloudapp://';
|
||||
public const URL_RESPONSE_ONLY_PREFIX = 'responseonly://';
|
||||
|
||||
public const FEATURE_MAP = [
|
||||
self::FEATURE_NONE => self::FEATURE_LABEL_NONE,
|
||||
|
|
|
|||
|
|
@ -452,7 +452,7 @@ class BotService {
|
|||
throw new \InvalidArgumentException('The provided secret is too short (min. 40 chars, max. 128 chars)');
|
||||
}
|
||||
|
||||
if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, Bot::URL_APP_PREFIX))) {
|
||||
if (!$url || strlen($url) > 4000 || !(str_starts_with($url, 'http://') || str_starts_with($url, 'https://') || str_starts_with($url, Bot::URL_APP_PREFIX) || str_starts_with($url, Bot::URL_RESPONSE_ONLY_PREFIX))) {
|
||||
throw new \InvalidArgumentException('The provided URL is not a valid URL');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -381,3 +381,13 @@ Feature: chat/bots
|
|||
Given invoking occ with "talk:bot:list room-name:room"
|
||||
And the command was successful
|
||||
And the command output is empty
|
||||
|
||||
Scenario: Test bot creation
|
||||
Given invoking occ with "talk:bot:create Bot"
|
||||
And the command was successful
|
||||
And the command output contains the text "Secret:"
|
||||
When invoking occ with "talk:bot:create Bot Description"
|
||||
Then the command was successful
|
||||
And the command output contains the text "Secret:"
|
||||
When invoking occ with "talk:bot:create --secret Secret1234567890123456789012345678901234567890 Bot"
|
||||
Then the command was successful
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue