Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chatbot v2 #9656

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ LEGISLATIVES_AVECVOUS_HOST=legislatives.avecvous.code
RENAISSANCE_HOST=renaissance.code
APP_RENAISSANCE_HOST=renaissance.code
WEBHOOK_RENAISSANCE_HOST=webhook.renaissance.code
WEBHOOK_PROXY_HOST=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be in .env.dev file, no?

API_RENAISSANCE_HOST=api.renaissance.code
RENAISSANCE_QRCODE_HOST=qr.renaissance.code

Expand Down
14 changes: 13 additions & 1 deletion config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,20 @@ framework:
exchange: { name: 'messenger-topic', type: 'topic', default_publish_routing_key: 'chatbot' }
queues:
chatbot: { binding_keys: [ 'chatbot' ] }
telegram:
dsn: "%env(RABBITMQ_DSN)%"
retry_strategy:
max_retries: 10
delay: 3000
multiplier: 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the default value

options:
exchange: { name: 'messenger-topic', type: 'topic', default_publish_routing_key: 'telegram' }
queues:
telegram: { binding_keys: [ 'telegram' ] }

routing:
App\Chatbot\Command\RefreshThreadCommand: chatbot
App\OpenAI\Command\RunThreadCommand: chatbot
App\Telegram\Command\SendBotMessageCommand: telegram
App\Mailchimp\CampaignMessageInterface: mailchimp_campaign
App\Mailchimp\SynchronizeMessageInterface: mailchimp_sync
App\Pap\Command\AsynchronousMessageInterface: pap
Expand Down
4 changes: 4 additions & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ security:
- ROLE_APP_ADMIN_CLIENT_ALL
ROLE_ADMIN_TECH_SCOPES:
- ROLE_APP_ADMIN_SCOPE_ALL
ROLE_ADMIN_TECH_TELEGRAM_BOTS:
- ROLE_APP_ADMIN_TELEGRAM_BOT_ALL
ROLE_ADMIN_TECH_OPENAI_ASSISTANTS:
- ROLE_APP_ADMIN_OPENAI_ASSISTANT_ALL

## Idées
ROLE_ADMIN_IDEES_OLDOLF:
Expand Down
12 changes: 12 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ services:
tags: ['app.acquisition_statistics.calculator']
App\Adherent\Tag\TagGenerator\TagGeneratorInterface:
tags: ['app.adherent.tag.generator']
App\Chatbot\Assistant\AssistantHandlerInterface:
tags: ['app.chatbot.assistant_handler']
App\Telegram\Update\UpdateHandlerInterface:
tags: ['app.telegram.update_handler']

# Imports
App\:
Expand Down Expand Up @@ -491,3 +495,11 @@ services:
App\Adhesion\Handler\GenerateActivationCodeCommandHandler:
arguments:
$loginLinkHandler: '@security.authenticator.login_link_handler.main'

App\Chatbot\Assistant:
arguments:
$assistantHandlers: !tagged_iterator 'app.chatbot.assistant_handler'

App\Telegram\UpdateHandler:
arguments:
$updateHandlers: !tagged_iterator 'app.telegram.update_handler'
32 changes: 24 additions & 8 deletions config/services/admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,6 @@
<argument />
</service>

<service id="app.admin.chatbot_chatbot" class="App\Admin\Chatbot\ChatbotAdmin">
<tag name="sonata.admin" manager-type="orm" label="Chatbots" group="Communication" />

<argument />
<argument>App\Entity\Chatbot\Chatbot</argument>
<argument />
</service>

<!--
######################################################
# Adhérents
Expand Down Expand Up @@ -988,6 +980,30 @@
<argument type="service" id="event_dispatcher"/>
</service>

<service id="app.admin.chatbot_chatbot" class="App\Admin\Chatbot\ChatbotAdmin">
<tag name="sonata.admin" manager-type="orm" label="Chatbots" group="Tech" />

<argument />
<argument>App\Entity\Chatbot\Chatbot</argument>
<argument />
</service>

<service id="app.admin.telegram_bot" class="App\Admin\TelegramBotAdmin">
<tag name="sonata.admin" manager-type="orm" label="Bots Telegram" group="Tech" />

<argument />
<argument>App\Entity\TelegramBot</argument>
<argument />
</service>

<service id="app.admin.openai_assistant" class="App\Admin\OpenAI\AssistantAdmin">
<tag name="sonata.admin" manager-type="orm" label="Assistants OpenAI" group="Tech" />

<argument />
<argument>App\Entity\OpenAI\Assistant</argument>
<argument />
</service>

<!--
######################################################
# Formation
Expand Down
16 changes: 12 additions & 4 deletions config/services_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
public: false
bind:
$environment: "%kernel.environment%"
$webhookProxyHost: "%env(WEBHOOK_PROXY_HOST)%"

