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,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Whatsapp
|
|
6
|
+
# Creates (or re-syncs) an org-scoped chatbot from a Meta phone id.
|
|
7
|
+
class CreateFromPhone < Decidim::Command
|
|
8
|
+
def initialize(organization:, phone_id:, client: nil)
|
|
9
|
+
@organization = organization
|
|
10
|
+
@phone_id = phone_id.to_s
|
|
11
|
+
@client = client
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
reason = precondition_error
|
|
16
|
+
return broadcast(:invalid, reason) if reason
|
|
17
|
+
|
|
18
|
+
persist_from_graph
|
|
19
|
+
rescue StandardError => e
|
|
20
|
+
report_graph_error(e)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
attr_reader :organization, :phone_id, :client, :chatbot
|
|
26
|
+
|
|
27
|
+
def report_graph_error(error)
|
|
28
|
+
Rails.logger.error("[Decidim::Chatbot::Whatsapp::CreateFromPhone] #{error.class}: #{error.message}")
|
|
29
|
+
broadcast(:invalid, :graph_error, "#{error.class}: #{error.message}")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def precondition_error
|
|
33
|
+
return :disabled unless credentials[:enabled]
|
|
34
|
+
return :missing_credentials unless credentials_present?
|
|
35
|
+
return :phone_belongs_to_other_organization if foreign_phone?
|
|
36
|
+
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def persist_from_graph
|
|
41
|
+
details = graph_client.fetch_phone(phone_id)
|
|
42
|
+
return broadcast(:invalid, :phone_not_found) unless details
|
|
43
|
+
|
|
44
|
+
persist!(details)
|
|
45
|
+
broadcast(:ok, chatbot)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def credentials
|
|
49
|
+
@credentials ||= Decidim::Chatbot.credentials_for(organization)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def credentials_present?
|
|
53
|
+
credentials[:access_token].present? && credentials[:business_id].present?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def existing
|
|
57
|
+
@existing ||= ::Decidim::Chatbot::Chatbot.find_by(whatsapp_phone_id: phone_id)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def foreign_phone?
|
|
61
|
+
existing && existing.decidim_organization_id != organization.id
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def graph_client
|
|
65
|
+
client || GraphClient.new(
|
|
66
|
+
access_token: credentials[:access_token],
|
|
67
|
+
business_id: credentials[:business_id]
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def persist!(details)
|
|
72
|
+
@chatbot = existing || ::Decidim::Chatbot::Chatbot.new(whatsapp_phone_id: phone_id, organization:)
|
|
73
|
+
chatbot.assign_attributes(
|
|
74
|
+
whatsapp_display_name: details[:verified_name],
|
|
75
|
+
whatsapp_phone_number: details[:display_phone_number],
|
|
76
|
+
whatsapp_phone_status: StatusMapper.call(details),
|
|
77
|
+
name: named? ? chatbot.name : default_name(details),
|
|
78
|
+
welcome_message: chatbot.welcome_message.presence || {},
|
|
79
|
+
synced_at: Time.current
|
|
80
|
+
)
|
|
81
|
+
chatbot.save!
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def named?
|
|
85
|
+
Array((chatbot.name || {}).values).any?(&:present?)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def default_name(details)
|
|
89
|
+
label = details[:verified_name].presence || details[:display_phone_number].presence || phone_id
|
|
90
|
+
{ organization.default_locale.to_s => label }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Whatsapp
|
|
6
|
+
# Syncs WhatsApp phone fields on a chatbot from Meta Graph.
|
|
7
|
+
# Pass +client:+ when the caller already built a GraphClient for the org
|
|
8
|
+
# (e.g. chatbot:sync looping organizations).
|
|
9
|
+
class SyncPhone < Decidim::Command
|
|
10
|
+
def initialize(chatbot, client: nil)
|
|
11
|
+
@chatbot = chatbot
|
|
12
|
+
@client = client
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call
|
|
16
|
+
graph = resolve_client
|
|
17
|
+
return broadcast(:invalid, :missing_credentials) unless graph
|
|
18
|
+
|
|
19
|
+
details = graph.fetch_phone(chatbot.whatsapp_phone_id)
|
|
20
|
+
return broadcast(:invalid, :phone_not_found) unless details
|
|
21
|
+
|
|
22
|
+
chatbot.update!(
|
|
23
|
+
whatsapp_display_name: details[:verified_name],
|
|
24
|
+
whatsapp_phone_number: details[:display_phone_number],
|
|
25
|
+
whatsapp_phone_status: StatusMapper.call(details),
|
|
26
|
+
synced_at: Time.current
|
|
27
|
+
)
|
|
28
|
+
broadcast(:ok, chatbot)
|
|
29
|
+
rescue StandardError => e
|
|
30
|
+
report_graph_error(e)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
attr_reader :chatbot, :client
|
|
36
|
+
|
|
37
|
+
def report_graph_error(error)
|
|
38
|
+
Rails.logger.error("[Decidim::Chatbot::Whatsapp::SyncPhone] #{error.class}: #{error.message}")
|
|
39
|
+
broadcast(:invalid, :graph_error, "#{error.class}: #{error.message}")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def resolve_client
|
|
43
|
+
return client if client
|
|
44
|
+
|
|
45
|
+
credentials = Decidim::Chatbot.credentials_for(chatbot.organization)
|
|
46
|
+
return if credentials[:access_token].blank? || credentials[:business_id].blank?
|
|
47
|
+
|
|
48
|
+
GraphClient.new(
|
|
49
|
+
access_token: credentials[:access_token],
|
|
50
|
+
business_id: credentials[:business_id]
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class BaseController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
rescue_from Decidim::RestFull::Core::ApiException::NotImplemented do |exception|
|
|
9
|
+
render status: :not_implemented,
|
|
10
|
+
json: {
|
|
11
|
+
error: "501: Not implemented",
|
|
12
|
+
error_description: exception.message
|
|
13
|
+
}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
before_action { doorkeeper_authorize! :whatsapp }
|
|
17
|
+
before_action { ability.authorize! :read, ::Decidim::Chatbot::Chatbot }
|
|
18
|
+
before_action :ensure_chatbot_enabled!
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def ensure_chatbot_enabled!
|
|
23
|
+
return if Decidim::Chatbot.enabled_for?(current_organization)
|
|
24
|
+
|
|
25
|
+
raise Decidim::RestFull::Core::ApiException::NotImplemented,
|
|
26
|
+
I18n.t("decidim.chatbot.api.errors.not_implemented")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def serializer_params
|
|
30
|
+
{
|
|
31
|
+
host: current_organization.host,
|
|
32
|
+
locales: available_locales
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def render_index(payload, scope, meta)
|
|
37
|
+
payload[:meta] = meta
|
|
38
|
+
render_json_with_conditional_get(payload, fingerprint: collection_fingerprint_for(scope))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def render_show(payload, resource)
|
|
42
|
+
render_json_with_conditional_get(payload, fingerprint: resource_fingerprint_for(resource))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def filter_value(key)
|
|
46
|
+
params.dig(:filter, key).presence
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotMenuItemsController < BaseController
|
|
8
|
+
def index
|
|
9
|
+
scope = filtered_items
|
|
10
|
+
records, meta = paginate_collection(scope)
|
|
11
|
+
payload = ChatbotMenuItemSerializer.new(records, params: serializer_params).serializable_hash
|
|
12
|
+
render_index(payload, scope, meta)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def show
|
|
16
|
+
resource = menu_items_scope.find_by(id: params.require(:id))
|
|
17
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, I18n.t("decidim.chatbot.api.errors.menu_item_not_found") unless resource
|
|
18
|
+
|
|
19
|
+
payload = ChatbotMenuItemSerializer.new(resource, params: serializer_params).serializable_hash
|
|
20
|
+
render_show(payload, resource)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def menu_items_scope
|
|
26
|
+
Decidim::Chatbot::Item
|
|
27
|
+
.joins(:chatbot)
|
|
28
|
+
.merge(Decidim::Chatbot::Chatbot.for_organization(current_organization))
|
|
29
|
+
.for_scope(:menu)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def filtered_items
|
|
33
|
+
scope = menu_items_scope
|
|
34
|
+
scope = scope.where(decidim_chatbot_id: filter_value("chatbot_id_eq")) if filter_value("chatbot_id_eq")
|
|
35
|
+
scope = apply_enabled_filter(scope)
|
|
36
|
+
scope.order(:position, :id)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def apply_enabled_filter(scope)
|
|
40
|
+
raw = filter_value("enabled_eq")
|
|
41
|
+
return scope if raw.nil?
|
|
42
|
+
|
|
43
|
+
scope.where(enabled: ActiveModel::Type::Boolean.new.cast(raw))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotUsersController < BaseController
|
|
8
|
+
def index
|
|
9
|
+
scope = users_scope.order(:id)
|
|
10
|
+
records, meta = paginate_collection(scope)
|
|
11
|
+
payload = ChatbotUserSerializer.new(records, params: user_serializer_params).serializable_hash
|
|
12
|
+
render_index(payload, scope, meta)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def show
|
|
16
|
+
resource = users_scope.find_by(id: params.require(:id))
|
|
17
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, I18n.t("decidim.chatbot.api.errors.user_not_found") unless resource
|
|
18
|
+
|
|
19
|
+
payload = ChatbotUserSerializer.new(resource, params: user_serializer_params).serializable_hash
|
|
20
|
+
render_show(payload, resource)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def users_scope
|
|
26
|
+
chatbot_id = filter_value("chatbot_id_eq")
|
|
27
|
+
return subscribed_for_chatbot(chatbot_id) if chatbot_id
|
|
28
|
+
|
|
29
|
+
Decidim::Chatbot::WhatsappUsers.subscribed_for_organization(current_organization)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def subscribed_for_chatbot(chatbot_id)
|
|
33
|
+
chatbot = Decidim::Chatbot::Chatbot.for_organization(current_organization).find_by(id: chatbot_id)
|
|
34
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, I18n.t("decidim.chatbot.api.errors.not_found") unless chatbot
|
|
35
|
+
|
|
36
|
+
Decidim::Chatbot::WhatsappUsers.subscribed_for(chatbot)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def user_serializer_params
|
|
40
|
+
serializer_params.merge(organization: current_organization)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Chatbot
|
|
7
|
+
class ChatbotsController < BaseController
|
|
8
|
+
def index
|
|
9
|
+
scope = filtered_chatbots.order(:id)
|
|
10
|
+
records, meta = paginate_collection(scope)
|
|
11
|
+
payload = ChatbotSerializer.new(records, params: serializer_params).serializable_hash
|
|
12
|
+
render_index(payload, scope, meta)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def show
|
|
16
|
+
resource = chatbots_scope.find_by(id: params.require(:id))
|
|
17
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, I18n.t("decidim.chatbot.api.errors.not_found") unless resource
|
|
18
|
+
|
|
19
|
+
payload = ChatbotSerializer.new(resource, params: serializer_params).serializable_hash
|
|
20
|
+
render_show(payload, resource)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def chatbots_scope
|
|
26
|
+
Decidim::Chatbot::Chatbot.for_organization(current_organization)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def filtered_chatbots
|
|
30
|
+
scope = chatbots_scope
|
|
31
|
+
scope = scope.where(whatsapp_phone_id: filter_value("whatsapp_phone_id_eq")) if filter_value("whatsapp_phone_id_eq")
|
|
32
|
+
scope = scope.where(whatsapp_phone_number: filter_value("whatsapp_phone_number_eq")) if filter_value("whatsapp_phone_number_eq")
|
|
33
|
+
scope = scope.where(whatsapp_display_name: filter_value("whatsapp_display_name_eq")) if filter_value("whatsapp_display_name_eq")
|
|
34
|
+
scope
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Shared org-scoped chatbot access for admin controllers.
|
|
7
|
+
module ChatbotEdit
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
TAB_ONBOARDING = {
|
|
11
|
+
"generals" => "general",
|
|
12
|
+
"whatsapps" => "whatsapp",
|
|
13
|
+
"menu_items" => "menu",
|
|
14
|
+
"notifications" => "notification",
|
|
15
|
+
"users" => "users"
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
included do
|
|
19
|
+
layout "decidim/admin/settings"
|
|
20
|
+
add_breadcrumb_item_from_menu :admin_settings_menu
|
|
21
|
+
before_action :ensure_chatbot_enabled!
|
|
22
|
+
before_action :append_chatbot_breadcrumb, if: :chatbot_resource?
|
|
23
|
+
helper_method :chatbots, :chatbot, :chatbot_breadcrumb_label, :tab_onboarding
|
|
24
|
+
helper Decidim::Chatbot::Admin::ItemsHelper
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def ensure_chatbot_enabled!
|
|
30
|
+
return if Decidim::Chatbot.enabled_for?(current_organization)
|
|
31
|
+
|
|
32
|
+
flash[:alert] = I18n.t("decidim.chatbot.admin.disabled")
|
|
33
|
+
redirect_to decidim.root_path
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def chatbots
|
|
37
|
+
@chatbots ||= Decidim::Chatbot::Chatbot.for_organization(current_organization).order(:id)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def chatbot
|
|
41
|
+
@chatbot ||= chatbots.find(chatbot_param_id)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def chatbot_param_id
|
|
45
|
+
params[:chatbot_id].presence || params.require(:id)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def authorize_org!
|
|
49
|
+
enforce_permission_to :update, :organization, organization: current_organization
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def chatbot_resource?
|
|
53
|
+
params[:chatbot_id].present? || member_chatbot_id?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def member_chatbot_id?
|
|
57
|
+
controller_name == "chatbots" && params[:id].present?
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def append_chatbot_breadcrumb
|
|
61
|
+
controller_breadcrumb_items << chatbot_crumb
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def chatbot_crumb
|
|
65
|
+
{ label: chatbot_breadcrumb_label, url: edit_chatbot_general_path(chatbot) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def chatbot_breadcrumb_label
|
|
69
|
+
chatbot.whatsapp_display_name.presence || chatbot_name_label
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def chatbot_name_label
|
|
73
|
+
chatbot.name[I18n.locale.to_s].presence || chatbot.name.values.first
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def tab_onboarding
|
|
77
|
+
key = TAB_ONBOARDING[controller_name]
|
|
78
|
+
return if key.blank?
|
|
79
|
+
|
|
80
|
+
I18n.t(key, scope: "decidim.chatbot.admin.tabs.onboarding")
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Chatbots
|
|
7
|
+
class GeneralsController < Decidim::Admin::ApplicationController
|
|
8
|
+
include Decidim::Chatbot::Admin::ChatbotEdit
|
|
9
|
+
|
|
10
|
+
def edit
|
|
11
|
+
authorize_org!
|
|
12
|
+
@form = form(ChatbotForm).from_model(chatbot)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def update
|
|
16
|
+
authorize_org!
|
|
17
|
+
@form = form(ChatbotForm).from_params(params)
|
|
18
|
+
save_welcome
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def save_welcome
|
|
24
|
+
UpdateChatbot.call(@form, chatbot) do
|
|
25
|
+
on(:ok) { redirect_after_update }
|
|
26
|
+
on(:invalid) { render_invalid }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def redirect_after_update
|
|
31
|
+
flash[:notice] = I18n.t("chatbots.update.success", scope: "decidim.chatbot.admin")
|
|
32
|
+
redirect_to edit_chatbot_general_path(chatbot)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def render_invalid
|
|
36
|
+
flash.now[:alert] = I18n.t("chatbots.update.error", scope: "decidim.chatbot.admin")
|
|
37
|
+
render :edit, status: :unprocessable_content
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Chatbots
|
|
7
|
+
class MenuItemsController < Decidim::Admin::ApplicationController
|
|
8
|
+
include Decidim::Chatbot::Admin::ChatbotEdit
|
|
9
|
+
include Decidim::Chatbot::Admin::ItemsCrud
|
|
10
|
+
|
|
11
|
+
def reorder
|
|
12
|
+
authorize_org!
|
|
13
|
+
run_reorder
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def item_scope
|
|
19
|
+
:menu
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def run_reorder
|
|
23
|
+
ReorderItems.call(collection, params[:order_ids]) do
|
|
24
|
+
on(:ok) { head :ok }
|
|
25
|
+
on(:invalid) { head :bad_request }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Chatbots
|
|
7
|
+
class NotificationsController < Decidim::Admin::ApplicationController
|
|
8
|
+
include Decidim::Chatbot::Admin::ChatbotEdit
|
|
9
|
+
include Decidim::Chatbot::Admin::ItemsCrud
|
|
10
|
+
|
|
11
|
+
helper_method :subscribed_recipients_count
|
|
12
|
+
before_action :append_test_breadcrumb, only: :test
|
|
13
|
+
|
|
14
|
+
def test
|
|
15
|
+
authorize_org!
|
|
16
|
+
return deliver_test_message if request.post?
|
|
17
|
+
|
|
18
|
+
@form = build_test_form.hydrate
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def deliver
|
|
22
|
+
authorize_org!
|
|
23
|
+
send_blast
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def item_scope
|
|
29
|
+
:notification
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def subscribed_recipients_count
|
|
33
|
+
WhatsappUsers.new(chatbot).subscribed.count
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def append_test_breadcrumb
|
|
37
|
+
controller_breadcrumb_items << {
|
|
38
|
+
label: I18n.t("items.test_breadcrumb", scope: "decidim.chatbot.admin"),
|
|
39
|
+
url: ""
|
|
40
|
+
}
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def deliver_test_message
|
|
44
|
+
@form = parse_test_form
|
|
45
|
+
SendTestNotification.call(@form) do
|
|
46
|
+
on(:ok) { redirect_test_ok }
|
|
47
|
+
on(:invalid) { |reason = nil, detail = nil| render_test_invalid(reason, detail) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def send_blast
|
|
52
|
+
Decidim::Chatbot::Notifications::Deliver.call(item, current_user) do
|
|
53
|
+
on(:ok) { redirect_sync(:notice, "items.send.success") }
|
|
54
|
+
on(:invalid) { redirect_sync(:alert, "items.send.error") }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def redirect_sync(kind, key)
|
|
59
|
+
flash[kind] = I18n.t(key, scope: "decidim.chatbot.admin")
|
|
60
|
+
redirect_to edit_chatbot_notification_path(chatbot, item)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def redirect_test_ok
|
|
64
|
+
flash[:notice] = I18n.t("items.test.success", scope: "decidim.chatbot.admin")
|
|
65
|
+
redirect_to chatbot_notifications_path(chatbot)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def render_test_invalid(reason = nil, detail = nil)
|
|
69
|
+
flash.now[:alert] = test_invalid_alert(reason, detail)
|
|
70
|
+
render :test, status: :unprocessable_content
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_invalid_alert(reason, detail)
|
|
74
|
+
base = I18n.t("items.test.error", scope: "decidim.chatbot.admin")
|
|
75
|
+
return base unless reason == :graph_error || detail.present?
|
|
76
|
+
return base if detail.blank?
|
|
77
|
+
|
|
78
|
+
"#{base} #{detail}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def build_test_form
|
|
82
|
+
form(Notifications::TestForm).instance(item:)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def parse_test_form
|
|
86
|
+
form(Notifications::TestForm).from_params(params, item:)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Chatbots
|
|
7
|
+
module Users
|
|
8
|
+
module Filterable
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
include Decidim::Admin::Filterable
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def base_query
|
|
17
|
+
collection
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def search_field_predicate
|
|
21
|
+
:name_or_nickname_or_email_cont
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def filters
|
|
25
|
+
[]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
module Chatbots
|
|
7
|
+
class UsersController < Decidim::Admin::ApplicationController
|
|
8
|
+
include Decidim::Chatbot::Admin::ChatbotEdit
|
|
9
|
+
include Decidim::Chatbot::Admin::Chatbots::Users::Filterable
|
|
10
|
+
|
|
11
|
+
helper_method :whatsapp_subscribed?
|
|
12
|
+
|
|
13
|
+
def index
|
|
14
|
+
authorize_org!
|
|
15
|
+
@users = filtered_collection
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def unsubscribe
|
|
19
|
+
authorize_org!
|
|
20
|
+
run_unsubscribe
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def collection
|
|
26
|
+
@collection ||= WhatsappUsers.for(chatbot)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def user
|
|
30
|
+
@user ||= collection.find(params[:id])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def run_unsubscribe
|
|
34
|
+
UnsubscribeUser.call(user, chatbot) do
|
|
35
|
+
on(:ok) { redirect_users(:success) }
|
|
36
|
+
on(:invalid) { redirect_users(:error) }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def redirect_users(kind)
|
|
41
|
+
key = kind == :success ? :notice : :alert
|
|
42
|
+
flash[key] = I18n.t("users.unsubscribe_#{kind}", scope: "decidim.chatbot.admin")
|
|
43
|
+
redirect_to chatbot_users_path(chatbot)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def whatsapp_subscribed?(member)
|
|
47
|
+
member.extended_data[chatbot.subscription_key] != false
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|