It is possible for FarmBot to send outbound HTTP requests using Lua. This feature can be used to create third-party integrations with proprietary software products that offer webhook integrations, such as Slack.

In this tutorial, we will demonstrate how it is possible to send messages from FarmBot to Slack using this abbreviated process:

  1. FarmBot performs an HTTP POST to an incoming webhook URL on Slack’s servers.
  2. Slack transforms the HTTP request to a message that is seen by Slack users in a particular chat room.

Step 1: Generate a webhook URL

Before you begin, you will need to generate an incoming webhook URL. Since these instructions may change over time, we recommend referencing the official Slack webhook documentation for guidance. By the end of setup, you will have a URL that can be used to generate messages on Slack by way of an HTTP POST.

Do not share the webhook URL

Anyone with access to the URL will be able to send messages to your Slack channel.

Step 2: Create a sequence

Once you have a Slack webhook URL, navigate to the sequence editor and create a new sequence. Then add a Lua command to the sequence.

Step 3: Add Lua code

Paste the following code into the Lua command, making sure to replace the example url with your webhook URL generated in step 1.

payload = json.encode({
  text = "FarmBot says hi! :wave:"
})

url = "https://hooks.slack.com/services/CHANGE/THIS/URL"

result, err = http({
  url = url,
  method = 'POST',
  headers = {},
  body = payload
})

if err then
    toast("Error: " .. inspect(err), "error")
else
    debug("Request sent: " .. inspect(result))
end

Step 4: Run the sequence

SAVE the sequence and wait for it to sync with the FarmBot. You can then test it with the RUN button to make sure it functions as expected.

Once the sequence is verified to be working, you can run it in a variety of ways:

  • By using the RUN button in the sequence editor.
  • By binding the sequence to a physical button on the device via a pin binding.
  • From within a parent sequence, via an EXECUTE command.
  • On a recurring schedule, via an event.
  • Via third party software, using FarmBot JS.