Integrating Drupal 8 Webforms Submissions and Rocket Chat

Use Case

Wanted form submissions to be posted to a Rocket Chat channel. Drupal 8 Webform has Remote Post Handlers (Email & Remote Post Handlers section).

First learned Rocket Chat API, specially the authentication part, and practiced with PostMan. Once got it working, started the integration.

Got stuck on a point where Rocket Chat needed custom headers  (Example Call section) to be sent but Webforms module would require the development of a custom module to address it.

Gladly the main developer of Drupal 8 Webforms, Jacob Rockowitz, was kind enough to add custom header capabilities to the module (this is the issue). The patch has just been committed and the feature is expected to be available in the next release of Webforms. I had to get the git version to get the patch.

It easily worked!

Then it was just a matter of:


  1.  figuring out how to submit a YAML array (Webforms) to a JSON array (RocketChat) for the attachments field of chat.postMessage API call
  2. get webform submission data (field values) into in RC

For #1, I've found that the attachments array has to be written in the same line. For example:

attachments: [{ 
    'color': '#00ff00', 
    'title': "ATUALIZADA", 
    "text": "Número: [webform_submission:sid]\nTítulo: [webform_submission:values:titulo]\nSolicitante: [webform_submission:values:solicitante]\nInício: [webform_submission:values:inicio]" , 
    'title_link': "http://xxxxx.xxxxxxxx.com.br/node/3/webform/submission/[webform_submission:sid]" 
    }]

must be written as a one liner. Notice that JSON array tags ([{ and ]}) are already in place:

attachments: [{ 'color': '#00ff00', 'title': "ATUALIZADA", "text": "Número: [webform_submission:sid]\nTítulo: [webform_submission:values:titulo]\nSolicitante: [webform_submission:values:solicitante]\nInício: [webform_submission:values:inicio]" , 'title_link': "http://xxxxx.xxxxxx.com.br/node/3/webform/submission/[webform_submission:sid]" }]


Only this way RC would recognize attachments as an array.

The snippet above also illustrate how to resolve #2: the field delimitator must be double quotes (") and shall use the form [webform_submission:values:titulo] for field values and [webform_submission:sid] for metadata (notice there is no :value in the latter).

The relevant configuration and the result can be seen in the pictures below.








Comments

Popular posts from this blog

Instalação eToken Pro no Ubuntu 18.04 para acesso ao eCAC da RFB

Ubuntu 17.10 - CIFS Mount Error Code -5