Example 1: Send Slack chat messages

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

In the tutorial below, 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"

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

if err then
    send_message("error", "ERROR: " .. inspect(err), {"toast"})
else
    send_message("debug", "REQUEST SENT: " .. inspect(res))
end

Now 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.

Step 4: Run the sequence

Once the sequence is coded and saved, 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.