Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Building an API in NodeJS and R to send message to Slack from your Garmin watch.
Why on earth
ThinkR is a remote company, meaning that we all work from our home. On top of other cool things about remote work, this allows me to skip my lunch break and take a one hour break in the middle of the afternoon for sport. And I usually go for a run around 2 or 3 pm, but that moment in the day is not exactly the same every day. And most of the time, I forget to tell everyone that I’m leaving the office. I recently joked that my 2020 resolution was that I’ll be more strict about telling when I arrive and leave the “office”.
I was sure it can be done straight from my watch. And guess what, it can!
The Slack Part
The slack API is pretty amazing and allows you to use a personal web hook 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 messages to Slack, for example {slackr}
: https://github.com/hrbrmstr/slackr& {slackteams}
: https://github.com/yonicd/slackteams. But as I just wanted to make a simple, unique call, it was more straightforward to write 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:
constexpress=require('express')constapp=express()app.get('/',function(req,res){constrequest=require('request');constoptions={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 you enter an API endpoint, and the API call is done from the watch.
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’m very excited about this and that also made me realized that I’m definitely a big nerd. pic.twitter.com/2h4fk8tCnf
— Colin Fay
(@\_ColinFay) January 7, 2020
So here it is, I’ve got a Widget on my watch that I can use to send message on Slack
You know you’re a nerd when you deploy an API that posts to Slack from your Garmin watch so that you can tell your team that you’re going for a run instead of just… you know… typing it. pic.twitter.com/f2cwn1pxd7
— Colin Fay
(@\_ColinFay) January 7, 2020
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.