App\DataFixtures\:
resource: '../src/DataFixtures/'
Expand All @@ -26,10 +27,6 @@ services:

App\Ohme\ClientInterface: '@Tests\App\Ohme\DummyClient'

Tests\App\Chatbot\Provider\DummyProvider: ~

App\Chatbot\Provider\ProviderInterface: '@Tests\App\Chatbot\Provider\DummyProvider'

App\Mandrill\EmailClient:
class: 'Tests\App\Test\Mailer\NullEmailClient'

Expand All @@ -51,3 +48,14 @@ services:

Kreait\Firebase\Contract\DynamicLinks: '@Tests\App\Test\Firebase\DummyDynamicLinks'
Kreait\Firebase\Contract\Messaging: '@Tests\App\Test\Firebase\DummyMessaging'

Tests\App\OpenAI\Client\Client: ~
App\OpenAI\Client\ClientInterface: '@Tests\App\OpenAI\Client\Client'

Tests\App\Telegram\Client\ClientFactory: ~
App\Telegram\Client\ClientFactoryInterface: '@Tests\App\Telegram\Client\ClientFactory'

App\Telegram\Webhook\UrlGenerator: ~
Tests\App\Telegram\Webhook\ProxyUrlGenerator:
decorates: App\Telegram\Webhook\UrlGenerator
arguments: ['@.inner']
Comment on lines +58 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Service UrlGenerator will be auto created by main service config and you can decorate it here

Suggested change
App\Telegram\Webhook\UrlGenerator: ~
Tests\App\Telegram\Webhook\ProxyUrlGenerator:
decorates: App\Telegram\Webhook\UrlGenerator
arguments: ['@.inner']
Tests\App\Telegram\Webhook\ProxyUrlGenerator:
decorates: App\Telegram\Webhook\UrlGenerator
arguments: ['@.inner']

