Playing with the new ‘Lua sandbox’ block for Sequences this code was inspired by a use case mentioned by @mdingena in this recent topic.
This Lua block uses the env()
function to retrieve 2 user-defined Key-Value pairs
- ‘recovery_sequence_id’ is the Id of an existing Sequence that the new Farm Event will execute
- ‘recovery_start_time’ is the (ISO 8601 formatted) start time for the new Farm Event
Setup your env K-V pairs by using the Settings helper env as mentioned in recent release notes, or, add a Lua block to the primary Sequence which just creates those parameters by using Lua env( 'key-name', 'value')
.
--
-- Lua block to create a Farm Event to run a specified Sequence
--
-- Fetch our current Web App API token value
tokens = 'https://my.farm.bot/api/tokens'
headers = {['Content-Type']='application/json'}
creds = {
user = {
email='your-email-configured-in-the-web-app',
password='your-password-for-your-my.farm.bot-account'
}
}
request = {
url=tokens,
headers=headers,
method='POST',
body=json.encode(creds)
}
res, err = http(request)
if err then
send_message('warn', 'error '..inspect(err), "toast")
else
token = json.decode(res.body)['token']['encoded']
-- Create a Farm Event to run required Sequence
-- Assume Env knows Sequence Id and Time offset
farm_event = {
executable_id = env('recovery_sequence_id'),
executable_type = 'Sequence',
['repeat'] = '1',
start_time = env('recovery_start_time'),
time_unit = 'never'
}
farm_events = 'https://my.farm.bot/api/farm_events'
headers = {
['Content-Type']='application/json',
['Authorization']='Bearer '..token
}
request = {
url=farm_events,
headers=headers,
method='POST',
body=json.encode(farm_event)
}
res, err = http(request)
if err then
send_message('warn', 'error '..inspect(err), "toast")
else
send_message(
'success',
"Farm Event created :: " .. inspect(json.decode(res.body)),
'email'
)
end
end