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
data/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Decidim Chatbot
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="website/static/img/logo.svg" alt="Decidim Chatbot" width="120" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
[](LICENSE.md)
|
|
8
|
+
|
|
9
|
+
WhatsApp / chatbot administration for Decidim, plus RestFull Extension endpoints for chatbot configuration.
|
|
10
|
+
|
|
11
|
+
Requires Decidim `>= 0.32`, `< 0.33` (Ruby 3.4.7).
|
|
12
|
+
|
|
13
|
+
## Host app install
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem "decidim-chatbot"
|
|
17
|
+
gem "decidim-restfull-core",
|
|
18
|
+
git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full",
|
|
19
|
+
branch: "main"
|
|
20
|
+
gem "decidim-toggle",
|
|
21
|
+
git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle",
|
|
22
|
+
branch: "main"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bundle install
|
|
27
|
+
bundle exec rails decidim_chatbot:install:migrations
|
|
28
|
+
bundle exec rails decidim_toggle:install:migrations
|
|
29
|
+
bundle exec rails db:migrate
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Full walkthrough (System credentials, WhatsApp phone number ID, rake, admin, API): [Install and configure](website/docs/integrate/index.md).
|
|
33
|
+
|
|
34
|
+
In **System**: organization → **Chatbot** tab (Meta access token + business ID).
|
|
35
|
+
|
|
36
|
+
Then:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bundle exec rails chatbot:create PHONE_ID=123123 ORG_HOST=nca.voca.city
|
|
40
|
+
bundle exec rails chatbot:sync
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
In admin: **Configuration → Settings → Chatbot** (list phones; edit name + welcome message).
|
|
44
|
+
|
|
45
|
+
RestFull: API client scope `chatbot` → `GET …/chatbots` and `GET …/chatbots/:id`.
|
|
46
|
+
|
|
47
|
+
## Documentation
|
|
48
|
+
|
|
49
|
+
See the [Docusaurus site](website/) (`cd website && yarn && yarn start`).
|
|
50
|
+
|
|
51
|
+
## Development
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
docker compose up -d
|
|
55
|
+
docker compose exec chatbot bash -lc 'bundle && yarn && bundle exec rake test_app && ./bin/check'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
After a fresh clone (or after deleting `spec/decidim_dummy_app`), generate the test app once with `bundle exec rake test_app`. Then `./bin/check` runs RuboCop, erblint, Prettier, i18n-tasks, OpenAPI freshness, and RSpec. Compose sets `DATABASE_URL` for the app DB; `rake test_app` and `./bin/check` unset it so dummy apps use `database.yml` (`*_development` / `*_test`). Do not leave `DATABASE_URL` set when running RSpec on Ruby 3.4.
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
[AGPL-3.0](LICENSE.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "decidim/dev/common_rake"
|
|
4
|
+
|
|
5
|
+
def install_module(path)
|
|
6
|
+
Dir.chdir(path) do
|
|
7
|
+
# Compose sets DATABASE_URL → single "decidim" DB; RSpec/bin/check unset it and
|
|
8
|
+
# use database.yml (*_development / *_test). Drop the override for setup.
|
|
9
|
+
ENV.delete("DATABASE_URL")
|
|
10
|
+
|
|
11
|
+
# RestFull forces active_record_store; dummy apps need the sessions table.
|
|
12
|
+
# Spring + cache_classes=true breaks rails in the generated test env.
|
|
13
|
+
sh "sed -i 's/config.cache_classes = true/config.cache_classes = false/' ./config/environments/test.rb"
|
|
14
|
+
hosts_sed = "config.hosts.clear\\n config.host_authorization = { exclude: ->(_request) { true } }"
|
|
15
|
+
sh "grep -q 'config.hosts.clear' ./config/environments/test.rb || " \
|
|
16
|
+
"sed -i '/^end$/i\\ #{hosts_sed}' ./config/environments/test.rb"
|
|
17
|
+
sh "bundle exec rails generate active_record:session_migration"
|
|
18
|
+
sh "DISABLE_SPRING=1 bundle exec rails decidim_chatbot:install:migrations"
|
|
19
|
+
sh "DISABLE_SPRING=1 bundle exec rails decidim_toggle:install:migrations"
|
|
20
|
+
sh "DISABLE_SPRING=1 bundle exec rails db:migrate"
|
|
21
|
+
sh "DISABLE_SPRING=1 RAILS_ENV=test bundle exec rails db:drop db:create db:migrate"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def seed_db(path)
|
|
26
|
+
Dir.chdir(path) do
|
|
27
|
+
sh "bundle exec rake db:seed"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
desc "Generates a dummy app for testing"
|
|
32
|
+
task :test_app do
|
|
33
|
+
Rake::Task["decidim:generate_external_test_app"].invoke
|
|
34
|
+
install_module("spec/decidim_dummy_app")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
desc "Generates a development app."
|
|
38
|
+
task :development_app do
|
|
39
|
+
Bundler.with_original_env do
|
|
40
|
+
generate_decidim_app(
|
|
41
|
+
"development_app",
|
|
42
|
+
"--app_name",
|
|
43
|
+
"#{base_app_name}_development_app",
|
|
44
|
+
"--path",
|
|
45
|
+
"..",
|
|
46
|
+
"--recreate_db",
|
|
47
|
+
"--demo",
|
|
48
|
+
"--locales",
|
|
49
|
+
"en,ca,es,fr"
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
install_module("development_app")
|
|
53
|
+
seed_db("development_app")
|
|
54
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
class DestroyItem < Decidim::Command
|
|
7
|
+
def initialize(item)
|
|
8
|
+
@item = item
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
chatbot = item.chatbot
|
|
13
|
+
menu = item.scope.to_s == "menu"
|
|
14
|
+
item.destroy!
|
|
15
|
+
Decidim::Chatbot::WebhookPublisher.menu_updated(chatbot) if menu
|
|
16
|
+
broadcast(:ok)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
attr_reader :item
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Creates or updates a chatbot item from a type form.
|
|
7
|
+
class PersistItem < Decidim::Command
|
|
8
|
+
def initialize(form, item)
|
|
9
|
+
@form = form
|
|
10
|
+
@item = item
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
return broadcast(:invalid) if form.invalid?
|
|
15
|
+
|
|
16
|
+
item.assign_attributes(item_attributes)
|
|
17
|
+
item.save!
|
|
18
|
+
publish_menu_updated if menu_scope?
|
|
19
|
+
broadcast(:ok, item)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :form, :item
|
|
25
|
+
|
|
26
|
+
def item_attributes
|
|
27
|
+
{
|
|
28
|
+
title: form.title,
|
|
29
|
+
introduction: form.introduction,
|
|
30
|
+
settings: form.settings_hash,
|
|
31
|
+
manifest_name: form.manifest_name,
|
|
32
|
+
scope: form.scope_name,
|
|
33
|
+
enabled: form.enabled
|
|
34
|
+
}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def menu_scope?
|
|
38
|
+
item.scope.to_s == "menu"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def publish_menu_updated
|
|
42
|
+
Decidim::Chatbot::WebhookPublisher.menu_updated(item.chatbot)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Reorders items in one chatbot scope. Foreign ids are refused.
|
|
7
|
+
class ReorderItems < Decidim::Command
|
|
8
|
+
def initialize(items, order)
|
|
9
|
+
@items = items
|
|
10
|
+
@order = Array(order).map(&:to_s)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
return broadcast(:invalid) unless valid_order?
|
|
15
|
+
|
|
16
|
+
apply_order
|
|
17
|
+
publish_menu_updated if menu_scope?
|
|
18
|
+
broadcast(:ok)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
attr_reader :items, :order
|
|
24
|
+
|
|
25
|
+
def valid_order?
|
|
26
|
+
order.present? && items.present? && foreign_ids.empty?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def foreign_ids
|
|
30
|
+
order - items.map { |item| item.id.to_s }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def apply_order
|
|
34
|
+
transaction do
|
|
35
|
+
order.each_with_index do |id, index|
|
|
36
|
+
items.find_by(id:)&.update!(position: index)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def menu_scope?
|
|
42
|
+
items.first&.scope.to_s == "menu"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def publish_menu_updated
|
|
46
|
+
Decidim::Chatbot::WebhookPublisher.menu_updated(items.first.chatbot)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Sends one WhatsApp template message to the phone on the test form.
|
|
7
|
+
class SendTestNotification < Decidim::Command
|
|
8
|
+
def initialize(form, client: nil)
|
|
9
|
+
@form = form
|
|
10
|
+
@client = client
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
return broadcast(:invalid) if form.invalid?
|
|
15
|
+
return broadcast(:invalid, :missing_credentials) unless graph
|
|
16
|
+
|
|
17
|
+
deliver
|
|
18
|
+
broadcast(:ok)
|
|
19
|
+
rescue StandardError => e
|
|
20
|
+
report_graph_error(e)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
attr_reader :form, :client
|
|
26
|
+
|
|
27
|
+
def deliver
|
|
28
|
+
graph.send_template(
|
|
29
|
+
chatbot.whatsapp_phone_id,
|
|
30
|
+
to: form.normalized_phone,
|
|
31
|
+
template_id: form.template_id,
|
|
32
|
+
parameters: form.body_parameters
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def graph
|
|
37
|
+
@graph ||= client || build_client
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def build_client
|
|
41
|
+
creds = Decidim::Chatbot.credentials_for(chatbot.organization)
|
|
42
|
+
return if creds[:access_token].blank? || creds[:business_id].blank?
|
|
43
|
+
|
|
44
|
+
Whatsapp::GraphClient.new(
|
|
45
|
+
access_token: creds[:access_token],
|
|
46
|
+
business_id: creds[:business_id]
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def chatbot
|
|
51
|
+
form.item.chatbot
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def report_graph_error(error)
|
|
55
|
+
Rails.logger.error("[Decidim::Chatbot::Admin::SendTestNotification] #{error.class}: #{error.message}")
|
|
56
|
+
broadcast(:invalid, :graph_error, user_facing_graph_error(error))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def user_facing_graph_error(error)
|
|
60
|
+
extract_meta_message(error.message).presence || error.message.to_s
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def extract_meta_message(text)
|
|
64
|
+
json_part = text.to_s[/{.*}\z/m]
|
|
65
|
+
return if json_part.blank?
|
|
66
|
+
|
|
67
|
+
JSON.parse(json_part).dig("error", "message").presence
|
|
68
|
+
rescue JSON::ParserError
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Sets whatsapp_chatbot_<id>_subscribed to false on User.extended_data.
|
|
7
|
+
class UnsubscribeUser < Decidim::Command
|
|
8
|
+
def initialize(user, chatbot)
|
|
9
|
+
@user = user
|
|
10
|
+
@chatbot = chatbot
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
return broadcast(:invalid) unless same_organization?
|
|
15
|
+
|
|
16
|
+
write_flag
|
|
17
|
+
broadcast(:ok)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :user, :chatbot
|
|
23
|
+
|
|
24
|
+
def same_organization?
|
|
25
|
+
user.organization == chatbot.organization
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def write_flag
|
|
29
|
+
data = user.extended_data.merge(chatbot.subscription_key => false)
|
|
30
|
+
user.update!(extended_data: data)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Admin
|
|
6
|
+
# Updates org-owned copy. Name stays WhatsApp-synced.
|
|
7
|
+
class UpdateChatbot < Decidim::Commands::UpdateResource
|
|
8
|
+
fetch_form_attributes :welcome_message, :menu_intro, :menu_connector,
|
|
9
|
+
:error_404, :error_502, :error_401
|
|
10
|
+
|
|
11
|
+
def run_after_hooks
|
|
12
|
+
Decidim::Chatbot::WebhookPublisher.settings_updated(resource)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Notifications
|
|
6
|
+
# Enqueues a blast send for an enabled manual notification.
|
|
7
|
+
class Deliver < Decidim::Command
|
|
8
|
+
def initialize(item, user)
|
|
9
|
+
@item = item
|
|
10
|
+
@user = user
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def call
|
|
14
|
+
return broadcast(:invalid) unless deliverable?
|
|
15
|
+
|
|
16
|
+
SendNotificationJob.perform_later(item.id, user.id)
|
|
17
|
+
broadcast(:ok)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :item, :user
|
|
23
|
+
|
|
24
|
+
def deliverable?
|
|
25
|
+
item.manifest_name == "manual" && item.enabled? && item.template_id.present?
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Notifications
|
|
6
|
+
# Enqueues enabled phase notifications when a process step is activated.
|
|
7
|
+
class EnqueuePhase
|
|
8
|
+
def self.call(step)
|
|
9
|
+
new(step).call
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def initialize(step)
|
|
13
|
+
@step = step
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
return if step.blank?
|
|
18
|
+
|
|
19
|
+
matching_items.each { |item| enqueue(item) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :step
|
|
25
|
+
|
|
26
|
+
def matching_items
|
|
27
|
+
scoped_items.select { |item| matches_process?(item) && item.template_id.present? }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def scoped_items
|
|
31
|
+
Item.joins(:chatbot).merge(Chatbot.for_organization(organization))
|
|
32
|
+
.for_scope(:notification).where(manifest_name: "phase").enabled
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def matches_process?(item)
|
|
36
|
+
item.settings["participatory_space_gid"].to_s == process.to_gid.to_s
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def enqueue(item)
|
|
40
|
+
return if actor.blank?
|
|
41
|
+
|
|
42
|
+
SendNotificationJob.perform_later(item.id, actor.id)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def actor
|
|
46
|
+
@actor ||= logged_actor || fallback_admin
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def logged_actor
|
|
50
|
+
Decidim::ActionLog.where(resource: step, action: "activate").order(id: :desc).first&.user
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def fallback_admin
|
|
54
|
+
Decidim::User.where(organization:, admin: true).order(:id).first
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def process
|
|
58
|
+
step.participatory_process
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def organization
|
|
62
|
+
process.organization
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Chatbot
|
|
5
|
+
module Notifications
|
|
6
|
+
# Sends a template to subscribed WhatsApp users and writes an admin log.
|
|
7
|
+
class Send < Decidim::Command
|
|
8
|
+
def initialize(item, actor, client: nil)
|
|
9
|
+
@item = item
|
|
10
|
+
@actor = actor
|
|
11
|
+
@client = client
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
return broadcast(:invalid) unless sendable?
|
|
16
|
+
|
|
17
|
+
deliver_all
|
|
18
|
+
log_send
|
|
19
|
+
broadcast(:ok, counts)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
attr_reader :item, :actor, :client
|
|
25
|
+
|
|
26
|
+
# ponytail: ceiling — Graph rejects unknown template names at send time.
|
|
27
|
+
def sendable?
|
|
28
|
+
item.enabled? && item.template_id.present? && graph
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def deliver_all
|
|
32
|
+
recipients.find_each { |user| deliver_to(user) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def deliver_to(user)
|
|
36
|
+
graph.send_template(
|
|
37
|
+
item.chatbot.whatsapp_phone_id,
|
|
38
|
+
to: phone_for(user),
|
|
39
|
+
template_id: template_id_for(user),
|
|
40
|
+
parameters: parameters_for(user)
|
|
41
|
+
)
|
|
42
|
+
counts[:recipients_count] += 1
|
|
43
|
+
rescue StandardError
|
|
44
|
+
counts[:failures_count] += 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def recipients
|
|
48
|
+
WhatsappUsers.new(item.chatbot).subscribed
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def phone_for(user)
|
|
52
|
+
user.extended_data["whatsapp_phone_number"].to_s.gsub(/\D/, "")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def parameters_for(user)
|
|
56
|
+
Whatsapp::BodyParameters.from(expanded_variables(user))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def expanded_variables(user)
|
|
60
|
+
interpolator_for(user).expand(item.variables_hash_for(locale_for(user)))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def template_id_for(user)
|
|
64
|
+
item.template_id_for(locale_for(user))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def locale_for(user)
|
|
68
|
+
user.locale.presence || item.organization.default_locale.to_s
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def interpolator_for(user)
|
|
72
|
+
VariableInterpolator.new(Bindings.for(item, user))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# ponytail: ceiling — ActionLogger#scope treats Item#scope (String) as Decidim::Scope; use ActionLogger when that collision is gone.
|
|
76
|
+
def log_send
|
|
77
|
+
Decidim::ActionLog.create!(
|
|
78
|
+
organization: item.organization,
|
|
79
|
+
user: actor,
|
|
80
|
+
action: "send",
|
|
81
|
+
resource: item,
|
|
82
|
+
extra: counts.merge(user: log_user_extra),
|
|
83
|
+
visibility: "admin-only"
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def log_user_extra
|
|
88
|
+
{
|
|
89
|
+
ip: actor.current_sign_in_ip,
|
|
90
|
+
name: actor.name,
|
|
91
|
+
nickname: actor.nickname
|
|
92
|
+
}.compact
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def counts
|
|
96
|
+
@counts ||= { recipients_count: 0, failures_count: 0 }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def graph
|
|
100
|
+
@graph ||= client || build_client
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def build_client
|
|
104
|
+
credentials = Decidim::Chatbot.credentials_for(item.organization)
|
|
105
|
+
return if credentials[:access_token].blank? || credentials[:business_id].blank?
|
|
106
|
+
|
|
107
|
+
Whatsapp::GraphClient.new(
|
|
108
|
+
access_token: credentials[:access_token],
|
|
109
|
+
business_id: credentials[:business_id]
|
|
110
|
+
)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|