195 changes: 195 additions & 0 deletions migrations/Version20240204231024.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240204231024 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE openai_assistant (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
created_by_administrator_id INT DEFAULT NULL,
updated_by_administrator_id INT DEFAULT NULL,
name VARCHAR(255) NOT NULL,
open_ai_id VARCHAR(255) NOT NULL,
uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
UNIQUE INDEX UNIQ_55C6718E5E237E06 (name),
UNIQUE INDEX UNIQ_55C6718EF6E32D4D (open_ai_id),
UNIQUE INDEX UNIQ_55C6718ED17F50A6 (uuid),
INDEX IDX_55C6718E9DF5350C (created_by_administrator_id),
INDEX IDX_55C6718ECF1918FF (updated_by_administrator_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE telegram_bot (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
created_by_administrator_id INT DEFAULT NULL,
updated_by_administrator_id INT DEFAULT NULL,
name VARCHAR(255) NOT NULL,
enabled TINYINT(1) DEFAULT 0 NOT NULL,
api_token VARCHAR(255) NOT NULL,
secret VARCHAR(255) NOT NULL,
blacklisted_ids LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:simple_array)\',
whitelisted_ids LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:simple_array)\',
uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
UNIQUE INDEX UNIQ_CED6A3DF5E237E06 (name),
UNIQUE INDEX UNIQ_CED6A3DF7BA2F5EB (api_token),
UNIQUE INDEX UNIQ_CED6A3DF5CA2E8E5 (secret),
UNIQUE INDEX UNIQ_CED6A3DFD17F50A6 (uuid),
INDEX IDX_CED6A3DF9DF5350C (created_by_administrator_id),
INDEX IDX_CED6A3DFCF1918FF (updated_by_administrator_id),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE
openai_assistant
ADD
CONSTRAINT FK_55C6718E9DF5350C FOREIGN KEY (created_by_administrator_id) REFERENCES administrators (id) ON DELETE
SET
NULL');
$this->addSql('ALTER TABLE
openai_assistant
ADD
CONSTRAINT FK_55C6718ECF1918FF FOREIGN KEY (updated_by_administrator_id) REFERENCES administrators (id) ON DELETE
SET
NULL');
$this->addSql('ALTER TABLE
telegram_bot
ADD
CONSTRAINT FK_CED6A3DF9DF5350C FOREIGN KEY (created_by_administrator_id) REFERENCES administrators (id) ON DELETE
SET
NULL');
$this->addSql('ALTER TABLE
telegram_bot
ADD
CONSTRAINT FK_CED6A3DFCF1918FF FOREIGN KEY (updated_by_administrator_id) REFERENCES administrators (id) ON DELETE
SET
NULL');
$this->addSql('DROP INDEX UNIQ_7DC4B00477153098 ON chatbot');
$this->addSql('ALTER TABLE
chatbot
ADD
open_ai_assistant_id INT UNSIGNED DEFAULT NULL,
ADD
telegram_bot_id INT UNSIGNED DEFAULT NULL,
ADD
name VARCHAR(255) NOT NULL,
ADD
type VARCHAR(255) NOT NULL,
ADD
assistant_type VARCHAR(255) NOT NULL,
DROP
code,
DROP
assistant_id,
DROP
enabled,
DROP
telegram_bot_api_token,
DROP
telegram_bot_secret');
$this->addSql('ALTER TABLE
chatbot
ADD
CONSTRAINT FK_7DC4B0043FF15E50 FOREIGN KEY (open_ai_assistant_id) REFERENCES openai_assistant (id) ON DELETE
SET
NULL');
$this->addSql('ALTER TABLE
chatbot
ADD
CONSTRAINT FK_7DC4B004A0E2F38 FOREIGN KEY (telegram_bot_id) REFERENCES telegram_bot (id) ON DELETE
SET
NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_7DC4B0045E237E06 ON chatbot (name)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_7DC4B0043FF15E50 ON chatbot (open_ai_assistant_id)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_7DC4B004A0E2F38 ON chatbot (telegram_bot_id)');
$this->addSql('ALTER TABLE
chatbot_message
ADD
run_id INT UNSIGNED DEFAULT NULL,
ADD
entities JSON NOT NULL,
CHANGE
content text LONGTEXT NOT NULL,
CHANGE
external_id open_ai_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE
chatbot_message
ADD
CONSTRAINT FK_EDF1E88484E3FEC4 FOREIGN KEY (run_id) REFERENCES chatbot_run (id) ON DELETE
SET
NULL');
$this->addSql('CREATE INDEX IDX_EDF1E88484E3FEC4 ON chatbot_message (run_id)');
$this->addSql('ALTER TABLE
chatbot_run
ADD
open_ai_id VARCHAR(255) DEFAULT NULL,
CHANGE
external_id open_ai_status VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE chatbot_thread CHANGE external_id open_ai_id VARCHAR(255) DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chatbot DROP FOREIGN KEY FK_7DC4B0043FF15E50');
$this->addSql('ALTER TABLE chatbot DROP FOREIGN KEY FK_7DC4B004A0E2F38');
$this->addSql('ALTER TABLE openai_assistant DROP FOREIGN KEY FK_55C6718E9DF5350C');
$this->addSql('ALTER TABLE openai_assistant DROP FOREIGN KEY FK_55C6718ECF1918FF');
$this->addSql('ALTER TABLE telegram_bot DROP FOREIGN KEY FK_CED6A3DF9DF5350C');
$this->addSql('ALTER TABLE telegram_bot DROP FOREIGN KEY FK_CED6A3DFCF1918FF');
$this->addSql('DROP TABLE openai_assistant');
$this->addSql('DROP TABLE telegram_bot');
$this->addSql('DROP INDEX UNIQ_7DC4B0045E237E06 ON chatbot');
$this->addSql('DROP INDEX UNIQ_7DC4B0043FF15E50 ON chatbot');
$this->addSql('DROP INDEX UNIQ_7DC4B004A0E2F38 ON chatbot');
$this->addSql('ALTER TABLE
chatbot
ADD
code VARCHAR(255) NOT NULL,
ADD
assistant_id VARCHAR(255) NOT NULL,
ADD
enabled TINYINT(1) DEFAULT 0 NOT NULL,
ADD
telegram_bot_api_token VARCHAR(255) DEFAULT NULL,
ADD
telegram_bot_secret VARCHAR(255) DEFAULT NULL,
DROP
open_ai_assistant_id,
DROP
telegram_bot_id,
DROP
name,
DROP
type,
DROP
assistant_type');
$this->addSql('CREATE UNIQUE INDEX UNIQ_7DC4B00477153098 ON chatbot (code)');
$this->addSql('ALTER TABLE chatbot_message DROP FOREIGN KEY FK_EDF1E88484E3FEC4');
$this->addSql('DROP INDEX IDX_EDF1E88484E3FEC4 ON chatbot_message');
$this->addSql('ALTER TABLE
chatbot_message
DROP
run_id,
DROP
entities,
CHANGE
text content LONGTEXT NOT NULL,
CHANGE
open_ai_id external_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE
chatbot_run
ADD
external_id VARCHAR(255) DEFAULT NULL,
DROP
open_ai_status,
DROP
open_ai_id');
$this->addSql('ALTER TABLE chatbot_thread CHANGE open_ai_id external_id VARCHAR(255) DEFAULT NULL');
}
}
Loading
Loading