decidim-chatbot 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.erb_lint.yml +2134 -0
- data/.github/workflows/website.yml +57 -0
- data/.gitignore +13 -0
- data/.gitlab-ci.yml +204 -0
- data/.node-version +1 -0
- data/.nvmrc +1 -0
- data/.rspec +2 -0
- data/.rubocop.yml +17 -0
- data/.ruby-version +1 -0
- data/.simplecov +36 -0
- data/CONTRIBUTING.md +54 -0
- data/Gemfile +50 -0
- data/Gemfile.lock +1013 -0
- data/LICENSE.md +661 -0
- data/README.md +62 -0
- data/Rakefile +54 -0
- data/app/commands/decidim/chatbot/admin/destroy_item.rb +25 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_exit.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_login.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_propose_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_blogs.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_vote_on_proposal.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_manual.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_phase.rb +12 -0
- data/app/commands/decidim/chatbot/admin/persist_item.rb +47 -0
- data/app/commands/decidim/chatbot/admin/reorder_items.rb +51 -0
- data/app/commands/decidim/chatbot/admin/send_test_notification.rb +74 -0
- data/app/commands/decidim/chatbot/admin/unsubscribe_user.rb +35 -0
- data/app/commands/decidim/chatbot/admin/update_chatbot.rb +17 -0
- data/app/commands/decidim/chatbot/notifications/deliver.rb +30 -0
- data/app/commands/decidim/chatbot/notifications/enqueue_phase.rb +67 -0
- data/app/commands/decidim/chatbot/notifications/send.rb +115 -0
- data/app/commands/decidim/chatbot/whatsapp/create_from_phone.rb +95 -0
- data/app/commands/decidim/chatbot/whatsapp/sync_phone.rb +56 -0
- data/app/controllers/decidim/api/rest_full/chatbot/base_controller.rb +52 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_menu_items_controller.rb +49 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_users_controller.rb +46 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbots_controller.rb +40 -0
- data/app/controllers/decidim/chatbot/admin/chatbot_edit.rb +85 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/generals_controller.rb +43 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/menu_items_controller.rb +32 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/notifications_controller.rb +92 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users/filterable.rb +33 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users_controller.rb +53 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/whatsapps_controller.rb +59 -0
- data/app/controllers/decidim/chatbot/admin/chatbots_controller.rb +20 -0
- data/app/controllers/decidim/chatbot/admin/items_crud.rb +183 -0
- data/app/forms/decidim/chatbot/admin/chatbot_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/config_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/menu_item_form.rb +80 -0
- data/app/forms/decidim/chatbot/admin/menu_items/component_id_form.rb +60 -0
- data/app/forms/decidim/chatbot/admin/menu_items/exit_form.rb +24 -0
- data/app/forms/decidim/chatbot/admin/menu_items/login_form.rb +35 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_labels.rb +53 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_blogs_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_content_labels.rb +51 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/vote_on_proposal_form.rb +123 -0
- data/app/forms/decidim/chatbot/admin/notification_item_form.rb +111 -0
- data/app/forms/decidim/chatbot/admin/notifications/manual_form.rb +13 -0
- data/app/forms/decidim/chatbot/admin/notifications/phase_form.rb +79 -0
- data/app/forms/decidim/chatbot/admin/notifications/template_fields.rb +60 -0
- data/app/forms/decidim/chatbot/admin/notifications/test_form.rb +179 -0
- data/app/helpers/decidim/chatbot/admin/items_helper.rb +44 -0
- data/app/jobs/decidim/chatbot/chatbot_webhook_job.rb +59 -0
- data/app/jobs/decidim/chatbot/send_notification_job.rb +33 -0
- data/app/models/decidim/chatbot/chatbot.rb +47 -0
- data/app/models/decidim/chatbot/item.rb +99 -0
- data/app/packs/.keep +0 -0
- data/app/presenters/decidim/chatbot/admin_log/chatbot_presenter.rb +42 -0
- data/app/presenters/decidim/chatbot/admin_log/item_presenter.rb +32 -0
- data/app/queries/decidim/chatbot/admin/proposal_vote_mode.rb +41 -0
- data/app/queries/decidim/chatbot/whatsapp_users.rb +58 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer.rb +113 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_notification_serializer.rb +19 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_serializer.rb +66 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_identity_serializer.rb +23 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer.rb +28 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_webhook_serializer.rb +21 -0
- data/app/services/decidim/chatbot/chatbot_webhook_handler.rb +40 -0
- data/app/services/decidim/chatbot/notifications/bindings.rb +87 -0
- data/app/services/decidim/chatbot/whatsapp/body_parameters.rb +29 -0
- data/app/services/decidim/chatbot/whatsapp/graph_client.rb +136 -0
- data/app/services/decidim/chatbot/whatsapp/status_mapper.rb +93 -0
- data/app/services/decidim/chatbot/whatsapp/template_manager_url.rb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/_menu_preview.html.erb +49 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/edit.html.erb +79 -0
- data/app/views/decidim/chatbot/admin/chatbots/index.html.erb +50 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/_form.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/edit.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/index.html.erb +43 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/new.html.erb +12 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_form.html.erb +4 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_manual_fields.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_phase_fields.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/edit.html.erb +3 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/index.html.erb +40 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/new.html.erb +11 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/test.html.erb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/tabs/_whatsapp.html.erb +47 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/_filters.html.erb +23 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/index.html.erb +56 -0
- data/app/views/decidim/chatbot/admin/chatbots/whatsapps/show.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/_item_form.html.erb +64 -0
- data/app/views/decidim/chatbot/admin/shared/_row_actions.html.erb +28 -0
- data/app/views/decidim/chatbot/admin/shared/_tabs_menu.html.erb +13 -0
- data/app/views/decidim/chatbot/admin/shared/_type_fields.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/shared/_type_picker.html.erb +15 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_component_id.html.erb +9 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_exit.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_login.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_manual.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_phase.html.erb +46 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_propose_proposals.html.erb +10 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_blogs.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_proposals.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_vote_on_proposal.html.erb +26 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_abstention_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_vote_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_votes_closed_warning.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_weight_labels.html.erb +15 -0
- data/bin/check +13 -0
- data/bin/ci-setup +38 -0
- data/bin/gen-node-client +112 -0
- data/bin/gen-openapi +248 -0
- data/bin/i18n-tasks +27 -0
- data/bin/postversion +17 -0
- data/config/i18n-tasks.yml +38 -0
- data/config/locales/decidim_chatbot.en.yml +323 -0
- data/config/locales/decidim_chatbot.fr.yml +29 -0
- data/contrib/decidim-node-client/.gitignore +4 -0
- data/contrib/decidim-node-client/.npmignore +1 -0
- data/contrib/decidim-node-client/.openapi-generator/FILES +8 -0
- data/contrib/decidim-node-client/.openapi-generator/VERSION +1 -0
- data/contrib/decidim-node-client/.openapi-generator-ignore +13 -0
- data/contrib/decidim-node-client/.yarnrc.yml +5 -0
- data/contrib/decidim-node-client/README.md +37 -0
- data/contrib/decidim-node-client/api.ts +14193 -0
- data/contrib/decidim-node-client/base.ts +70 -0
- data/contrib/decidim-node-client/common.ts +185 -0
- data/contrib/decidim-node-client/configuration.ts +138 -0
- data/contrib/decidim-node-client/git_push.sh +57 -0
- data/contrib/decidim-node-client/index.ts +16 -0
- data/contrib/decidim-node-client/package.json +49 -0
- data/contrib/decidim-node-client/packup.config.ts +7 -0
- data/contrib/decidim-node-client/tsconfig.json +25 -0
- data/contrib/decidim-node-client/yarn.lock +2367 -0
- data/crowdin.yml +15 -0
- data/db/migrate/20260725120000_create_decidim_chatbot_chatbots.rb +20 -0
- data/db/migrate/20260822120000_create_decidim_chatbot_items.rb +20 -0
- data/db/migrate/20260824120000_add_menu_and_error_copy_to_decidim_chatbot_chatbots.rb +13 -0
- data/db/migrate/20260824140000_add_enabled_to_decidim_chatbot_items.rb +7 -0
- data/decidim-chatbot.gemspec +40 -0
- data/docker-compose.ci.yml +59 -0
- data/docker-compose.yml +47 -0
- data/lib/decidim/chatbot/admin_engine.rb +67 -0
- data/lib/decidim/chatbot/admin_menu.rb +41 -0
- data/lib/decidim/chatbot/default_manifests.rb +91 -0
- data/lib/decidim/chatbot/engine.rb +107 -0
- data/lib/decidim/chatbot/manifest.rb +31 -0
- data/lib/decidim/chatbot/menu_item_label_keys.rb +26 -0
- data/lib/decidim/chatbot/registry.rb +42 -0
- data/lib/decidim/chatbot/rest_full_ability.rb +24 -0
- data/lib/decidim/chatbot/settings_tab.rb +18 -0
- data/lib/decidim/chatbot/test_definitions.rb +352 -0
- data/lib/decidim/chatbot/variable_interpolator.rb +30 -0
- data/lib/decidim/chatbot/version.rb +21 -0
- data/lib/decidim/chatbot/webhook_publisher.rb +29 -0
- data/lib/decidim/chatbot.rb +41 -0
- data/lib/decidim-chatbot.rb +3 -0
- data/lib/tasks/chatbot.rake +64 -0
- data/openapi/openapi.json +37801 -0
- data/openapitools.json +7 -0
- data/package.json +22 -0
- data/prettier.config.js +15 -0
- data/spec/commands/decidim/chatbot/admin/destroy_item_spec.rb +24 -0
- data/spec/commands/decidim/chatbot/admin/persist_item_spec.rb +80 -0
- data/spec/commands/decidim/chatbot/admin/reorder_items_spec.rb +53 -0
- data/spec/commands/decidim/chatbot/admin/send_test_notification_spec.rb +105 -0
- data/spec/commands/decidim/chatbot/admin/unsubscribe_user_spec.rb +46 -0
- data/spec/commands/decidim/chatbot/admin/update_chatbot_spec.rb +56 -0
- data/spec/commands/decidim/chatbot/notifications/enqueue_phase_spec.rb +59 -0
- data/spec/commands/decidim/chatbot/notifications/send_spec.rb +101 -0
- data/spec/commands/decidim/chatbot/whatsapp/create_from_phone_spec.rb +122 -0
- data/spec/commands/decidim/chatbot/whatsapp/sync_phone_spec.rb +110 -0
- data/spec/controllers/decidim/chatbot/admin/chatbots_controller_spec.rb +25 -0
- data/spec/factories/chatbot.rb +44 -0
- data/spec/forms/decidim/chatbot/admin/chatbot_form_spec.rb +41 -0
- data/spec/forms/decidim/chatbot/admin/config_form_spec.rb +30 -0
- data/spec/forms/decidim/chatbot/admin/menu_item_form_spec.rb +49 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_form_spec.rb +54 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_forms_spec.rb +512 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/exit_form_spec.rb +39 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/login_form_spec.rb +101 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/propose_proposals_form_prefill_spec.rb +26 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/read_blogs_form_prefill_spec.rb +22 -0
- data/spec/forms/decidim/chatbot/admin/notifications/manual_form_spec.rb +130 -0
- data/spec/forms/decidim/chatbot/admin/notifications/phase_form_spec.rb +79 -0
- data/spec/forms/decidim/chatbot/admin/notifications/test_form_spec.rb +172 -0
- data/spec/jobs/decidim/chatbot/chatbot_webhook_job_spec.rb +47 -0
- data/spec/jobs/decidim/chatbot/send_notification_job_spec.rb +53 -0
- data/spec/lib/decidim/chatbot/engine_spec.rb +44 -0
- data/spec/lib/decidim/chatbot/registry_spec.rb +30 -0
- data/spec/lib/decidim/chatbot/settings_tab_spec.rb +21 -0
- data/spec/lib/decidim/chatbot/variable_interpolator_spec.rb +34 -0
- data/spec/lib/decidim/chatbot/whatsapp/status_mapper_spec.rb +64 -0
- data/spec/lib/decidim/chatbot_spec.rb +59 -0
- data/spec/models/decidim/chatbot/chatbot_spec.rb +23 -0
- data/spec/models/decidim/chatbot/item_spec.rb +88 -0
- data/spec/presenters/decidim/chatbot/admin_log/chatbot_presenter_spec.rb +64 -0
- data/spec/queries/decidim/chatbot/admin/proposal_vote_mode_spec.rb +74 -0
- data/spec/queries/decidim/chatbot/whatsapp_users_spec.rb +57 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_menu_items_controller_spec.rb +344 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_users_controller_spec.rb +140 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbots_controller_spec.rb +195 -0
- data/spec/requests/decidim/chatbot/admin/chatbots/notifications_controller_spec.rb +44 -0
- data/spec/requests/decidim/chatbot/admin/tenant_isolation_spec.rb +133 -0
- data/spec/requests/decidim/chatbot/admin/unsubscribe_user_spec.rb +28 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer_spec.rb +321 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_serializer_spec.rb +49 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer_spec.rb +40 -0
- data/spec/services/decidim/chatbot/chatbot_webhook_handler_spec.rb +43 -0
- data/spec/services/decidim/chatbot/notifications/bindings_spec.rb +86 -0
- data/spec/services/decidim/chatbot/whatsapp/body_parameters_spec.rb +22 -0
- data/spec/services/decidim/chatbot/whatsapp/graph_client_spec.rb +205 -0
- data/spec/services/decidim/chatbot/whatsapp/template_manager_url_spec.rb +33 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/allow_all_hosts.rb +9 -0
- data/spec/support/chatbot_admin.rb +16 -0
- data/spec/support/decidim_factories.rb +7 -0
- data/spec/support/decidim_system_factories.rb +3 -0
- data/spec/support/devise.rb +5 -0
- data/spec/support/rack_test_system_driver.rb +43 -0
- data/spec/support/whatsapp_graph.rb +15 -0
- data/spec/swagger_helper.rb +47 -0
- data/spec/system/decidim/chatbot/admin/admin_can_send_manual_when_ready_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_configures_vote_on_proposal_menu_item_spec.rb +84 -0
- data/spec/system/decidim/chatbot/admin/admin_confirms_send_now_recipient_count_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_menu_item_with_demo_field_spec.rb +65 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_phase_and_manual_notifications_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_org_processes_in_phase_select_spec.rb +27 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_organization_chatbots_spec.rb +25 -0
- data/spec/system/decidim/chatbot/admin/admin_is_redirected_when_chatbot_is_disabled_spec.rb +28 -0
- data/spec/system/decidim/chatbot/admin/admin_lists_organization_chatbots_spec.rb +36 -0
- data/spec/system/decidim/chatbot/admin/admin_manual_notification_sends_with_nickname_spec.rb +68 -0
- data/spec/system/decidim/chatbot/admin/admin_navigates_chatbot_main_tabs_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_phase_notification_sends_on_step_change_spec.rb +79 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_blog_component_spec.rb +205 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_participatory_space_spec.rb +45 -0
- data/spec/system/decidim/chatbot/admin/admin_reorders_chatbot_menu_items_spec.rb +29 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_menu_item_faded_on_the_table_spec.rb +23 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_notification_item_faded_on_the_table_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_notification_facts_and_frozen_template_name_spec.rb +71 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_propose_proposals_defaults_prefilled_spec.rb +119 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_type_specific_fields_spec.rb +89 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_facts_and_can_sync_spec.rb +48 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_template_edit_link_spec.rb +37 -0
- data/spec/system/decidim/chatbot/admin/admin_tests_notification_spec.rb +125 -0
- data/spec/system/decidim/chatbot/admin/admin_unchecks_enabled_on_a_menu_item_spec.rb +30 -0
- data/spec/system/decidim/chatbot/admin/admin_unsubscribes_whatsapp_user_from_chatbot_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_updates_welcome_message_from_general_tab_spec.rb +84 -0
- data/spec/system/decidim/chatbot/system/admin_updates_organization_chatbot_settings_spec.rb +33 -0
- data/spec/tasks/chatbot_create_spec.rb +99 -0
- data/spec/tasks/chatbot_sync_spec.rb +90 -0
- data/website/.node-version +1 -0
- data/website/.nvmrc +1 -0
- data/website/docs/developer/_category_.json +4 -0
- data/website/docs/developer/contribute.md +87 -0
- data/website/docs/index.md +42 -0
- data/website/docs/integrate/_category_.json +4 -0
- data/website/docs/integrate/index.md +214 -0
- data/website/docusaurus.config.ts +93 -0
- data/website/package.json +37 -0
- data/website/sidebars.ts +7 -0
- data/website/src/css/custom.css +37 -0
- data/website/static/api-docs.html +2218 -0
- data/website/static/img/logo.svg +1 -0
- data/website/static/openapi.json +37801 -0
- data/website/tsconfig.json +6 -0
- data/website/yarn.lock +9782 -0
- data/yarn.lock +1003 -0
- metadata +424 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
class Chatbot < ApplicationRecord
|
|
6
|
+
self.table_name = "decidim_chatbot_chatbots"
|
|
7
|
+
|
|
8
|
+
include Decidim::Traceable
|
|
9
|
+
include Decidim::Loggable
|
|
10
|
+
include Decidim::TranslatableResource
|
|
11
|
+
|
|
12
|
+
STATUSES = %w(
|
|
13
|
+
created
|
|
14
|
+
subscribed
|
|
15
|
+
waiting_2fa
|
|
16
|
+
waiting_confirmation
|
|
17
|
+
blocked
|
|
18
|
+
ready
|
|
19
|
+
).freeze
|
|
20
|
+
|
|
21
|
+
belongs_to :organization,
|
|
22
|
+
foreign_key: "decidim_organization_id",
|
|
23
|
+
class_name: "Decidim::Organization"
|
|
24
|
+
has_many :items,
|
|
25
|
+
class_name: "Decidim::Chatbot::Item",
|
|
26
|
+
foreign_key: "decidim_chatbot_id",
|
|
27
|
+
inverse_of: :chatbot,
|
|
28
|
+
dependent: :destroy
|
|
29
|
+
|
|
30
|
+
translatable_fields :name, :welcome_message, :menu_intro, :menu_connector,
|
|
31
|
+
:error_404, :error_502, :error_401
|
|
32
|
+
|
|
33
|
+
def subscription_key
|
|
34
|
+
"whatsapp_chatbot_#{id}_subscribed"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
validates :whatsapp_phone_id, presence: true, uniqueness: true
|
|
38
|
+
validates :whatsapp_phone_status, inclusion: { in: STATUSES }
|
|
39
|
+
|
|
40
|
+
scope :for_organization, ->(organization) { where(organization:) }
|
|
41
|
+
|
|
42
|
+
def self.log_presenter_class_for(_log)
|
|
43
|
+
Decidim::Chatbot::AdminLog::ChatbotPresenter
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
class Item < ApplicationRecord
|
|
6
|
+
self.table_name = "decidim_chatbot_items"
|
|
7
|
+
|
|
8
|
+
include Decidim::Traceable
|
|
9
|
+
include Decidim::Loggable
|
|
10
|
+
include Decidim::TranslatableResource
|
|
11
|
+
|
|
12
|
+
SCOPES = %w(menu notification).freeze
|
|
13
|
+
|
|
14
|
+
belongs_to :chatbot,
|
|
15
|
+
class_name: "Decidim::Chatbot::Chatbot",
|
|
16
|
+
foreign_key: "decidim_chatbot_id",
|
|
17
|
+
inverse_of: :items,
|
|
18
|
+
touch: true
|
|
19
|
+
|
|
20
|
+
translatable_fields :title, :introduction
|
|
21
|
+
|
|
22
|
+
validates :scope, inclusion: { in: SCOPES }
|
|
23
|
+
validates :manifest_name, presence: true
|
|
24
|
+
validate :manifest_must_be_registered
|
|
25
|
+
|
|
26
|
+
scope :for_scope, ->(name) { where(scope: name.to_s).order(:position, :id) }
|
|
27
|
+
scope :enabled, -> { where(enabled: true) }
|
|
28
|
+
|
|
29
|
+
delegate :organization, to: :chatbot
|
|
30
|
+
|
|
31
|
+
def manifest
|
|
32
|
+
Decidim::Chatbot.registry.for(scope).find { |entry| entry.name.to_s == manifest_name }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.log_presenter_class_for(_log)
|
|
36
|
+
Decidim::Chatbot::AdminLog::ItemPresenter
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def template_id
|
|
40
|
+
template_id_for(default_locale)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def template_id_for(locale)
|
|
44
|
+
raw = settings["template_id"]
|
|
45
|
+
return raw.to_s if raw.is_a?(String)
|
|
46
|
+
|
|
47
|
+
hash = stringify(raw)
|
|
48
|
+
hash[locale.to_s].presence || hash[default_locale].presence || hash.values.find(&:present?).to_s
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def variables_hash
|
|
52
|
+
variables_hash_for(default_locale)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def variables_hash_for(locale)
|
|
56
|
+
raw = settings["variables"]
|
|
57
|
+
return stringify(raw) unless locale_keyed?(raw)
|
|
58
|
+
|
|
59
|
+
hash = stringify(raw)
|
|
60
|
+
picked = hash[locale.to_s]
|
|
61
|
+
return stringify(picked) if picked.is_a?(Hash)
|
|
62
|
+
|
|
63
|
+
stringify(locale_hash(raw))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def manifest_must_be_registered
|
|
69
|
+
return if manifest
|
|
70
|
+
|
|
71
|
+
errors.add(:manifest_name, :invalid)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def locale_hash(raw)
|
|
75
|
+
hash = stringify(raw)
|
|
76
|
+
value = hash[default_locale] || hash.values.find { |item| item.is_a?(Hash) }
|
|
77
|
+
value.is_a?(Hash) ? value : {}
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def locale_keyed?(raw)
|
|
81
|
+
return false unless raw.is_a?(Hash)
|
|
82
|
+
|
|
83
|
+
stringify(raw).keys.intersect?(available_locales)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def available_locales
|
|
87
|
+
organization.available_locales.map(&:to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def default_locale
|
|
91
|
+
organization.default_locale.to_s
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def stringify(value)
|
|
95
|
+
value.to_h.stringify_keys
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
data/app/packs/.keep
ADDED
|
File without changes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module AdminLog
|
|
6
|
+
# Presents chatbot updates in the organization admin activity log.
|
|
7
|
+
class ChatbotPresenter < Decidim::Log::BasePresenter
|
|
8
|
+
DIFF_FIELDS = {
|
|
9
|
+
name: :i18n,
|
|
10
|
+
welcome_message: :i18n,
|
|
11
|
+
menu_intro: :i18n,
|
|
12
|
+
menu_connector: :i18n,
|
|
13
|
+
error_404: :i18n,
|
|
14
|
+
error_502: :i18n,
|
|
15
|
+
error_401: :i18n,
|
|
16
|
+
whatsapp_phone_status: :string,
|
|
17
|
+
whatsapp_display_name: :string,
|
|
18
|
+
whatsapp_phone_number: :string
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def diff_fields_mapping
|
|
24
|
+
DIFF_FIELDS
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def action_string
|
|
28
|
+
case action
|
|
29
|
+
when "create", "delete", "update"
|
|
30
|
+
"decidim.admin_log.chatbot.#{action}"
|
|
31
|
+
else
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def i18n_labels_scope
|
|
37
|
+
"activemodel.attributes.chatbot"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module AdminLog
|
|
6
|
+
# Presents notification send actions in the organization admin activity log.
|
|
7
|
+
class ItemPresenter < Decidim::Log::BasePresenter
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def action_string
|
|
11
|
+
return "decidim.admin_log.item.send" if action.to_s == "send"
|
|
12
|
+
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def i18n_params
|
|
17
|
+
super.merge(recipients_count: extra_value("recipients_count"),
|
|
18
|
+
failures_count: extra_value("failures_count"))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def extra_value(key)
|
|
22
|
+
hash = action_log.extra.with_indifferent_access
|
|
23
|
+
hash[key] || hash.dig("extra", key)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def diff_actions
|
|
27
|
+
[]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Reads proposal-component vote mode for chatbot menu settings.
|
|
7
|
+
class ProposalVoteMode
|
|
8
|
+
def initialize(component)
|
|
9
|
+
@component = component
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def votes_open?
|
|
13
|
+
return false unless @component
|
|
14
|
+
|
|
15
|
+
settings = @component.current_settings
|
|
16
|
+
settings.present? && settings.votes_enabled? && !settings.votes_blocked?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def voting_cards?
|
|
20
|
+
return false unless awesome_loaded? && @component
|
|
21
|
+
|
|
22
|
+
@component.settings.try(:awesome_voting_manifest).to_s == "voting_cards"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def simple_vote?
|
|
26
|
+
@component.present? && !voting_cards?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def abstention?
|
|
30
|
+
voting_cards? && @component.settings.try(:voting_cards_show_abstain)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def awesome_loaded?
|
|
36
|
+
Gem.loaded_specs.has_key?("decidim-decidim_awesome")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
# Org users who have a WhatsApp phone on extended_data.
|
|
6
|
+
class WhatsappUsers < Decidim::Query
|
|
7
|
+
def self.for(chatbot)
|
|
8
|
+
new(chatbot).query
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.subscribed_for(chatbot)
|
|
12
|
+
new(chatbot).subscribed
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.subscribed_for_organization(organization)
|
|
16
|
+
chatbots = Chatbot.for_organization(organization).to_a
|
|
17
|
+
return Decidim::User.none if chatbots.empty?
|
|
18
|
+
|
|
19
|
+
base = Decidim::User.where(organization:).where(phone_sql)
|
|
20
|
+
clauses = chatbots.map { |bot| not_unsubscribed_sql(bot) }
|
|
21
|
+
base.where(clauses.join(" OR "))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.phone_sql
|
|
25
|
+
["extended_data ? :key", { key: "whatsapp_phone_number" }]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.not_unsubscribed_sql(chatbot)
|
|
29
|
+
ActiveRecord::Base.sanitize_sql_array(
|
|
30
|
+
[
|
|
31
|
+
"NOT (coalesce(extended_data, '{}'::jsonb) @> ?::jsonb)",
|
|
32
|
+
{ chatbot.subscription_key => false }.to_json
|
|
33
|
+
]
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(chatbot)
|
|
38
|
+
@chatbot = chatbot
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def query
|
|
42
|
+
Decidim::User.where(organization: chatbot.organization).where(self.class.phone_sql)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def subscribed
|
|
46
|
+
query.where.not(unsubscribed_clause)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
attr_reader :chatbot
|
|
52
|
+
|
|
53
|
+
def unsubscribed_clause
|
|
54
|
+
["coalesce(extended_data, '{}'::jsonb) @> ?::jsonb", { chatbot.subscription_key => false }.to_json]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotMenuItemSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
COMPONENT_MANIFESTS = %w(read_blogs read_proposals propose_proposals vote_on_proposal).freeze
|
|
9
|
+
RESPONSE_LABEL_MANIFESTS = %w(login exit).freeze
|
|
10
|
+
MENU_BUTTON_MANIFESTS = %w(read_blogs read_proposals vote_on_proposal).freeze
|
|
11
|
+
READ_MORE_MANIFESTS = %w(read_blogs read_proposals).freeze
|
|
12
|
+
PROPOSE_PROPOSALS_MANIFESTS = %w(propose_proposals).freeze
|
|
13
|
+
ACTION_PROMPT_MANIFESTS = %w(propose_proposals vote_on_proposal).freeze
|
|
14
|
+
VOTE_OPTIONAL_LABELS = %w(vote_label weight_1_label weight_2_label weight_3_label abstention_label).freeze
|
|
15
|
+
|
|
16
|
+
set_type :chatbot_menu_item
|
|
17
|
+
|
|
18
|
+
attribute :manifest_name, &:manifest_name
|
|
19
|
+
attribute :enabled, &:enabled
|
|
20
|
+
attribute :position, &:position
|
|
21
|
+
attribute :chatbot_id do |item|
|
|
22
|
+
item.decidim_chatbot_id.to_s
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
attribute :title do |item, params|
|
|
26
|
+
translated_field(item.title, params[:locales])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
attribute :introduction do |item, params|
|
|
30
|
+
translated_field(item.introduction, params[:locales])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
attribute :component_id, if: proc { |item| COMPONENT_MANIFESTS.include?(item.manifest_name) } do |item|
|
|
34
|
+
item.settings["component_id"]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
attribute :redirect_path, if: proc { |item| item.manifest_name == "login" } do |item|
|
|
38
|
+
item.settings["redirect_path"]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
attribute :response_label, if: proc { |item| RESPONSE_LABEL_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
42
|
+
translated_setting(item.settings["response_label"], params[:locales])
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
attribute :menu_button_label, if: proc { |item| MENU_BUTTON_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
46
|
+
translated_setting(item.settings["menu_button_label"], params[:locales])
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
attribute :menu_connector_label, if: proc { |item| item.manifest_name == "vote_on_proposal" } do |item, params|
|
|
50
|
+
translated_setting(item.settings["menu_connector_label"], params[:locales])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
attribute :skip_menu_button, if: proc { |item| item.manifest_name == "vote_on_proposal" } do |item, params|
|
|
54
|
+
translated_setting(item.settings["skip_menu_button"], params[:locales])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
attribute :read_more_button_label, if: proc { |item| READ_MORE_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
58
|
+
translated_setting(item.settings["read_more_button_label"], params[:locales])
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
attribute :nothing_to_read_label, if: proc { |item| READ_MORE_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
62
|
+
translated_setting(item.settings["nothing_to_read_label"], params[:locales])
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
attribute :action_prompt_label, if: proc { |item| ACTION_PROMPT_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
66
|
+
translated_setting(item.settings["action_prompt_label"], params[:locales])
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
Decidim::Chatbot::MenuItemLabelKeys::PROPOSE_PROPOSALS.excluding(:action_prompt_label).each do |attr|
|
|
70
|
+
attribute attr, if: proc { |item| PROPOSE_PROPOSALS_MANIFESTS.include?(item.manifest_name) } do |item, params|
|
|
71
|
+
translated_setting(item.settings[attr.to_s], params[:locales])
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
VOTE_OPTIONAL_LABELS.each do |attr|
|
|
76
|
+
attribute attr, if: proc { |item| translated_setting_present?(item.settings[attr]) } do |item, params|
|
|
77
|
+
translated_setting(item.settings[attr], params[:locales])
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
attribute :created_at do |item|
|
|
82
|
+
item.created_at&.iso8601
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
attribute :updated_at do |item|
|
|
86
|
+
item.updated_at&.iso8601
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# jsonapi-serializer 2.2 has no block set_type; stamp discriminator type here.
|
|
90
|
+
def self.record_hash(record, fieldset, includes_list, params = {})
|
|
91
|
+
hash = super
|
|
92
|
+
hash[:type] = "chatbot_menu_item_#{record.manifest_name}"
|
|
93
|
+
hash
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.translated_setting(value, locales)
|
|
97
|
+
return translated_field(value, locales) if value.is_a?(Hash)
|
|
98
|
+
return {} if value.blank?
|
|
99
|
+
|
|
100
|
+
locale = Array(locales).first || :en
|
|
101
|
+
translated_field({ locale.to_s => value.to_s }, locales)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def self.translated_setting_present?(value)
|
|
105
|
+
return value.values.any?(&:present?) if value.is_a?(Hash)
|
|
106
|
+
|
|
107
|
+
value.present?
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotNotificationSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
set_type :chatbot_notification
|
|
9
|
+
|
|
10
|
+
def self.record_hash(record, fieldset, includes_list, params = {})
|
|
11
|
+
hash = super
|
|
12
|
+
hash[:type] = "chatbot_notification_#{record.manifest_name}"
|
|
13
|
+
hash
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
set_type :chatbot
|
|
9
|
+
|
|
10
|
+
attributes :whatsapp_phone_id,
|
|
11
|
+
:whatsapp_display_name,
|
|
12
|
+
:whatsapp_phone_number,
|
|
13
|
+
:whatsapp_phone_status
|
|
14
|
+
|
|
15
|
+
attribute :name do |chatbot, params|
|
|
16
|
+
translated_field(chatbot.name, params[:locales])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
attribute :welcome_message do |chatbot, params|
|
|
20
|
+
translated_field(chatbot.welcome_message, params[:locales])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attribute :menu_intro do |chatbot, params|
|
|
24
|
+
translated_field(chatbot.menu_intro, params[:locales])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attribute :menu_connector do |chatbot, params|
|
|
28
|
+
translated_field(chatbot.menu_connector, params[:locales])
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attribute :error_404 do |chatbot, params|
|
|
32
|
+
translated_field(chatbot.error_404, params[:locales])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
attribute :error_502 do |chatbot, params|
|
|
36
|
+
translated_field(chatbot.error_502, params[:locales])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
attribute :error_401 do |chatbot, params|
|
|
40
|
+
translated_field(chatbot.error_401, params[:locales])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
attribute :created_at do |chatbot|
|
|
44
|
+
chatbot.created_at&.iso8601
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
attribute :updated_at do |chatbot|
|
|
48
|
+
chatbot.updated_at&.iso8601
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
has_many :menu_items, serializer: ChatbotMenuItemSerializer do |chatbot|
|
|
52
|
+
chatbot.items.for_scope(:menu)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
has_many :notifications, serializer: ChatbotNotificationSerializer do |chatbot|
|
|
56
|
+
chatbot.items.for_scope(:notification)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
has_many :users, serializer: ChatbotUserSerializer do |chatbot|
|
|
60
|
+
Decidim::Chatbot::WhatsappUsers.subscribed_for(chatbot)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotUserIdentitySerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
set_type :chatbot_user_identity
|
|
9
|
+
|
|
10
|
+
attributes :provider, :uid
|
|
11
|
+
|
|
12
|
+
attribute :created_at do |identity|
|
|
13
|
+
identity.created_at&.iso8601
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attribute :updated_at do |identity|
|
|
17
|
+
identity.updated_at&.iso8601
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotUserSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
set_type :chatbot_user
|
|
9
|
+
|
|
10
|
+
attribute :whatsapp_phone_number do |user|
|
|
11
|
+
user.extended_data["whatsapp_phone_number"]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attribute :subscribed_chatbot_ids do |user, params|
|
|
15
|
+
org = params[:organization] || user.organization
|
|
16
|
+
Decidim::Chatbot::Chatbot.for_organization(org).filter_map do |chatbot|
|
|
17
|
+
next if user.extended_data[chatbot.subscription_key] == false
|
|
18
|
+
|
|
19
|
+
chatbot.id.to_s
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
has_many :identities, serializer: ChatbotUserIdentitySerializer, &:identities
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotWebhookSerializer < ::Decidim::Api::RestFull::Core::WebhookSerializer
|
|
8
|
+
def self.find_example_resource(organization, event_name:) # rubocop:disable Lint/UnusedMethodArgument
|
|
9
|
+
Decidim::Chatbot::Chatbot.for_organization(organization).order(:id).last
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
protected
|
|
13
|
+
|
|
14
|
+
def resource_serializable_hash
|
|
15
|
+
ChatbotSerializer.new(resource, params: serializer_params).serializable_hash
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
# Maps Decidim chatbot notifications to RestFull webhook catalog events.
|
|
6
|
+
module ChatbotWebhookHandler
|
|
7
|
+
EVENT_MAP = {
|
|
8
|
+
WebhookPublisher::SETTINGS_EVENT => "chatbot.settings_updated.succeeded",
|
|
9
|
+
WebhookPublisher::MENU_EVENT => "chatbot.menu_updated.succeeded"
|
|
10
|
+
}.freeze
|
|
11
|
+
|
|
12
|
+
def self.call(event_name, data)
|
|
13
|
+
webhook_event = EVENT_MAP[event_name]
|
|
14
|
+
return unless webhook_event
|
|
15
|
+
|
|
16
|
+
payload = normalize_payload(data)
|
|
17
|
+
chatbot = payload[:resource]
|
|
18
|
+
return unless chatbot.is_a?(::Decidim::Chatbot::Chatbot)
|
|
19
|
+
|
|
20
|
+
organization = chatbot.organization
|
|
21
|
+
return unless Decidim::RestFull::Core::ModuleAvailability.module_enabled?(organization, :chatbot)
|
|
22
|
+
|
|
23
|
+
ChatbotWebhookJob.perform_later(webhook_event, chatbot.id, organization.id)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.normalize_payload(data)
|
|
27
|
+
case data
|
|
28
|
+
when Hash
|
|
29
|
+
data.symbolize_keys
|
|
30
|
+
when ActiveSupport::Notifications::Event
|
|
31
|
+
data.payload.symbolize_keys
|
|
32
|
+
else
|
|
33
|
+
{}
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private_class_method :normalize_payload
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|