Microsoft Teams Webhook

All,
I used to love Slack until Salesforce bought them and drastically raised prices. I noticed the documentation for Farmbot developer documents shows a webhook for a channel. I thought I would share an example of using Microsoft Teams webhook to let me know the plants got 1 second of water on my Farmbot named the “Hoyt Clagwell” as the Tractor from Green Acres. I had to remove comments this was based on the (water it all sequence) to get the code to be under the 3000 character limit but for you Microsoft Teams users here is the tutorial to set up webhooks (teams) replace the url for your Msft Teams webhook below and here is my code :
local watering_time = 1
start_time = os.time() * 1000

local points = api({method = “GET”, url = “/api/points”})
local plants = {}

for k, v in pairs(points) do
if v.pointer_type == “Plant” then
table.insert(plants, {name = v.name, x = v.x, y = v.y})
end
end

table.sort(plants, function(l, r)
if math.abs(l.x - r.x) < 150 then
return l.y < r.y
else
return l.x < r.x
end
end)

local count = 0
local total = #plants
local job = “Watering all " … total … " plants”

send_message(
“info”,
“Preparing to water " … total … " plants for " … watering_time … " second(s) each.”,
“toast”
)

for k, v in pairs(plants) do
coordinates = “(” … v.x … ", " … v.y … “)”
set_job_progress(job, {
percent = 100 * (count) / total,
status = "Moving to " … (v.name or “plant”) … " at " … coordinates,
time = start_time
})
move_absolute(v.x, v.y, 0)
set_job_progress(job, {
percent = 100 * (count + 0.5) / total,
status = “Watering " … (v.name or “plant”) … " for " … watering_time … " second(s)”,
time = start_time
})
write_pin(8, “digital”, 1)
wait(watering_time * 1000)
write_pin(8, “digital”, 0)
count = count + 1
end

set_job_progress(job, {
percent = 100,
status = “Complete”,
time = start_time
})

local sections = {
{
[“activityTitle”] = “Farmbot Job Report”,
[“activitySubtitle”] = “Watering Status Update”,
[“activityImage”] = “https://forum.farmbot.org/uploads/default/original/2X/3/3644d5941650ef4a0e9b91eb44d46c7d05faea05.png”,
[“facts”] = {
{
[“name”] = “Plants”,
[“value”] = total
},
{
[“name”] = “Watering Time”,
[“value”] = tostring(watering_time) … " second(s)"
},
},
[“markdown”] = true
}
}

– Create the message payload for Msft Teams
local payload = json.encode({
[“@type”] = “MessageCard”,
[“@context”] = “http://schema.org/extensions”,
[“summary”] = “Farmbot Status Update”,
[“themeColor”] = “8EA604”,
[“sections”] = sections
})

local url = “https://mywork.webhook.office.com/webhookb2/d837c5cb-baf7-4dbb-bed6-29752993d7b0@f919b1ef-c0c3-4735-8fca-0928ec39d8d5/IncomingWebhook/3b75a211c0b9

local 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

:

5 Likes

Thanks for sharing!

Have you tried publishing a sequence before? This allows you to wrap up your Lua code with a title, description, variables, and any other sequence steps into one handy link that others can use to import into their account. You could make the webhook url a text variable and the watering_time a number variable.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.