Chatbot for integrating Decidim participation in popular chat applications (ie: Whatsapp).
- Communication is user-initiated: Users must start conversations with your business WhatsApp number
- 24-hour messaging window: After a user initiates contact, you have 24 hours to send messages before needing user re-engagement
- Delivery status tracking: Webhook notifications include message delivery status updates from Meta
- Configurable conversation timeout: Automatically reset conversations after a specified idle period
Add this line to your application's Gemfile:
gem "decidim-chatbot", github: "openpoke/decidim-module-chatbot"And then execute:
bundle install
bin/rails decidim:upgradeEXPERTS ONLY
When you run
bin/rails decidim:upgrade, Decidim's upgrade process is extended by this gem so thatdecidim_chatbotis included in the set of plugins handled bydecidim:choose_target_plugins. Once selected there, the standard Decidim upgrade pipeline will apply this plugin's migrations.Running
bin/rails decidim:upgradeis usually all you need. However, you can also run the migrations for this gem explicitly with:bin/rails decidim_chatbot:install:migrations
Workflows provide a way to define what logic will users encounter when interacting with the chatbot. Some workflows can be registered as "start workflows" using the standard Manifest mechanism in Decidim.
The admin must enable one of these "starting workflow" in order to allow communication between users and the chatbot. Workflows can also be configured with certain settings (if applicable).
Other workflows can be started from the initial one, these workflows do not need to be registered in any manifest.
See the Engine for the initialization of the built-in start workflows:
Decidim::Chatbot.start_workflows_registry.register(:organization_welcome) do |manifest|
manifest.workflow_class = "Decidim::Chatbot::Workflows::OrganizationWelcomeWorkflow"
end- Path (mounted): POST /chatbot/webhooks/:provider, GET /chatbot/webhooks/:provider
- Currently supported provider:
whatsapp. - WhatsApp verification (GET): set
WHATSAPP_VERIFY_TOKENin environment. Meta will call the endpoint withhub.mode,hub.verify_token, andhub.challenge. When the token matches, the endpoint echoes thehub.challengewith 200. - Delivery (POST): the endpoint acknowledges with 200 for supported providers. Signature verification and payload processing can be added later per provider.
Example verify request:
curl -G \
--data-urlencode "hub.mode=subscribe" \
--data-urlencode "hub.verify_token=$WHATSAPP_VERIFY_TOKEN" \
--data-urlencode "hub.challenge=abc123" \
http://localhost:3000/chatbot/webhooks/whatsappExample delivery request:
curl -X POST http://localhost:3000/chatbot/webhooks/whatsapp \
-H 'Content-Type: application/json' \
-d '{"entry":[]}'In order to develop locally, it is convenient to use a service such as ngrok to expose your local server to the internet. This allows Meta's webhook to reach your development environment. If you use ngrok, just start the proxy with:
ngrok http 300This will give you a domain name, change the domain of your "localhost" organization:
``bash bin/rails c Decidim::Organization.first.update(host: "the-domain-from-ngrok")
Note: Currently only WhatsApp is supported (PRs welcomed!)
The Decidim Chatbot module supports integration with the WhatsApp Business API. Follow these steps to set up your WhatsApp developer environment.
- Go to Meta for Developers portal.
- Create a Facebook Business Manager account if you don’t already have one.
- Set up a WhatsApp Business Account and register a phone number.
- In your WhatsApp Business Account, create a WhatsApp API app.
- Obtain the following credentials:
- Phone Number ID
- WhatsApp API Token
- Webhook Verify Secret (used to validate the webhook endpoint, this is a user-defined value, needs to be configured in Decidim and Whatsapp developers settings)
- In the WhatsApp API app settings, configure a Webhook URL pointing to your Decidim instance: https://your-decidim-domain.com/chatbot/webhooks/whatsapp
- Enable the following webhook events:
messagesmessage_reactionsmessage_deliveries(optional)
- Verify your webhook using the secret set in your Decidim Chatbot settings.
Add the following variables to your .env or server environment:
# WhatsApp Provider Configuration
WHATSAPP_PROVIDER_NAME=whatsapp
WHATSAPP_VERIFY_TOKEN=anything-you-want-here
WHATSAPP_ACCESS_TOKEN=your_whatsapp_api_token_hereContributions are welcome !
Bug reports and pull requests are welcome on GitHub at https://github.com/openpoke/decidim-module-chatbot.
We expect the contributions to follow the Decidim's contribution guide.
To start contributing to this project, first:
- Install the basic dependencies (such as Ruby and PostgreSQL)
- Clone this repository
Decidim's main repository also provides a Docker configuration file if you prefer to use Docker instead of installing the dependencies locally on your machine.
You can create the development app by running the following commands after cloning this project:
$ bundle
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rake development_appNote that the database user has to have rights to create and drop a database in order to create the dummy test app database.
Then to test how the module works in Decidim, start the development server:
$ cd development_app
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rails sIn case you are using rbenv and have the
rbenv-vars plugin installed for it, you
can add the environment variables to the root directory of the project in a file
named .rbenv-vars. If these are defined for the environment, you can omit
defining these in the commands shown above.
Please follow the code styling defined by the different linters that ensure we are all talking with the same language collaborating on the same project. This project is set to follow the same rules that Decidim itself follows.
Rubocop linter is used for the Ruby language.
You can run the code styling checks by running the following commands from the console:
$ bundle exec rubocop
To ease up following the style guide, you should install the plugin to your favorite editor, such as:
- Sublime Text - Sublime RuboCop
- Visual Studio Code - Rubocop for Visual Studio Code
To run the tests run the following in the gem development path:
$ bundle
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rake test_app
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rspecNote that the database user has to have rights to create and drop a database in order to create the dummy test app database.
In case you are using rbenv and have the
rbenv-vars plugin installed for it, you
can add these environment variables to the root directory of the project in a
file named .rbenv-vars. In this case, you can omit defining these in the
commands shown above.
If you want to generate the code coverage report for the tests, you can use
the SIMPLECOV=1 environment variable in the rspec command as follows:
$ SIMPLECOV=1 bundle exec rspecThis will generate a folder named coverage in the project root which contains
the code coverage report.
If you would like to see this module in your own language, you can help with its translation at Crowdin:
https://crowdin.com/project/decidim-module-chatbot
Security is very important to us. If you have any issue regarding security, please disclose the information responsibly by sending an email to ivan [at] pokecode [dot] net and not by creating a GitHub issue.
This engine is distributed under the GNU AFFERO GENERAL PUBLIC LICENSE.