[PYTHON] Notify Slack of GitHub push

Slack settings

● Slack Help Center https://slack.com/intl/ja-jp/help/articles/115005265063-Slack-%E3%81%A7%E3%81%AE-Incoming-Webhook-%E3%81%AE%E5%88%A9%E7%94%A8

Issue the Incoming Webhook URL by following the steps in Setting Up an Incoming Webhook.

Google Cloud Platform (Cloud Function) settings

If you haven't registered for Google Cloud Platform, do so.

procedure

--Go to "Cloud Function". --Create a function with "Create Function". The settings are as follows

Name: Optional Allotted memory: optional Trigger: HTTP Authentication: Allow unauthenticated calls Source code: inline editor Runtime: Python 3.7 Function to be executed: Function name described in the source (slack_notice in sample)

Source code

sample.py


import requests
import json

def slack_notice(request):

    webhook_url = "https://hooks.slack.com/<Incoming Webhook URL>"
    request_json = request.get_json()

    if 'commits' in request_json:
        name = request_json['commits'][0]['author']['name']
        text = name+"Pushed."
        requests.post(webhook_url, data = json.dumps({
            "text": text
        }))

GitHub settings

Go to "Setteing"-> "Webhooks"-> "Add webhook" and set as below

Payload URL: URL written in the trigger of Google Cloud Platform Content type:application/json SSL verification:Enable SSL verification Which events would you like to trigger this webhook?:Just the push event. Active: Check

Then push for a trial!

Big img.png

Recommended Posts

Notify Slack of GitHub push
Regularly notify Slack of missed Backlog issues
Notify Slack of how Keras is learning
I tried to notify slack of Redmine update
Notify Slack of process state changes using Supervisor's EventListener
Notify LINE of train operation information
Github Interesting Repository ⓪ Table of Contents
Allow Slack to notify you of the end of a time-consuming program process