img

A QR-code based, Google Cloud Function hosted, Slack integrated Door bell!

2020-05-19

Door bell

We will be using the following technologies: Python/Flask Python/requests Google Cloud SDK Firebase Cloud Function Slack Web API

Let's get started!

We will be using the following technologies:

  • Python/Flask

  • Python/requests

  • Google Cloud SDK

  • Firebase Cloud Function

  • Slack Web API

Step 0: Slack Webhook URL

Get your Slack Custom Integration Web hook.

  1. Navigate to https://YOUR_COMPANY.slack.com/apps/manage/custom-integrations

  2. Click on Incoming Webhooks

  3. Add configuration

  4. Choose a channel and then press "Add Incoming WebHooks integration

  5. Note down the Webhoook URL. This will be used in the Python script below

Step 1: Download Google Cloud CLI

Start off by downloading and installing the Google Cloud CLI https://cloud.google.com/sdk/docs/#install_the_latest_cloud_tools_version_cloudsdk_current_version

Follow the instructions to install and setup your environment.

Make sure you run gcloud auth login to authenticate your terminal with Google.

Step 2: Writing the Python Code

In your project folder, create a new directory called functions and create all the needed files and folders.

mkdir functions
cd functions
mkdir dingdong
cd dingdong
touch main.py
touch requirements.py

Open up requirements.txt in your favourite editor and add the following dependencies. I use nano, so for me the command would be nano requirements.txt:

CacheControl==0.12.5
cachetools==2.1.0
certifi==2018.10.15
chardet==3.0.4
Click==7.0
docopt==0.4.0
enum34==1.1.6
firebase-admin==2.13.0
Flask==1.0.2
google-api-core==1.5.0
google-auth==1.5.1
google-cloud-core==0.28.1
google-cloud-firestore==0.30.0
google-cloud-storage==1.13.0
google-resumable-media==0.3.1
googleapis-common-protos==1.5.3
idna==2.7
itsdangerous==1.1.0
Jinja2==2.10
mailchimp==2.0.9
MarkupSafe==1.0
msgpack==0.5.6
protobuf==3.6.1
pyasn1==0.4.4
pyasn1-modules==0.2.2
pytz==2018.5
requests==2.20.0
rsa==4.0
six==1.11.0
urllib3==1.24
Werkzeug==0.14.1

Open up main.py in your favourite editor and lets start adding the actual code.

Pythonimport flask
import json
import requests

SLACK_URL = 'ENTER YOUR SLACK URL'


def create_slack_message(request):
    message = request.form.get('message')
    payload = {
        'username': 'DING DONG',
        'icon_emoji': ':ghost:',
        'text': 'Ding Dong! Kindly open the door. "{}"'.format(message)
    }
    r = requests.post(SLACK_URL, json.dumps(payload))
    return 'Thank You! One of my colleagues will open the door shortly!', 200


def render_form():
    return """
    <html>
        <form method="post">
            <input
                style="width: 100%" type="text" name="message"
                placeholder="Please enter a message"
            />
            <input
                style="width: 100%; margin-top:40px;"
                type="submit" value="Ding Dong!"
            >
        </form>
    </html>
    """


def dingdong(request):
    if request.path == '/' or request.path == '':
        if request.method == 'POST':
            return create_slack_message(request)
        else:
            return render_form(), 200
    return 'URL not found', 404

Make sure you replace the SLACK_URLvariable with your Slack Webhook URL

Step 3: Creating a Firebase App

Go to console.firebase.google.com and create a firebase app Once you've created an app move back to your console and while standing in the functionsdirectory, execute the following command:

gcloud beta functions deploy dingdong - runtime python37 - trigger-http - project YOUR_FIREBASE_PROJECT_NAME

Make sure to change YOUR_FIREBASE_PROJEcT_NAME with your actual firebase project name.

After the deployment is complete, the terminal will show you your https trigger url. It should be something like:

https://us-central1-YOUR_PROJECT_NAME.cloudfunctions.net/dingdong

Step 4: Generating a QR Code 🤳🏼

  1. Visit https://www.qr-code-generator.com/

  2. Paste your Cloud Function url

  3. Press "Create QR Code"

  4. Download and Print your QR Code

Edit:

There are several other QR Generators as well. I recently got "Free QR Code Generator" recommended by Fabiola Castro.

Thats it, folks! If you have any questions or thoughts, please feel free to reach out to us either by email or our Instagram!