How to trigger a Slack webhook with uniform?

I’m using the uniform plugin to provide a contact form. Now I want to trigger a message in a Slack channel via a webhook when someone submits the form. Therefore I took the webhook example from the docs. But I’m struggling with getting the request right so that Slack shows the message.

This is what Slack requires:

You have two options for sending data to the Webhook URL above:

  • Send a JSON string as the payload parameter in a POST request
  • Send a JSON string as the body of a POST request

For a simple message, your JSON payload could contain a text property at minimum. This is the text that will be posted to the channel.

A simple example:
payload={“text”: “This is a line of text in a channel.\nAnd this is another line of text.”}

How can I sent a request like that with the uniform plugin?

In the template:

<form action="<?php echo $page->url() ?>" method="POST">
   <textarea name="text"><?php echo $form->old('text') ?></textarea>
   <?php echo honeypot_field() ?>
   <?php echo csrf_field() ?>
   <input type="submit" value="Submit">
</form>

In the controller:

<?php

use Uniform\Form;

return function ($site, $pages, $page)
{
   $form = new Form([
      'text' => [
         'rules' => ['required'],
         'message' => 'Please enter something',
      ],
   ]);

   if (r::is('POST')) {
      $form->webhookAction([
         'url' => 'your_url',
         'json' => true,
         'params' => [
            'method' => 'POST',
         ],
      ]);
   }

   return compact('form');
};

This should post the submitted text in the Slack channel.

But you probably want to compose the posted message of the content of multiple different form fields. I realized that this can be easily achieved with just a little tweak to the WebhookAction class. I’ve just pushed the tweak to the current master of Uniform. Once you updated your Uniform installation to the current master you can implement a custom action like this:

<?php

namespace Uniform\Actions;

class SlackAction extends WebhookAction
{
   protected function transfromData(array $data)
   {
      return [
         'text' => 'Some message from '.$data['email'].':\n'.$data['message'],
      ];
   }
}

Template:

<form action="<?php echo $page->url() ?>" method="POST">
   <input name="email" type="email" value="<?php echo $form->old('email') ?>">
   <textarea name="message"><?php echo $form->old('message') ?></textarea>
   <?php echo csrf_field() ?>
   <?php echo honeypot_field() ?>
   <input type="submit" value="Submit">
</form>

Controller:

<?php

use Uniform\Form;

return function ($site, $pages, $page)
{
   $form = new Form([
      'email' => [
         'rules' => ['required', 'email'],
         'message' => 'Email is required',
      ],
      'message' => [
         'rules' => ['required'],
         'message' => 'Message is required',
      ],
   ]);

   if (r::is('POST')) {
      $form->slackAction([
         'url' => 'your_url',
         'json' => true,
         'params' => [
            'method' => 'POST',
         ],
      ]);
   }

   return compact('form');
};