71 seconds to build your free custom webhook (Illustrated step by step)

Moe Ibrahim
Mar 9, 2018 · 7 min read

Disclaimer : I’m not affiliated with gitlab in anyway, or being compensated from any recommendations I make, in fact this tutorial with minimal tweaking can work with Bitbucket-CI, Travis-CI, Circle-CI and others.

If you you want to skip the discussion below and go straight to the instructions, scroll to where it says Let’s get you started.

So Google is cracking down on android apps background services.

No wonder that the bitcoin app I’m using did not alert me for the recent price dip.

There are many scenarios when you want a service to continuously check for a condition, and alert you when this condition is met. something like a webhook.

The first definition that pops up when you google webhook is:

A WebHook is an HTTP callback: an HTTP POST that occurs when something happens; a simple event-notification via HTTP POST. A web application implementing WebHooks will POST a message to a URL when certain things happen.

In the previous tutorial we have tackled running periodic jobs with gitlab.

In this very similar tutorial we will add conditions, we will create a webhook:

  • A periodic job will run daily : GET bitcoin price
  • It will check if a condition is met : if the bitcoin price is greater than x and lesser than y
  • If the condition is met it will POST an alert : we will direct this post request to an http request inspector to see it in action, BUT we can also ,with the proper authentication, direct it to another service like IFTTT or Zapier to cause it to trigger 100s of connected services. For example let your smart lights turn green if bitcoin is within a specific price range, or even trigger a song to play.

In another article we discuss in details how to connect this same project to IFTTT and have IFTTT alert us with a phone call, email, or tweet when the price of bitcoin reaches a specific range.

IFTTT allows you to connect to hundreds of services and smart appliances, list of IFTTT services

There are many ways for creating webhooks, but this is my favourite way because:

  1. It’s easy, this whole tutorial can be completed from a browser, you can use your mobile phone and in 71 seconds you would have a functioning webhook.
  2. It can be set up in seconds : I don’t need to set up a server on my own, not even a cloud instance, so I can focus on creating what I need, rather than get tangled in system admin tasks .
  3. Although it’s also easy to run a cron job from an always-on always-connected computer or a server, like a Raspberry Pi. This is not practical for everyone, specially travellers and digital nomads.
  4. It provides more freedom and versatility : you can obviously use IFTTT to trigger events, IFTTT is very slick with a great API and a nice free tier, but then you’re limited to the integrations IFTTT does and does not provide, also you’re limited by their caps, for example free IFTTT caps your tweets at 100/day, although that’s perfectly fine, you can’t compare that to the actual Twitter cap of 2,400 tweets/day.
  5. This can last, with other services you’ll end up getting a bill, either after you pass a certain quota or because it can run as long as gitlab continues to offer 2000 min/month of free build time on shared build runners. this is enough to run our sample code here 16 times per day, this is once every 90 minutes each month. As of this writing Bitbucket on the other hand only offers 50 minutes/month.
Bitbucket hosted CI free includes 50 min /month build time

5. Gitlab integrates the code repo with the build system, sure you can host your code on Github and build with Travis or Circle-CI, but I would like to login once and manage both the repo and the build process, and I don’t have to mess around with ssh keys to access another service.

6. Gitlab allows private repos : Travis CI allows free unlimited build minutes for personal projects, but the last I’ve checked Travis-CI only supports free builds for public repos.

7. Gitlab uses containers to allow users to create their build stack, so although for this tutorial we’re using a simple bash file, in reality you can specify a container that tests, builds and deploys your Android app, Java application using Maven or Gradle, Ruby, Node , Python, Django and a long list of applications, better yet Gitlab has ready standard templates to automatically set the build stack for you. I’ve forked a list of ready templates here.

So in essence you provide your code, and Gitlab-CI provides the right environment to build your code (you just tweak the YAML file template provided by Gitlab-CI). Here is the official Gitlab-CI examples page.

If you are into browser automation, there’s a cool tutorial by Joyz for using Gitlab-CI to automatically spin up a docker container ready with chrome-headless to run Selenium tests.

8. The project build file .gitlab-ci.yml is very similar to the configuration file used in many other services, including Travis-CI, Bitbucket-CI Circle-CI and others, even Jenkins has a YAML file plugin. this makes migration easy if the need arise.

9. Not that we used an open API to retrieve Bitcoin price, this does not mean that we are limited to APIs that does not require private key or token authentication.

Like other services you can store private keys as variables in Gitlab-CI, for example MY_API_PRIVATE_KEY , and then use ‘$MY_API_PRIVATE_KEY’ in your .gitlab-ci.yml file to authenticate when calling the API.

you can store private keys as variables in Gitlab-CI to authenticate API calls

if you do that make sure that you set your build Pipeline toprivate, otherwise other will be able to see your keys in plain text.

Let’s get you started:

Level : All levels

Requirements : Any web browser

  1. Sign in (or create a new account) at Gitlab.com (10 seconds)

2. Create a new Project : Click on New Project button to create a new repo, in the name field type btc-webhook or any other name. (9 seconds)

Then save it By clicking on Create Project (1 second)

3. Create a btc-price-alert.sh file in this new project : Click on New File, copy and paste the following snippet into the btc-price-alert.sh file then click save (10 seconds)

#!/bin/bash
echo 'request Bitcoin price';
x=$(curl https://min-api.cryptocompare.com/data/price?fsym=BTC\&tsyms=CAD)
echo 'removing all non digit from the response'
x=${x//[^0-9\.]/}
echo 'Bitcoin price is CA$ '"$x"echo 'removing decimals from the price'
x=${x%.*}
echo 'checking if the price within the defined range' if [ "$x" -ge 10000 -a "$x" -lt 14000 ]; then
echo 'price is within range, will post an alert'
curl -i -X POST https://putsreq.com/wkDdMQWhaOyalisaIe49 --data 'price='"$x"

else echo 'Price is not within range, no alert posted this time'
fi

This will check if the price of bitcoin above CA$ 10,000 and less than CA$ 14,000 . feel free to customize it.

4. Create another file, call it .gitlab-ci.yml file : (20 seconds) Click on the home menu icon to go back to your project’s home page.

click on the plus icon next to your project’s name and select New File from the menu
copy and paste the following snippet into the .gitlab-ci.yml file then click save
test: script: - bash btc-price-alert.sh

this is a simple commands, to run the bash file that we’ve created

Click on the Commite changes button, this will trigger it to build and run.

5. Schedule it to check price everyday : click on the CI/CD icon to expand the menu, select Schedules to set up a name and a timer for your webhook to trigger. (11 seconds)

click on New schedule button
Type in a name for the new schedule daily-bitcoin-price-alert, select to run it daily, or whenever you want it to run, then click Save
Your scheduled job has been saved

6. Congrats! you’re done. if the price within the range you’ve specified, the webhook will post an alert. go to to this link in putsreq.com to see it in action. (10 seconds)

This job will run everyday as long as your free 2000/month build minutes do not run out.

You can find the sample code here and previous read the jobs build logs here

we can still kick it up a notch

If still want to integrate your webhook with IFTTT, this can be easily be done

This will be the topic of the coming tutorial

HackerNoon.com

how hackers start their afternoons.

Moe Ibrahim

Written by

Developer 🇨🇦 Traveler

HackerNoon.com

how hackers start their afternoons.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade