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.
Files changed (285) hide show
  1. checksums.yaml +7 -0
  2. data/.erb_lint.yml +2134 -0
  3. data/.github/workflows/website.yml +57 -0
  4. data/.gitignore +13 -0
  5. data/.gitlab-ci.yml +204 -0
  6. data/.node-version +1 -0
  7. data/.nvmrc +1 -0
  8. data/.rspec +2 -0
  9. data/.rubocop.yml +17 -0
  10. data/.ruby-version +1 -0
  11. data/.simplecov +36 -0
  12. data/CONTRIBUTING.md +54 -0
  13. data/Gemfile +50 -0
  14. data/Gemfile.lock +1013 -0
  15. data/LICENSE.md +661 -0
  16. data/README.md +62 -0
  17. data/Rakefile +54 -0
  18. data/app/commands/decidim/chatbot/admin/destroy_item.rb +25 -0
  19. data/app/commands/decidim/chatbot/admin/menu_items/update_exit.rb +12 -0
  20. data/app/commands/decidim/chatbot/admin/menu_items/update_login.rb +12 -0
  21. data/app/commands/decidim/chatbot/admin/menu_items/update_propose_proposals.rb +12 -0
  22. data/app/commands/decidim/chatbot/admin/menu_items/update_read_blogs.rb +12 -0
  23. data/app/commands/decidim/chatbot/admin/menu_items/update_read_proposals.rb +12 -0
  24. data/app/commands/decidim/chatbot/admin/menu_items/update_vote_on_proposal.rb +12 -0
  25. data/app/commands/decidim/chatbot/admin/notifications/update_manual.rb +12 -0
  26. data/app/commands/decidim/chatbot/admin/notifications/update_phase.rb +12 -0
  27. data/app/commands/decidim/chatbot/admin/persist_item.rb +47 -0
  28. data/app/commands/decidim/chatbot/admin/reorder_items.rb +51 -0
  29. data/app/commands/decidim/chatbot/admin/send_test_notification.rb +74 -0
  30. data/app/commands/decidim/chatbot/admin/unsubscribe_user.rb +35 -0
  31. data/app/commands/decidim/chatbot/admin/update_chatbot.rb +17 -0
  32. data/app/commands/decidim/chatbot/notifications/deliver.rb +30 -0
  33. data/app/commands/decidim/chatbot/notifications/enqueue_phase.rb +67 -0
  34. data/app/commands/decidim/chatbot/notifications/send.rb +115 -0
  35. data/app/commands/decidim/chatbot/whatsapp/create_from_phone.rb +95 -0
  36. data/app/commands/decidim/chatbot/whatsapp/sync_phone.rb +56 -0
  37. data/app/controllers/decidim/api/rest_full/chatbot/base_controller.rb +52 -0
  38. data/app/controllers/decidim/api/rest_full/chatbot/chatbot_menu_items_controller.rb +49 -0
  39. data/app/controllers/decidim/api/rest_full/chatbot/chatbot_users_controller.rb +46 -0
  40. data/app/controllers/decidim/api/rest_full/chatbot/chatbots_controller.rb +40 -0
  41. data/app/controllers/decidim/chatbot/admin/chatbot_edit.rb +85 -0
  42. data/app/controllers/decidim/chatbot/admin/chatbots/generals_controller.rb +43 -0
  43. data/app/controllers/decidim/chatbot/admin/chatbots/menu_items_controller.rb +32 -0
  44. data/app/controllers/decidim/chatbot/admin/chatbots/notifications_controller.rb +92 -0
  45. data/app/controllers/decidim/chatbot/admin/chatbots/users/filterable.rb +33 -0
  46. data/app/controllers/decidim/chatbot/admin/chatbots/users_controller.rb +53 -0
  47. data/app/controllers/decidim/chatbot/admin/chatbots/whatsapps_controller.rb +59 -0
  48. data/app/controllers/decidim/chatbot/admin/chatbots_controller.rb +20 -0
  49. data/app/controllers/decidim/chatbot/admin/items_crud.rb +183 -0
  50. data/app/forms/decidim/chatbot/admin/chatbot_form.rb +20 -0
  51. data/app/forms/decidim/chatbot/admin/config_form.rb +20 -0
  52. data/app/forms/decidim/chatbot/admin/menu_item_form.rb +80 -0
  53. data/app/forms/decidim/chatbot/admin/menu_items/component_id_form.rb +60 -0
  54. data/app/forms/decidim/chatbot/admin/menu_items/exit_form.rb +24 -0
  55. data/app/forms/decidim/chatbot/admin/menu_items/login_form.rb +35 -0
  56. data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_form.rb +23 -0
  57. data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_labels.rb +53 -0
  58. data/app/forms/decidim/chatbot/admin/menu_items/read_blogs_form.rb +23 -0
  59. data/app/forms/decidim/chatbot/admin/menu_items/read_content_labels.rb +51 -0
  60. data/app/forms/decidim/chatbot/admin/menu_items/read_proposals_form.rb +23 -0
  61. data/app/forms/decidim/chatbot/admin/menu_items/vote_on_proposal_form.rb +123 -0
  62. data/app/forms/decidim/chatbot/admin/notification_item_form.rb +111 -0
  63. data/app/forms/decidim/chatbot/admin/notifications/manual_form.rb +13 -0
  64. data/app/forms/decidim/chatbot/admin/notifications/phase_form.rb +79 -0
  65. data/app/forms/decidim/chatbot/admin/notifications/template_fields.rb +60 -0
  66. data/app/forms/decidim/chatbot/admin/notifications/test_form.rb +179 -0
  67. data/app/helpers/decidim/chatbot/admin/items_helper.rb +44 -0
  68. data/app/jobs/decidim/chatbot/chatbot_webhook_job.rb +59 -0
  69. data/app/jobs/decidim/chatbot/send_notification_job.rb +33 -0
  70. data/app/models/decidim/chatbot/chatbot.rb +47 -0
  71. data/app/models/decidim/chatbot/item.rb +99 -0
  72. data/app/packs/.keep +0 -0
  73. data/app/presenters/decidim/chatbot/admin_log/chatbot_presenter.rb +42 -0
  74. data/app/presenters/decidim/chatbot/admin_log/item_presenter.rb +32 -0
  75. data/app/queries/decidim/chatbot/admin/proposal_vote_mode.rb +41 -0
  76. data/app/queries/decidim/chatbot/whatsapp_users.rb +58 -0
  77. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer.rb +113 -0
  78. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_notification_serializer.rb +19 -0
  79. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_serializer.rb +66 -0
  80. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_identity_serializer.rb +23 -0
  81. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer.rb +28 -0
  82. data/app/serializers/decidim/api/rest_full/chatbot/chatbot_webhook_serializer.rb +21 -0
  83. data/app/services/decidim/chatbot/chatbot_webhook_handler.rb +40 -0
  84. data/app/services/decidim/chatbot/notifications/bindings.rb +87 -0
  85. data/app/services/decidim/chatbot/whatsapp/body_parameters.rb +29 -0
  86. data/app/services/decidim/chatbot/whatsapp/graph_client.rb +136 -0
  87. data/app/services/decidim/chatbot/whatsapp/status_mapper.rb +93 -0
  88. data/app/services/decidim/chatbot/whatsapp/template_manager_url.rb +35 -0
  89. data/app/views/decidim/chatbot/admin/chatbots/generals/_menu_preview.html.erb +49 -0
  90. data/app/views/decidim/chatbot/admin/chatbots/generals/edit.html.erb +79 -0
  91. data/app/views/decidim/chatbot/admin/chatbots/index.html.erb +50 -0
  92. data/app/views/decidim/chatbot/admin/chatbots/menu_items/_form.html.erb +2 -0
  93. data/app/views/decidim/chatbot/admin/chatbots/menu_items/edit.html.erb +2 -0
  94. data/app/views/decidim/chatbot/admin/chatbots/menu_items/index.html.erb +43 -0
  95. data/app/views/decidim/chatbot/admin/chatbots/menu_items/new.html.erb +12 -0
  96. data/app/views/decidim/chatbot/admin/chatbots/notifications/_form.html.erb +4 -0
  97. data/app/views/decidim/chatbot/admin/chatbots/notifications/_manual_fields.html.erb +8 -0
  98. data/app/views/decidim/chatbot/admin/chatbots/notifications/_phase_fields.html.erb +6 -0
  99. data/app/views/decidim/chatbot/admin/chatbots/notifications/edit.html.erb +3 -0
  100. data/app/views/decidim/chatbot/admin/chatbots/notifications/index.html.erb +40 -0
  101. data/app/views/decidim/chatbot/admin/chatbots/notifications/new.html.erb +11 -0
  102. data/app/views/decidim/chatbot/admin/chatbots/notifications/test.html.erb +35 -0
  103. data/app/views/decidim/chatbot/admin/chatbots/tabs/_whatsapp.html.erb +47 -0
  104. data/app/views/decidim/chatbot/admin/chatbots/users/_filters.html.erb +23 -0
  105. data/app/views/decidim/chatbot/admin/chatbots/users/index.html.erb +56 -0
  106. data/app/views/decidim/chatbot/admin/chatbots/whatsapps/show.html.erb +5 -0
  107. data/app/views/decidim/chatbot/admin/shared/_item_form.html.erb +64 -0
  108. data/app/views/decidim/chatbot/admin/shared/_row_actions.html.erb +28 -0
  109. data/app/views/decidim/chatbot/admin/shared/_tabs_menu.html.erb +13 -0
  110. data/app/views/decidim/chatbot/admin/shared/_type_fields.html.erb +2 -0
  111. data/app/views/decidim/chatbot/admin/shared/_type_picker.html.erb +15 -0
  112. data/app/views/decidim/chatbot/admin/shared/type_fields/_component_id.html.erb +9 -0
  113. data/app/views/decidim/chatbot/admin/shared/type_fields/_exit.html.erb +5 -0
  114. data/app/views/decidim/chatbot/admin/shared/type_fields/_login.html.erb +8 -0
  115. data/app/views/decidim/chatbot/admin/shared/type_fields/_manual.html.erb +16 -0
  116. data/app/views/decidim/chatbot/admin/shared/type_fields/_phase.html.erb +46 -0
  117. data/app/views/decidim/chatbot/admin/shared/type_fields/_propose_proposals.html.erb +10 -0
  118. data/app/views/decidim/chatbot/admin/shared/type_fields/_read_blogs.html.erb +16 -0
  119. data/app/views/decidim/chatbot/admin/shared/type_fields/_read_proposals.html.erb +16 -0
  120. data/app/views/decidim/chatbot/admin/shared/type_fields/_vote_on_proposal.html.erb +26 -0
  121. data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_abstention_label.html.erb +7 -0
  122. data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_vote_label.html.erb +7 -0
  123. data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_votes_closed_warning.html.erb +6 -0
  124. data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_weight_labels.html.erb +15 -0
  125. data/bin/check +13 -0
  126. data/bin/ci-setup +38 -0
  127. data/bin/gen-node-client +112 -0
  128. data/bin/gen-openapi +248 -0
  129. data/bin/i18n-tasks +27 -0
  130. data/bin/postversion +17 -0
  131. data/config/i18n-tasks.yml +38 -0
  132. data/config/locales/decidim_chatbot.en.yml +323 -0
  133. data/config/locales/decidim_chatbot.fr.yml +29 -0
  134. data/contrib/decidim-node-client/.gitignore +4 -0
  135. data/contrib/decidim-node-client/.npmignore +1 -0
  136. data/contrib/decidim-node-client/.openapi-generator/FILES +8 -0
  137. data/contrib/decidim-node-client/.openapi-generator/VERSION +1 -0
  138. data/contrib/decidim-node-client/.openapi-generator-ignore +13 -0
  139. data/contrib/decidim-node-client/.yarnrc.yml +5 -0
  140. data/contrib/decidim-node-client/README.md +37 -0
  141. data/contrib/decidim-node-client/api.ts +14193 -0
  142. data/contrib/decidim-node-client/base.ts +70 -0
  143. data/contrib/decidim-node-client/common.ts +185 -0
  144. data/contrib/decidim-node-client/configuration.ts +138 -0
  145. data/contrib/decidim-node-client/git_push.sh +57 -0
  146. data/contrib/decidim-node-client/index.ts +16 -0
  147. data/contrib/decidim-node-client/package.json +49 -0
  148. data/contrib/decidim-node-client/packup.config.ts +7 -0
  149. data/contrib/decidim-node-client/tsconfig.json +25 -0
  150. data/contrib/decidim-node-client/yarn.lock +2367 -0
  151. data/crowdin.yml +15 -0
  152. data/db/migrate/20260725120000_create_decidim_chatbot_chatbots.rb +20 -0
  153. data/db/migrate/20260822120000_create_decidim_chatbot_items.rb +20 -0
  154. data/db/migrate/20260824120000_add_menu_and_error_copy_to_decidim_chatbot_chatbots.rb +13 -0
  155. data/db/migrate/20260824140000_add_enabled_to_decidim_chatbot_items.rb +7 -0
  156. data/decidim-chatbot.gemspec +40 -0
  157. data/docker-compose.ci.yml +59 -0
  158. data/docker-compose.yml +47 -0
  159. data/lib/decidim/chatbot/admin_engine.rb +67 -0
  160. data/lib/decidim/chatbot/admin_menu.rb +41 -0
  161. data/lib/decidim/chatbot/default_manifests.rb +91 -0
  162. data/lib/decidim/chatbot/engine.rb +107 -0
  163. data/lib/decidim/chatbot/manifest.rb +31 -0
  164. data/lib/decidim/chatbot/menu_item_label_keys.rb +26 -0
  165. data/lib/decidim/chatbot/registry.rb +42 -0
  166. data/lib/decidim/chatbot/rest_full_ability.rb +24 -0
  167. data/lib/decidim/chatbot/settings_tab.rb +18 -0
  168. data/lib/decidim/chatbot/test_definitions.rb +352 -0
  169. data/lib/decidim/chatbot/variable_interpolator.rb +30 -0
  170. data/lib/decidim/chatbot/version.rb +21 -0
  171. data/lib/decidim/chatbot/webhook_publisher.rb +29 -0
  172. data/lib/decidim/chatbot.rb +41 -0
  173. data/lib/decidim-chatbot.rb +3 -0
  174. data/lib/tasks/chatbot.rake +64 -0
  175. data/openapi/openapi.json +37801 -0
  176. data/openapitools.json +7 -0
  177. data/package.json +22 -0
  178. data/prettier.config.js +15 -0
  179. data/spec/commands/decidim/chatbot/admin/destroy_item_spec.rb +24 -0
  180. data/spec/commands/decidim/chatbot/admin/persist_item_spec.rb +80 -0
  181. data/spec/commands/decidim/chatbot/admin/reorder_items_spec.rb +53 -0
  182. data/spec/commands/decidim/chatbot/admin/send_test_notification_spec.rb +105 -0
  183. data/spec/commands/decidim/chatbot/admin/unsubscribe_user_spec.rb +46 -0
  184. data/spec/commands/decidim/chatbot/admin/update_chatbot_spec.rb +56 -0
  185. data/spec/commands/decidim/chatbot/notifications/enqueue_phase_spec.rb +59 -0
  186. data/spec/commands/decidim/chatbot/notifications/send_spec.rb +101 -0
  187. data/spec/commands/decidim/chatbot/whatsapp/create_from_phone_spec.rb +122 -0
  188. data/spec/commands/decidim/chatbot/whatsapp/sync_phone_spec.rb +110 -0
  189. data/spec/controllers/decidim/chatbot/admin/chatbots_controller_spec.rb +25 -0
  190. data/spec/factories/chatbot.rb +44 -0
  191. data/spec/forms/decidim/chatbot/admin/chatbot_form_spec.rb +41 -0
  192. data/spec/forms/decidim/chatbot/admin/config_form_spec.rb +30 -0
  193. data/spec/forms/decidim/chatbot/admin/menu_item_form_spec.rb +49 -0
  194. data/spec/forms/decidim/chatbot/admin/menu_items/component_id_form_spec.rb +54 -0
  195. data/spec/forms/decidim/chatbot/admin/menu_items/component_id_forms_spec.rb +512 -0
  196. data/spec/forms/decidim/chatbot/admin/menu_items/exit_form_spec.rb +39 -0
  197. data/spec/forms/decidim/chatbot/admin/menu_items/login_form_spec.rb +101 -0
  198. data/spec/forms/decidim/chatbot/admin/menu_items/propose_proposals_form_prefill_spec.rb +26 -0
  199. data/spec/forms/decidim/chatbot/admin/menu_items/read_blogs_form_prefill_spec.rb +22 -0
  200. data/spec/forms/decidim/chatbot/admin/notifications/manual_form_spec.rb +130 -0
  201. data/spec/forms/decidim/chatbot/admin/notifications/phase_form_spec.rb +79 -0
  202. data/spec/forms/decidim/chatbot/admin/notifications/test_form_spec.rb +172 -0
  203. data/spec/jobs/decidim/chatbot/chatbot_webhook_job_spec.rb +47 -0
  204. data/spec/jobs/decidim/chatbot/send_notification_job_spec.rb +53 -0
  205. data/spec/lib/decidim/chatbot/engine_spec.rb +44 -0
  206. data/spec/lib/decidim/chatbot/registry_spec.rb +30 -0
  207. data/spec/lib/decidim/chatbot/settings_tab_spec.rb +21 -0
  208. data/spec/lib/decidim/chatbot/variable_interpolator_spec.rb +34 -0
  209. data/spec/lib/decidim/chatbot/whatsapp/status_mapper_spec.rb +64 -0
  210. data/spec/lib/decidim/chatbot_spec.rb +59 -0
  211. data/spec/models/decidim/chatbot/chatbot_spec.rb +23 -0
  212. data/spec/models/decidim/chatbot/item_spec.rb +88 -0
  213. data/spec/presenters/decidim/chatbot/admin_log/chatbot_presenter_spec.rb +64 -0
  214. data/spec/queries/decidim/chatbot/admin/proposal_vote_mode_spec.rb +74 -0
  215. data/spec/queries/decidim/chatbot/whatsapp_users_spec.rb +57 -0
  216. data/spec/requests/decidim/api/rest_full/chatbot/chatbot_menu_items_controller_spec.rb +344 -0
  217. data/spec/requests/decidim/api/rest_full/chatbot/chatbot_users_controller_spec.rb +140 -0
  218. data/spec/requests/decidim/api/rest_full/chatbot/chatbots_controller_spec.rb +195 -0
  219. data/spec/requests/decidim/chatbot/admin/chatbots/notifications_controller_spec.rb +44 -0
  220. data/spec/requests/decidim/chatbot/admin/tenant_isolation_spec.rb +133 -0
  221. data/spec/requests/decidim/chatbot/admin/unsubscribe_user_spec.rb +28 -0
  222. data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer_spec.rb +321 -0
  223. data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_serializer_spec.rb +49 -0
  224. data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer_spec.rb +40 -0
  225. data/spec/services/decidim/chatbot/chatbot_webhook_handler_spec.rb +43 -0
  226. data/spec/services/decidim/chatbot/notifications/bindings_spec.rb +86 -0
  227. data/spec/services/decidim/chatbot/whatsapp/body_parameters_spec.rb +22 -0
  228. data/spec/services/decidim/chatbot/whatsapp/graph_client_spec.rb +205 -0
  229. data/spec/services/decidim/chatbot/whatsapp/template_manager_url_spec.rb +33 -0
  230. data/spec/spec_helper.rb +33 -0
  231. data/spec/support/allow_all_hosts.rb +9 -0
  232. data/spec/support/chatbot_admin.rb +16 -0
  233. data/spec/support/decidim_factories.rb +7 -0
  234. data/spec/support/decidim_system_factories.rb +3 -0
  235. data/spec/support/devise.rb +5 -0
  236. data/spec/support/rack_test_system_driver.rb +43 -0
  237. data/spec/support/whatsapp_graph.rb +15 -0
  238. data/spec/swagger_helper.rb +47 -0
  239. data/spec/system/decidim/chatbot/admin/admin_can_send_manual_when_ready_spec.rb +35 -0
  240. data/spec/system/decidim/chatbot/admin/admin_configures_vote_on_proposal_menu_item_spec.rb +84 -0
  241. data/spec/system/decidim/chatbot/admin/admin_confirms_send_now_recipient_count_spec.rb +40 -0
  242. data/spec/system/decidim/chatbot/admin/admin_creates_menu_item_with_demo_field_spec.rb +65 -0
  243. data/spec/system/decidim/chatbot/admin/admin_creates_phase_and_manual_notifications_spec.rb +52 -0
  244. data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_org_processes_in_phase_select_spec.rb +27 -0
  245. data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_organization_chatbots_spec.rb +25 -0
  246. data/spec/system/decidim/chatbot/admin/admin_is_redirected_when_chatbot_is_disabled_spec.rb +28 -0
  247. data/spec/system/decidim/chatbot/admin/admin_lists_organization_chatbots_spec.rb +36 -0
  248. data/spec/system/decidim/chatbot/admin/admin_manual_notification_sends_with_nickname_spec.rb +68 -0
  249. data/spec/system/decidim/chatbot/admin/admin_navigates_chatbot_main_tabs_spec.rb +52 -0
  250. data/spec/system/decidim/chatbot/admin/admin_phase_notification_sends_on_step_change_spec.rb +79 -0
  251. data/spec/system/decidim/chatbot/admin/admin_picks_published_blog_component_spec.rb +205 -0
  252. data/spec/system/decidim/chatbot/admin/admin_picks_published_participatory_space_spec.rb +45 -0
  253. data/spec/system/decidim/chatbot/admin/admin_reorders_chatbot_menu_items_spec.rb +29 -0
  254. data/spec/system/decidim/chatbot/admin/admin_sees_disabled_menu_item_faded_on_the_table_spec.rb +23 -0
  255. data/spec/system/decidim/chatbot/admin/admin_sees_disabled_notification_item_faded_on_the_table_spec.rb +40 -0
  256. data/spec/system/decidim/chatbot/admin/admin_sees_notification_facts_and_frozen_template_name_spec.rb +71 -0
  257. data/spec/system/decidim/chatbot/admin/admin_sees_propose_proposals_defaults_prefilled_spec.rb +119 -0
  258. data/spec/system/decidim/chatbot/admin/admin_sees_type_specific_fields_spec.rb +89 -0
  259. data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_facts_and_can_sync_spec.rb +48 -0
  260. data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_template_edit_link_spec.rb +37 -0
  261. data/spec/system/decidim/chatbot/admin/admin_tests_notification_spec.rb +125 -0
  262. data/spec/system/decidim/chatbot/admin/admin_unchecks_enabled_on_a_menu_item_spec.rb +30 -0
  263. data/spec/system/decidim/chatbot/admin/admin_unsubscribes_whatsapp_user_from_chatbot_spec.rb +35 -0
  264. data/spec/system/decidim/chatbot/admin/admin_updates_welcome_message_from_general_tab_spec.rb +84 -0
  265. data/spec/system/decidim/chatbot/system/admin_updates_organization_chatbot_settings_spec.rb +33 -0
  266. data/spec/tasks/chatbot_create_spec.rb +99 -0
  267. data/spec/tasks/chatbot_sync_spec.rb +90 -0
  268. data/website/.node-version +1 -0
  269. data/website/.nvmrc +1 -0
  270. data/website/docs/developer/_category_.json +4 -0
  271. data/website/docs/developer/contribute.md +87 -0
  272. data/website/docs/index.md +42 -0
  273. data/website/docs/integrate/_category_.json +4 -0
  274. data/website/docs/integrate/index.md +214 -0
  275. data/website/docusaurus.config.ts +93 -0
  276. data/website/package.json +37 -0
  277. data/website/sidebars.ts +7 -0
  278. data/website/src/css/custom.css +37 -0
  279. data/website/static/api-docs.html +2218 -0
  280. data/website/static/img/logo.svg +1 -0
  281. data/website/static/openapi.json +37801 -0
  282. data/website/tsconfig.json +6 -0
  283. data/website/yarn.lock +9782 -0
  284. data/yarn.lock +1003 -0
  285. metadata +424 -0
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe "Admin updates welcome message from general tab", driver: :rack_test do
6
+ let(:organization) { create(:organization, available_locales: %w(en fr es)) }
7
+ let(:user) { create(:user, :admin, :confirmed, organization:) }
8
+ let!(:chatbot) do
9
+ create(:chatbot, organization:, name: { "en" => "Org bot" }, welcome_message: { "en" => "Hi" })
10
+ end
11
+
12
+ before do
13
+ enable_chatbot!(organization)
14
+ switch_to_host(organization.host)
15
+ login_as user, scope: :user
16
+ visit decidim_admin_chatbot.edit_chatbot_general_path(chatbot)
17
+ end
18
+
19
+ it "updates welcome_message, menu copy, and error messages" do
20
+ expect(page).to have_no_css(".item_show__header")
21
+ expect(page).to have_no_link("Back to list")
22
+ expect(page).to have_css(".process-title-content-breadcrumb-container-left", text: "Demo Bot")
23
+ expect(page).to have_css("span.help-text", text: "Set the welcome message participants see when they start a conversation.")
24
+ expect(page).to have_css("#accordion-basic_settings")
25
+ expect(page).to have_css("#accordion-menu")
26
+ expect(page).to have_css("#accordion-error_messages")
27
+ expect(page).to have_css("#panel-basic_settings")
28
+ expect(page).to have_css("#panel-menu")
29
+ expect(page).to have_css("#panel-error_messages")
30
+ expect(page).to have_css("h2.card-title#basic_settings", text: "Basic Settings")
31
+ expect(page).to have_css("h2.card-title#menu", text: "Menu")
32
+ expect(page).to have_css("h2.card-title#error_messages", text: "Error Messages")
33
+ expect(page).to have_css(".card-section")
34
+ expect(page).to have_css(".row.column")
35
+ expect(page).to have_no_css("fieldset legend")
36
+ expect(page).to have_no_css("#chatbot-whatsapp-preview-toggle")
37
+ expect(page).to have_no_css(".toggle__switch-trigger")
38
+ expect(page).to have_css(".chatbot-whatsapp-preview__canvas", text: "Hi")
39
+ expect(page).to have_css(".chatbot-whatsapp-preview__canvas", text: "What actions do you want to do?")
40
+ expect(page.body).to include("padding-inline: 1rem")
41
+ expect(page).to have_css("[data-announcement]",
42
+ text: "You can configure general settings of your chatbot here.")
43
+ expect(page).to have_css(".item__edit-sticky")
44
+ expect(page).to have_no_field("chatbot_name_en")
45
+
46
+ fill_in "chatbot_welcome_message_en", with: "Welcome friend"
47
+ fill_in "chatbot_menu_intro_en", with: "What actions do you want to do?"
48
+ fill_in "chatbot_menu_connector_en", with: "or"
49
+ fill_in "chatbot_error_404_en", with: "Not found"
50
+ fill_in "chatbot_error_502_en", with: "Service unavailable"
51
+ fill_in "chatbot_error_401_en", with: "Unauthorized"
52
+ within(".item__edit-sticky") { click_on "Save" }
53
+
54
+ expect(page).to have_admin_callout("Chatbot updated successfully")
55
+ expect(chatbot.reload.name["en"]).to eq("Org bot")
56
+ expect(chatbot.welcome_message["en"]).to eq("Welcome friend")
57
+ expect(chatbot.menu_intro["en"]).to eq("What actions do you want to do?")
58
+ expect(chatbot.menu_connector["en"]).to eq("or")
59
+ expect(chatbot.error_404["en"]).to eq("Not found")
60
+ expect(chatbot.error_502["en"]).to eq("Service unavailable")
61
+ expect(chatbot.error_401["en"]).to eq("Unauthorized")
62
+ end
63
+
64
+ it "shows the menu connector between groups of three items" do
65
+ %w(Alpha Bravo Charlie Delta).each_with_index do |title, index|
66
+ create(:chatbot_item, chatbot:, title: { "en" => title }, position: index)
67
+ end
68
+ visit decidim_admin_chatbot.edit_chatbot_general_path(chatbot)
69
+
70
+ expect(page).to have_css(".chatbot-whatsapp-preview__card-body", text: "What actions do you want to do?")
71
+ expect(page).to have_css(".chatbot-whatsapp-preview__card-body", text: "or")
72
+ expect(page).to have_no_css(".chatbot-whatsapp-preview__bubble", text: "or")
73
+ expect(page).to have_css(".chatbot-whatsapp-preview__button", text: "Alpha")
74
+ expect(page).to have_css(".chatbot-whatsapp-preview__button", text: "Bravo")
75
+ expect(page).to have_css(".chatbot-whatsapp-preview__button", text: "Charlie")
76
+ expect(page).to have_css(".chatbot-whatsapp-preview__button", text: "Delta")
77
+ expect(page).to have_css(".chatbot-whatsapp-preview__card", count: 2)
78
+ expect(page).to have_no_css(".chatbot-whatsapp-preview__bubble", text: "Alpha")
79
+ within(all(".chatbot-whatsapp-preview__card")[1]) do
80
+ expect(page).to have_css(".chatbot-whatsapp-preview__card-body", text: "or")
81
+ expect(page).to have_css(".chatbot-whatsapp-preview__button", text: "Delta")
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe "System admin updates organization chatbot settings", driver: :rack_test do
6
+ let(:admin) { create(:admin) }
7
+ let!(:organization) { create(:organization, available_locales: %w(en fr es)) }
8
+
9
+ before do
10
+ login_as admin, scope: :admin
11
+ visit decidim_system.edit_organization_path(organization)
12
+ end
13
+
14
+ it "persists chatbot credentials from the organization Chatbot tab" do
15
+ expect(page).to have_css("#decidim-toggle-settings-tabs")
16
+ expect(page).to have_css("#trigger-toggle-decidim_chatbot")
17
+ expect(page).to have_css("#settings_tab_form_decidim_chatbot")
18
+
19
+ within("#settings_tab_form_decidim_chatbot") do
20
+ check I18n.t("decidim_toggle.system.decidim_chatbot.enabled")
21
+ fill_in I18n.t("decidim_toggle.system.decidim_chatbot.access_token"), with: "tok-from-system"
22
+ fill_in I18n.t("decidim_toggle.system.decidim_chatbot.business_id"), with: "biz-from-system"
23
+ click_on I18n.t("decidim_toggle.system.organizations.form_tab.save")
24
+ end
25
+
26
+ expect(page).to have_content(I18n.t("decidim_toggle.system.organizations.form_tab.success"))
27
+ expect(Decidim::Chatbot.credentials_for(organization.reload)).to eq(
28
+ enabled: true,
29
+ access_token: "tok-from-system",
30
+ business_id: "biz-from-system"
31
+ )
32
+ end
33
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe "rake chatbot:create", type: :task do
6
+ subject(:run_task) { task.execute }
7
+
8
+ let(:organization) { create(:organization) }
9
+ let(:phone_id) { "phone-create-1" }
10
+ let(:client) { instance_double(Decidim::Chatbot::Whatsapp::GraphClient) }
11
+ let(:details) do
12
+ {
13
+ id: phone_id,
14
+ verified_name: "Rake Bot",
15
+ display_phone_number: "+1000",
16
+ platform_type: "CLOUD_API",
17
+ subscribed_apps: [{ "id" => "1" }],
18
+ health_status: { "can_send_message" => "AVAILABLE", "entities" => [] }
19
+ }
20
+ end
21
+
22
+ before do
23
+ task.reenable
24
+ allow(ENV).to receive(:fetch).and_call_original
25
+ allow(ENV).to receive(:fetch).with("PHONE_ID").and_return(phone_id)
26
+ allow(ENV).to receive(:fetch).with("ORG_HOST").and_return(organization.host)
27
+
28
+ Decidim::Toggle.save_config!(
29
+ organization,
30
+ Decidim::Chatbot::MODULE_NAME,
31
+ { "enabled" => true, "access_token" => "token", "business_id" => "biz" }
32
+ )
33
+
34
+ allow(Decidim::Chatbot::Whatsapp::GraphClient).to receive(:new).and_return(client)
35
+ allow(client).to receive(:fetch_phone).with(phone_id).and_return(details)
36
+ end
37
+
38
+ it "preloads the Rails environment" do
39
+ expect(task.prerequisites).to include("environment")
40
+ end
41
+
42
+ it "creates a chatbot for the organization" do
43
+ expect { run_task }.to change(Decidim::Chatbot::Chatbot, :count).by(1)
44
+ expect($stdout.string).to include("Created/synced chatbot")
45
+ end
46
+
47
+ context "when the organization host is unknown" do
48
+ before { allow(ENV).to receive(:fetch).with("ORG_HOST").and_return("missing.example") }
49
+
50
+ it "aborts" do
51
+ expect { run_task }.to raise_error(SystemExit) do |error|
52
+ expect(error.message).to include("Organization not found")
53
+ end
54
+ end
55
+ end
56
+
57
+ context "when chatbot is disabled" do
58
+ before do
59
+ Decidim::Toggle.save_config!(
60
+ organization,
61
+ Decidim::Chatbot::MODULE_NAME,
62
+ { "enabled" => false, "access_token" => "token", "business_id" => "biz" }
63
+ )
64
+ end
65
+
66
+ it "aborts with disabled" do
67
+ expect { run_task }.to raise_error(SystemExit) do |error|
68
+ expect(error.message).to include("disabled")
69
+ end
70
+ end
71
+ end
72
+
73
+ context "when credentials are missing" do
74
+ before do
75
+ Decidim::Toggle.save_config!(
76
+ organization,
77
+ Decidim::Chatbot::MODULE_NAME,
78
+ { "enabled" => true, "access_token" => "", "business_id" => "" }
79
+ )
80
+ end
81
+
82
+ it "aborts with missing_credentials" do
83
+ expect { run_task }.to raise_error(SystemExit) do |error|
84
+ expect(error.message).to include("missing_credentials")
85
+ end
86
+ end
87
+ end
88
+
89
+ context "when Graph raises" do
90
+ before { allow(client).to receive(:fetch_phone).and_raise(StandardError, "boom") }
91
+
92
+ it "aborts with graph_error and exception text" do
93
+ expect { run_task }.to raise_error(SystemExit) do |error|
94
+ expect(error.message).to include("graph_error")
95
+ expect(error.message).to include("StandardError: boom")
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe "rake chatbot:sync", type: :task do
6
+ subject(:run_task) { task.execute }
7
+
8
+ let(:organization) { create(:organization) }
9
+ let!(:chatbot) { create(:chatbot, organization:, whatsapp_phone_id: "sync-1") }
10
+ let(:client) { instance_double(Decidim::Chatbot::Whatsapp::GraphClient) }
11
+ let(:details) do
12
+ {
13
+ id: "sync-1",
14
+ verified_name: "Synced",
15
+ display_phone_number: "+2000",
16
+ platform_type: "CLOUD_API",
17
+ subscribed_apps: [],
18
+ health_status: { "can_send_message" => "BLOCKED", "entities" => [] }
19
+ }
20
+ end
21
+
22
+ before do
23
+ task.reenable
24
+ allow(Decidim::Chatbot::Whatsapp::GraphClient).to receive(:new).and_return(client)
25
+ allow(client).to receive(:fetch_phone).with("sync-1").and_return(details)
26
+ end
27
+
28
+ it "preloads the Rails environment" do
29
+ expect(task.prerequisites).to include("environment")
30
+ end
31
+
32
+ context "when the organization has Meta credentials" do
33
+ before do
34
+ Decidim::Toggle.save_config!(
35
+ organization,
36
+ Decidim::Chatbot::MODULE_NAME,
37
+ { "enabled" => false, "access_token" => "token", "business_id" => "biz" }
38
+ )
39
+ end
40
+
41
+ it "syncs each chatbot using that org's Graph client" do
42
+ expect { run_task }.not_to raise_error
43
+ expect(chatbot.reload.whatsapp_display_name).to eq("Synced")
44
+ expect(chatbot.whatsapp_phone_status).to eq("blocked")
45
+ expect($stdout.string).to include("Synced chatbot ##{chatbot.id}")
46
+ expect($stdout.string).to include("chatbot:sync done")
47
+ end
48
+ end
49
+
50
+ context "when the organization is missing credentials" do
51
+ it "skips the org and aborts with failures" do
52
+ expect { run_task }.to raise_error(SystemExit) do |error|
53
+ expect(error.message).to include("1 failure")
54
+ end
55
+ end
56
+ end
57
+
58
+ context "when SyncPhone fails for a chatbot" do
59
+ before do
60
+ Decidim::Toggle.save_config!(
61
+ organization,
62
+ Decidim::Chatbot::MODULE_NAME,
63
+ { "enabled" => true, "access_token" => "token", "business_id" => "biz" }
64
+ )
65
+ allow(client).to receive(:fetch_phone).and_return(nil)
66
+ end
67
+
68
+ it "counts the failure and aborts" do
69
+ expect { run_task }.to raise_error(SystemExit) do |error|
70
+ expect(error.message).to include("1 failure")
71
+ end
72
+ end
73
+ end
74
+
75
+ context "when Graph raises" do
76
+ before do
77
+ Decidim::Toggle.save_config!(
78
+ organization,
79
+ Decidim::Chatbot::MODULE_NAME,
80
+ { "enabled" => true, "access_token" => "token", "business_id" => "biz" }
81
+ )
82
+ allow(client).to receive(:fetch_phone).and_raise(StandardError, "boom")
83
+ end
84
+
85
+ it "warns with graph_error detail and aborts" do
86
+ expect { run_task }.to raise_error(SystemExit, /1 failure/)
87
+ .and output(/graph_error.*StandardError: boom/m).to_stderr
88
+ end
89
+ end
90
+ end
@@ -0,0 +1 @@
1
+ 22
data/website/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 22
@@ -0,0 +1,4 @@
1
+ {
2
+ "label": "Developer",
3
+ "position": 3
4
+ }
@@ -0,0 +1,87 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
5
+ # Contribute
6
+
7
+ Contributor guide for the **decidim-chatbot** gem (not host install — see [Install and configure](/integrate)).
8
+
9
+ ## Checks
10
+
11
+ ```bash
12
+ ./bin/check
13
+ ```
14
+
15
+ Runs:
16
+
17
+ 1. RuboCop
18
+ 2. erblint
19
+ 3. Prettier (`yarn format:check`)
20
+ 4. i18n-tasks missing keys
21
+ 5. OpenAPI freshness (`bin/gen-openapi --check`)
22
+ 6. RSpec
23
+
24
+ OpenAPI regen (Docker, RestFull mount + dummy app): `bin/gen-openapi` merges RestFull `openapi.json` with chatbot RSwag specs.
25
+
26
+ TypeScript client regen is **not** in `./bin/check`. Same as restfull: `yarn gen:node-client` (`bin/gen-node-client --validate --input openapi/openapi.json`). Compose image `octree/decidim-dev:0.32` has Node 22 and no JRE; OpenAPI Generator needs Java:
27
+
28
+ ```bash
29
+ docker run --rm -v "$PWD:/workspace" -w /workspace node:22-bookworm bash -lc 'corepack enable && apt-get update && apt-get install -y --no-install-recommends default-jre-headless && yarn && yarn gen:node-client'
30
+ ```
31
+
32
+ Output: `contrib/decidim-node-client` (`@octree/decidim-chatbot`).
33
+
34
+ Docs site: `yarn docs:build` (ReDoc HTML + Docusaurus; API UI at `/api/`).
35
+
36
+ ## Local Docker
37
+
38
+ Image: `octree/decidim-dev:0.32`.
39
+
40
+ ```bash
41
+ docker compose up -d
42
+ docker compose exec chatbot bash -lc 'bundle && yarn && bundle exec rake test_app && ./bin/check'
43
+ ```
44
+
45
+ `decidim-restfull-core`, `decidim-restfull-blogs`, and `decidim-restfull-proposals` pin Gemfile tag `v0.3.11`; `decidim-toggle` stays on git `main`. Compose mounts `../decidim-restfull` so `bin/gen-openapi` can merge the sibling `openapi.json`. `DECIDIM_RESTFULL_PATH` defaults to `/home/decidim-restfull` in the container.
46
+
47
+ After cloning (or after deleting `spec/decidim_dummy_app`), generate the dummy app once: `bundle exec rake test_app` (installs chatbot + Toggle migrations). CI does the same in `ruby::rspec` before RSpec.
48
+
49
+ Compose sets `DATABASE_URL` (default `postgresql://decidim:pleaseChangeMe@chatbot_db:5432/decidim`). On Ruby 3.4 / Rails 8 that override wins over `database.yml`. `rake test_app`, `./bin/check`, and CI unset it. If you run RSpec by hand: `unset DATABASE_URL`.
50
+
51
+ ## Local CI (GitLab parity)
52
+
53
+ `docker-compose.ci.yml` is the gem-contributor merge-request gate (`ruby:3.4.7` + `postgres:17` + `redis`, same as `.gitlab-ci.yml` `ruby::rspec`). It mounts `.:/app` and `../decidim-restfull:/home/decidim-restfull`. Defaults: `DATABASE_HOST=postgres`, `DATABASE_USERNAME=decidim`, `DATABASE_PASSWORD=TEST-insecure_password`, `DISABLED_DOCKER_COMPOSE=true`, `TRAEFIK_REDIS_URL=redis://redis:6379/1`, `NODE_MAJOR=22`, `SIMPLECOV=1`, `CI=1`, `DECIDIM_FORCE_SSL=false`, `DECIDIM_RESTFULL_PATH=/home/decidim-restfull`, `DISABLE_SPRING=1`. `bin/ci-setup` mirrors the GitLab `before_script`, then runs dummy `bin/shakapacker` (packs at `spec/decidim_dummy_app/public/packs/manifest.json`; the binary’s exit code is ignored).
54
+
55
+ ```bash
56
+ docker compose -f docker-compose.ci.yml run --rm ci
57
+ docker compose -f docker-compose.ci.yml run --rm rspec
58
+ ```
59
+
60
+ `ci` runs `bin/check` plus website `yarn build`. `rspec` runs specs only. Crowdin and gem publish stay GitLab-only (tokens).
61
+
62
+ ## RestFull Extension
63
+
64
+ Registration lives in `lib/decidim/chatbot/engine.rb` via `Decidim::RestFull::Extension.register(:chatbot)` (OAuth scope `whatsapp`, permission `whatsapp.read`).
65
+
66
+ Routes: `GET /chatbots`, `GET /chatbots/:id` (see `Decidim::RestFull::Routing.read_resources`).
67
+
68
+ Upstream: RestFull [add-endpoint recipe](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full).
69
+
70
+ ## Admin + System
71
+
72
+ | Engine | Path |
73
+ | --- | --- |
74
+ | Admin | `/admin/chatbot` — Settings menu item (`admin_engine.rb`) |
75
+ | Toggle tab | System organization **Chatbot** tab (`settings_tab.rb`, `MODULE_NAME = "decidim_chatbot"`) |
76
+
77
+ Meta Graph client: `Decidim::Chatbot::Whatsapp::GraphClient` (API version from `Decidim::Chatbot.whatsapp_api_version`). Status mapping: `StatusMapper` (uses Meta [health_status](https://developers.facebook.com/docs/whatsapp/cloud-api/health-status/)). Template send resolves Meta `name` + `language` from the stored template ID via Graph; Decidim locales only pick which template ID / variables to use. Named template variables are sent as Cloud API `parameter_name` via `Whatsapp::BodyParameters` (positional numeric keys stay unnamed).
78
+
79
+ ## Doc site map
80
+
81
+ | Path | Persona | Content |
82
+ | --- | --- | --- |
83
+ | `docs/index.md` | Integrator | Product overview |
84
+ | `docs/integrate/` | System admin / host integrator | Install, System Toggle, rake, admin, API |
85
+ | `docs/developer/` | Gem contributor | Checks, local CI compose, Docker, extension points, node client regen |
86
+
87
+ No one-off sidebar layout: Docusaurus `autogenerated` from `docs/`.
@@ -0,0 +1,42 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
5
+ # Decidim Chatbot
6
+
7
+ **decidim-chatbot** adds WhatsApp chatbot administration to Decidim and RestFull API endpoints to read chatbot configuration.
8
+
9
+ ## Who this is for
10
+
11
+ Decidim **system administrators** and host integrators who attach WhatsApp Cloud API phone numbers to an organization and expose them to org admins and API clients.
12
+
13
+ ## What it does
14
+
15
+ | Surface | Role |
16
+ | --- | --- |
17
+ | **System** (`/system`) | Toggle **Chatbot** tab: Meta access token + business ID (per organization) |
18
+ | **Admin** | Configuration → Settings → **Chatbot**: list assigned phones; edit translated name and welcome message; enable/disable menu and notification items; Notifications send Meta templates (phase on step activation, manual via Send now) |
19
+ | **Rake** | `chatbot:create` / `chatbot:sync` pull WhatsApp fields from Meta Graph |
20
+ | **API** | RestFull scope `chatbot`: `GET /chatbots`, `GET /chatbots/:id` |
21
+
22
+ Org admins cannot register new Meta phone numbers in the UI. A system admin creates chatbots with a rake task after Meta credentials are stored.
23
+
24
+ ## Requirements
25
+
26
+ - Decidim `>= 0.32`, `< 0.33`
27
+ - [`decidim-restfull-core`](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full) `~> 0.3.11`
28
+ - [`decidim-restfull-blogs`](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full) `~> 0.3.11`
29
+ - [`decidim-restfull-proposals`](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full) `~> 0.3.11`
30
+ - [`decidim-toggle`](https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle) `~> 0.1.3`
31
+ - A Meta app with [WhatsApp Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/) access
32
+
33
+ ## Next
34
+
35
+ 1. [Install and configure](./integrate/) — Gemfile, System Toggle, phone ID, admin, API
36
+ 2. [Contribute](./developer/contribute) — lint, specs, OpenAPI regen
37
+
38
+ ## See also
39
+
40
+ - Meta: [Cloud API get started](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/)
41
+ - Meta: [Business phone numbers](https://developers.facebook.com/docs/whatsapp/cloud-api/phone-numbers/)
42
+ - Meta: [Messaging health status](https://developers.facebook.com/docs/whatsapp/cloud-api/health-status/)
@@ -0,0 +1,4 @@
1
+ {
2
+ "label": "Integrate",
3
+ "position": 2
4
+ }
@@ -0,0 +1,214 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
5
+ # Install and configure
6
+
7
+ Step-by-step setup for a Decidim host: install the gem, store Meta credentials in **System**, attach a WhatsApp phone number ID, then let org admins edit local copy.
8
+
9
+ ## Overview
10
+
11
+ ```text
12
+ Meta (Cloud API) → System Toggle (token + business ID)
13
+ → rake chatbot:create (PHONE_ID + ORG_HOST)
14
+ → Admin Settings → Chatbot (name + welcome)
15
+ → RestFull GET /chatbots
16
+ ```
17
+
18
+ Phone registration and 2FA on Meta stay in Meta’s tools for now. This gem **assigns** an existing Cloud API phone to an organization and syncs display fields.
19
+
20
+ ## Infrastructure
21
+
22
+ | Piece | Purpose |
23
+ | --- | --- |
24
+ | Meta Developer App + WhatsApp product | Cloud API access, tokens, phone number ID |
25
+ | `decidim-toggle` | Persists per-organization module config (System UI) |
26
+ | `decidim-restfull-core` | OAuth API + OpenAPI merge |
27
+ | `decidim-restfull-blogs` | RestFull blogs API |
28
+ | `decidim-restfull-proposals` | RestFull proposals API |
29
+ | Postgres | `decidim_chatbot_chatbots` + Toggle config tables |
30
+
31
+ ### Meta documentation (keep open while configuring)
32
+
33
+ | Topic | Link |
34
+ | --- | --- |
35
+ | Cloud API quickstart | [Get started](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/) |
36
+ | Phone numbers & phone number ID | [Business phone numbers](https://developers.facebook.com/docs/whatsapp/cloud-api/phone-numbers/) |
37
+ | Register a number for Cloud API | [Registration](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/registration/) |
38
+ | Permanent system user token | [Business Management API get started](https://developers.facebook.com/docs/whatsapp/business-management-api/get-started/) (system user + token permissions) |
39
+ | Graph API overview | [Graph API](https://developers.facebook.com/docs/graph-api/overview) |
40
+ | Messaging health (`health_status`) | [Health status](https://developers.facebook.com/docs/whatsapp/cloud-api/health-status/) |
41
+ | WABA phone_numbers edge | [WABA phone_numbers](https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account/phone_numbers/) |
42
+
43
+ This gem calls Graph **v26.0** (`Decidim::Chatbot.whatsapp_api_version`). Prefer a long-lived **system user** token with WhatsApp business permissions, not the short-lived App Dashboard test token.
44
+
45
+ ## 1. Install gems
46
+
47
+ Host Gemfile:
48
+
49
+ ```ruby
50
+ gem "decidim-chatbot"
51
+ gem "decidim-restfull-core",
52
+ git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full",
53
+ branch: "main"
54
+ gem "decidim-restfull-blogs",
55
+ git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full",
56
+ branch: "main"
57
+ gem "decidim-restfull-proposals",
58
+ git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-module-rest_full",
59
+ branch: "main"
60
+ gem "decidim-toggle",
61
+ git: "https://git.octree.ch/decidim/vocacity/decidim-modules/decidim-toggle",
62
+ branch: "main"
63
+ ```
64
+
65
+ ```bash
66
+ bundle install
67
+ bundle exec rails decidim_chatbot:install:migrations
68
+ bundle exec rails decidim_toggle:install:migrations
69
+ bundle exec rails db:migrate
70
+ ```
71
+
72
+ Restart the host app so engines boot.
73
+
74
+ ## 2. Configure Meta credentials in System
75
+
76
+ 1. Sign in to Decidim **System** (`/system`).
77
+ 2. Open the organization → settings tabs provided by Toggle.
78
+ 3. Open the **Chatbot** tab.
79
+ 4. Fill:
80
+
81
+ | Field | What to paste | Where Meta shows it |
82
+ | --- | --- | --- |
83
+ | Enable chatbot | Must be on for admin menu, RestFull endpoints, and `chatbot:create` | — |
84
+ | WhatsApp access token | Bearer token for Graph | [Get started](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/) / [system user token](https://developers.facebook.com/docs/whatsapp/business-management-api/get-started/) |
85
+ | WhatsApp business ID | Meta **business portfolio** ID used with `owned_whatsapp_business_accounts` | Meta Business Suite / app WhatsApp setup (same business that owns the WABA) |
86
+
87
+ Credentials are stored **in plain text** in Toggle’s JSON config (no encryption yet). Restrict System access accordingly.
88
+
89
+ When **Enable chatbot** is off: the admin Settings → Chatbot menu item is hidden, admin chatbot URLs redirect away, and RestFull `GET /chatbots` endpoints return **HTTP 501 Not Implemented**.
90
+
91
+ Save the tab before running rake tasks.
92
+
93
+ ## 3. Find the WhatsApp phone number ID
94
+
95
+ The rake task needs Meta’s **phone number ID** (Graph node id), not the human-readable `+33…` display number.
96
+
97
+ From Meta’s [get started](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/) / App Dashboard → WhatsApp → **API Setup**, copy the **Phone number ID** for the From number. Background: [Business phone numbers](https://developers.facebook.com/docs/whatsapp/cloud-api/phone-numbers/).
98
+
99
+ You can also list numbers on a WABA via Graph ([phone_numbers edge](https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account/phone_numbers/)).
100
+
101
+ ## 4. Create the chatbot (sysadmin)
102
+
103
+ On the host:
104
+
105
+ ```bash
106
+ bundle exec rails chatbot:create PHONE_ID=<phone_number_id> ORG_HOST=<organization.host>
107
+ ```
108
+
109
+ Example:
110
+
111
+ ```bash
112
+ bundle exec rails chatbot:create PHONE_ID=123456789012345 ORG_HOST=nca.voca.city
113
+ ```
114
+
115
+ | ENV | Required | Meaning |
116
+ | --- | --- | --- |
117
+ | `PHONE_ID` | yes | Meta WhatsApp **phone number ID** |
118
+ | `ORG_HOST` | yes | Decidim organization `host` |
119
+
120
+ The command loads Toggle credentials for that organization, calls Graph, and creates (or re-syncs) a `Decidim::Chatbot::Chatbot` with:
121
+
122
+ - `whatsapp_phone_id`, `whatsapp_display_name`, `whatsapp_phone_number`, `whatsapp_phone_status`
123
+ - default translated `name` from Meta’s verified name when empty
124
+
125
+ A phone ID is **globally unique**: it cannot belong to two organizations.
126
+
127
+ ### Sync later
128
+
129
+ Refresh WhatsApp fields for **all** chatbots, **organization by organization**:
130
+
131
+ ```bash
132
+ bundle exec rails chatbot:sync
133
+ ```
134
+
135
+ For each org that has chatbots: load that org’s Toggle token + business ID once, then sync every chatbot of that org with the same Graph client. Orgs missing credentials are skipped (counted as failures). `enabled` is not required for sync.
136
+
137
+ Status values (`created`, `subscribed`, `waiting_2fa`, `waiting_confirmation`, `blocked`, `ready`) are derived mainly from Graph [`health_status`](https://developers.facebook.com/docs/whatsapp/cloud-api/health-status/), platform type, and subscribed apps. Mapping lives in `Decidim::Chatbot::Whatsapp::StatusMapper`.
138
+
139
+ ## 5. Organization admin: Settings → Chatbot
140
+
141
+ As an organization admin:
142
+
143
+ 1. Open **Configuration → Settings → Chatbot**.
144
+ 2. See phones assigned to this organization (no “add phone” in UI).
145
+ 3. **Edit** a row to set translated **name** and **welcome message** (markdown in the textarea).
146
+ 4. WhatsApp phone id, display name, number, and status stay **read-only** (sourced from Meta via rake).
147
+
148
+ Menu and notification items have an **Enabled** checkbox (on by default). Uncheck it to keep the row in the admin list at 60% opacity without using it. Disabled menu items are omitted from the WhatsApp preview. Disabled notifications are not sent.
149
+
150
+ ## 5b. WhatsApp notification templates
151
+
152
+ Create message templates in Meta ([message templates](https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account/message-templates/)). This gem does not create templates on Meta.
153
+
154
+ Then in **Settings → Chatbot → Notifications**:
155
+
156
+ - **Phase**: pick a participatory process of this organization. The gem sends the template when that process’s step is activated (`decidim.events.participatory_process.step_activated`).
157
+ - **Manual**: click **Send now** on the edit screen when the template snapshot is ready. Confirm lists the current subscribed WhatsApp user count (same list as **Users**).
158
+
159
+ On create, store the Meta template ID per organization locale. At send time the gem loads that ID from Graph (`name`, `language`, `status`) and sends with Meta’s language code. Decidim locales only select which stored template ID and variables to use. Variable keys are sent as WhatsApp named parameters (`parameter_name`); they must match the `{{placeholders}}` on the Meta template.
160
+
161
+ Variable JSON stays keyed by organization locales. Values may use `%{organization_name}`, `%{chatbot_name}`, `%{user_name}`, `%{nickname}`.
162
+
163
+ Phase notifications do not use that JSON field. Admins check which named placeholders to bind: `{{space_name}}` (participatory process title), `{{phase_name}}` (step title), `{{phase_start}}` and `{{phase_end}}` (step dates as `dd/mm/yyyy`). Send runs in ActiveJob (`SendNotificationJob`) to subscribed WhatsApp users of that chatbot only.
164
+
165
+ Register / verify / subscribe flows on Meta are **not** in this gem yet; use Meta’s [registration](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/registration/) docs when a number is not Cloud-API ready.
166
+
167
+ ## 6. RestFull API (optional)
168
+
169
+ 1. Enable RestFull on the organization.
170
+ 2. Create an API client with OAuth scope **`whatsapp`** and permission **`whatsapp.read`**.
171
+ 3. Call (version segment matches your RestFull gem, e.g. `v0.3`):
172
+
173
+ ```http
174
+ GET /api/rest_full/v0.3/chatbots
175
+ Authorization: Bearer <token>
176
+ ```
177
+
178
+ ```http
179
+ GET /api/rest_full/v0.3/chatbots/{id}
180
+ Authorization: Bearer <token>
181
+ ```
182
+
183
+ ```http
184
+ GET /api/rest_full/v0.3/chatbot_menu_items
185
+ GET /api/rest_full/v0.3/chatbot_users
186
+ Authorization: Bearer <token>
187
+ ```
188
+
189
+ Responses are org-scoped JSON:API resources. Indexes are paginated (`page`, `per_page`) with body `meta` (`page`, `per_page`, `has_more`, `next`, `prev`) and support conditional GET. There are no write endpoints.
190
+
191
+ OpenAPI: [API reference](/api/). Regen in the gem: `bin/gen-openapi`.
192
+
193
+ ## FAQ
194
+
195
+ **Create fails with `disabled`**
196
+ System → Chatbot → turn **Enable chatbot** on, then save. This is separate from tokens.
197
+
198
+ **Create fails with `missing_credentials`**
199
+ System → Chatbot for that org; set access token and business ID; save. Chatbot must also be enabled.
200
+
201
+ **Create fails with `phone_not_found`**
202
+ Confirm `PHONE_ID` and that the business ID owns the WABA that lists that phone ([phone numbers](https://developers.facebook.com/docs/whatsapp/cloud-api/phone-numbers/)).
203
+
204
+ **Create fails with `phone_belongs_to_other_organization`**
205
+ That phone ID is already bound to another Decidim organization.
206
+
207
+ **Status stuck on `created` / not `ready`**
208
+ Check Meta [health status](https://developers.facebook.com/docs/whatsapp/cloud-api/health-status/) and whether the app is subscribed on the WABA. Run `chatbot:sync` after fixing Meta-side issues.
209
+
210
+ ## See also
211
+
212
+ - [Overview](/)
213
+ - [Contribute](/developer/contribute)
214
+ - Meta [Cloud API get started](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started/)