Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (45 sloc) 1.94 KB
import sys
import json
import random
import os
import requests
def handle(req):
"""handle a request to the function
req (str): request body
r = json.loads(req)
if "challenge" in r:
return challenge(r)
with open("/var/openfaas/secrets/incoming-webhook-url") as webhook_url_text:
webhook_url =
if "event" in r:
target_channel = os.getenv("target_channel")
return process_event(r, target_channel, webhook_url)
return "Nothing to do with webhook"
def challenge(r):
if r["type"] == "url_verification":
res = {"challenge": r["challenge"]}
return json.dumps(res)
def process_event(r, target_channel, webhook_url):
event_type = r["event"]["type"]
if r["event"]["channel"] == target_channel:
if event_type == "member_joined_channel":
if "user" in r["event"]:
user_name = r["event"]["user"]
who = "<@{}>".format(user_name)
positive_emoticons = [":openfaas:", ":whale:", ":thumbsup:", ":wave:", ":sunglasses:", ":ok_hand:", ":chart_with_upwards_trend:", ":sunrise:", ":smiley:", ":smiley_cat:", ":parrot:", ":rocket:", ":100:", ":muscle:", ":signal_strength:", ":man-cartwheeling:"]
emoticons = build_emoticons(positive_emoticons)
msg = {"text": "Let's all welcome {} to the community! {} ".format(who, emoticons.strip())}
out_req =, json=msg)
return ("{} response from Slack: {}".format(str(out_req.status_code), out_req.text))
return "Cannot process event_type: {}".format(event_type)
def build_emoticons(emoticons):
emoticon_str = ""
sample = random.sample(emoticons, 5)
for emoticon in sample:
emoticon_str = emoticon_str + emoticon + " "
return emoticon_str
def log_event(req):