Colin Fay
Posted on January 8, 2020
Building an API in NodeJS and R to send message to Slack from yourGarmin watch.
Why on earth
ThinkR is a remote company, meaning that we all work from our home. Ontop of other cool things about remote work, this allows me to skip mylunch break and take a one hour break in the middle of the afternoon forsport. And I usually go for a run around 2 or 3 pm, but that moment inthe day is not exactly the same every day. And most of the time, Iforget to tell everyone that I’m leaving the office. I recently jokedthat my 2020 resolution was that I’ll be more strict about telling whenI arrive and leave the “office”.
I was sure it can be done straight from my watch. And guess what, itcan!
The Slack Part
The slack API is pretty amazing and allows you to use a personal webhook and a curl call to send messages to a selected channel on Slack.
Note: there are several packages in R that can be used to send messagesto Slack, for example {slackr}
: https://github.com/hrbrmstr/slackr &{slackteams}
: https://github.com/yonicd/slackteams. But as I justwanted to make a simple, unique call, it was more straightforward towrite it directly.
So:
Go to
https://api.slack.com/
Click on Start Building
Add an app name and add it to a workspace
- Add a new “Incoming Webhooks”, and select the Channel to post in
And Tadaa 🎉 you now have a curl call that looks like this:
curl -X POST -H 'Content-type: application/json'
--data '{"text":"Hello, World!"}'
https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE
Now time to turn this into an API.
Node API
Here is a very simple API built in NodeJS:
const express = require('express')
const app = express()
app.get('/', function (req, res) {
const request = require('request');
const options = {
url: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE',
json: true,
body: {
text: "I'm off for a run!"
}
};
request.post(options);
res.send('OK')
})
app.listen(9999, function () {
console.log('API listening on port 9999!')
})
R API
And with R:
library(plumber)
#* @get /
function() {
httr::POST(
url = 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL/HERE',
body = list(
text = "I'm off for a run!"
),
encode = "json"
)
}
Adding this to the watch
I discovered that Garmin has a widget called “API calls”, that let youenter an API endpoint, and the API call is done from thewatch.
https://apps.garmin.com/en-US/apps/ac9a81ab-a52d-41b3-8c14-940a9de37544
I just discovered that I can send API calls from my Garmin watch and I'mvery excited about this and that also made me realized that I'mdefinitely a big nerd.pic.twitter.com/2h4fk8tCnf
— Colin Fay 🤘 (@_ColinFay)January7,2020
So here it is, I’ve got a Widget on my watch that I can use to sendmessage on Slack 🎉
You know you're a nerd when you deploy an API that posts to Slack fromyour Garmin watch so that you can tell your team that you're going for arun instead of just… you know… typing it.pic.twitter.com/f2cwn1pxd7
— Colin Fay 🤘 (@_ColinFay)January7,2020
Posted on January 8, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.