SAFERS Blog

Follow SAFERS blogs on Forest Fire Management

SAFERS Chatbot for citizens and volunteers: Goals, implementation and deployment

SAFERS Chatbot for citizens and volunteers: Goals, implementation and deployment

This blog article covers the key details of the architecture, the implementation and the deployment of the Chatbot application that is integrated with SAFERS Web Dashboard. The Chatbot main goal is to increase the level of situational awareness and support the in-field operations of professional users as well as citizens in all the emergency phases, before, during and after it. In fact, it is able to deliver as well as to retrieve multimedia geolocated contents from registered users’ smartphones. Everyone has the possibility to access the Chatbot functionalities after having completed the registration procedure. To create a valid account, citizens must provide username and password. No other profile information is collected by the application.

Chatbot Goals and Link with the Project Objectives

The tool can deliver as well as retrieve multimedia geolocated contents from people’s smartphones and use the Crowdsourcing Intelligence to establish a conversational dialogue in order to request data, assess the safety and the capabilities of citizens during emergencies to identify potential citizens responders, to verify or           complement reports spontaneously sent by citizens. Considering official early warnings, the Chatbot will be used to proactively push risk awareness contents to the potentially affected area. During emergencies, the Chatbot will be used for tasking and monitoring missions, and to coordinate efforts between responders, volunteers, and citizens. The activities of this task include:

  • State-of-art review about chatbot platform and related technologies
  • Selection of the most appropriate platform allowing pull and push of multimedia contents
  • Design, implementation, and test of the Chatbot
  • Design, implementation, and test of a back-office web-based application for authorities to allow sending contents, requests, and manage missions.

 


Chatbot logic architecture

The technology stack of the Chatbot logic, the main engine of the chatbot, is based on Nest.js. Nest.js is a modern yet lightweight TypeScript MVC Application Framework focused on modularity, and Telegraf.js, a popular, plugin-based, JavaScript library implementing a high-level interface for Telegram API. The Chatbot Logic makes use of a non-relational database for data persistence, needed to preserve the conversation state of user sessions when consuming the Telegram API.

The chatbot module is divided into a set of components that make it stateful, reliable and fully integrated with the Telegram Bot APIs capabilities. These main components are:

  • Bot logic – provides inline menus and custom keyboards for all the functionalities available for users.
  • Session storage – stores users’ sessions in a MongoDB managed data structure.
  • Web server (Chatbot client API) – handles all HTTP requests to/from the chatbot as well as providing architectural structure and inversion of control container to the whole codebase.

The server application has an MVC structure, with modules composed by services, controllers and views. The Chatbot Logic has two main modules, one for Authentication with SAFERS through the CIAM, and the other, Bot, which implements the main business logic. The Authentication module exposes a thin REST API layer with endpoints for OAuth 2.0 user authentication. The Chatbot logic also serves the static assets and dynamic HTML views, rendered with Pug and CSS, which are needed by the OAuth 2.0 authorization code flow. However, the main features of the Chatbot Logic are implemented in the Bot module, which is mainly composed by a service that handles all the interaction with Telegram API and with the CIG, using their respective REST frameworks. The Bot module service makes heavy usage of Telegraf.js high-level API for Telegram services. Messages sent by the bot to the user are formatted using Telegram-flavored HTML and Markdown and are rendered thanks to Handlebars templates. The Bot module controller exposes one endpoint for sending direct notifications to currently authenticated users.

Such endpoint is a webhook that is called by the CIG module, using a business-to-business authentication mechanism. On the Telegram interface, the Chatbot Logic receives as input the user messages, which include text, location information, multimedia such as picture, documents, video and audio files. The Output of the Chatbot Logic includes text messages, URLs, multimedia, inline keyboards (menus) and custom keyboards. On the CIG Module interface, inputs are user Profile, Status, Activity, Missions, Reports, Communications and Notifications, each one displayed using Telegram cards and menus. The Chatbot Logic uses the CIG Module interface for updating Status, Activity and Mission, and sends new user-generated Reports. The localization features of the Chatbot logic have been implemented using the I18Next framework. This feature allows to translate the UI content in the user language, and to easily extend the number of supported languages over time.

Chatbot deployment           

The CIG Module has been deployed on a Virtual Machine (VM) hosted on Azure Infrastructure. The Virtual Machine has a public IP address with an association to several domain names, one for each crowdsourcing component. An SSL Certificate has been associated to each of the registered domain names. All SSL Certificates are handled by Nginx. The nginx server is used to serve all the components, that is the CIAM, the CIG and the Chatbot logic. To make the deployment process more efficient and host-independent, while also easy to scale in future developments, all components and their dependencies are installed and managed as Docker containers, and Nginx is used as a reverse proxy towards their local instances. This pattern allows for easy horizontal scaling if needed. Nginx also handles the SSL certificates. Docker is a very popular containerization solution which has affirmed itself as market leader and de-facto industry standard, and it is well integrated in most popular IDEs and technologies. With Docker, it is possible to specify the runtime environment requirements in a way that is completely decoupled from the host. Using the integrated Docker Compose tool, each application containers and those of their dependencies can be even more easily configured and instantiated in a declarative way using YML files.

This setup allows to deploy separate copies of the CIG Module: a development environment, a staging environment for tests and finally a production environment where to deploy the new consolidated software versions during the product release cycle. The only exception to this schema is the CIAM, that has a single deployed instance due to its multi-tenant application management. Same deployment schema is followed for the Chatbot. At the time of this writing, the production environment has not been published yet.

To learn more about SAFERS Chatbot for citizens and volunteers: Link

Cookie Policy

This website uses cookies that are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By accepting this OR scrolling this page OR continuing to browse, you agree to our privacy policy.