Created
June 7, 2019 07:43
-
-
Save gimKondo/94a7991b1dbc9cef128cca79c6002944 to your computer and use it in GitHub Desktop.
Hook and notify channel creation and emoji registration of Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import logging | |
import urllib.request | |
# Bot's "Verification Token" at "Basic Information" | |
SLACK_VERIFY_TOKEN = os.environ['SLACK_VERIFY_TOKEN'] | |
# Bot's "Bot User OAuth Access Token" at "Install App" | |
SLACK_BOT_USER_ACCESS_TOKEN = os.environ['SLACK_BOT_USER_ACCESS_TOKEN'] | |
# User name of response bot | |
SLACK_BOT_USER_NAME = os.environ['SLACK_BOT_USER_NAME'] | |
# Icon emoji of response bot | |
SLACK_ICON_EMOJI = os.environ['SLACK_ICON_EMOJI'] | |
# channel name for notifying something (e.g. created channel) | |
SLACK_NOTIFY_CHANNEL = os.environ['SLACK_NOTIFY_CHANNEL'] | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
""" | |
Entry point | |
""" | |
logger.info("Event: " + str(event)) | |
body = json.loads(event["body"]) | |
# for verification | |
if "challenge" in body: | |
return make_response(200, {}, {"challenge": body["challenge"]}) | |
# check request token | |
if body.get("token") != SLACK_VERIFY_TOKEN: | |
return make_response(403, {}, "Forbidden Request") | |
# check parameters | |
if not "event" in body: | |
return make_response(400, {}, "Invalid Parameter(lack of 'event')") | |
# execute reaction | |
return execute_event_reaction(body["event"]) | |
def execute_event_reaction(event): | |
""" | |
[parameters] | |
event: event paramerter | |
""" | |
event_type = event["type"] | |
if event_type == "channel_created": | |
return send_msg_to_slack(SLACK_NOTIFY_CHANNEL, "#%s が生えたぜ ヒャッハー" % event["channel"]["name"]) | |
elif event_type == "emoji_changed" and event["subtype"] == "add": | |
emoji_name = event["name"] | |
return send_msg_to_slack(SLACK_NOTIFY_CHANNEL, ":%s: %s が咲いたぜ ヒャッハー" % (emoji_name, emoji_name)) | |
return make_response(400, {}, "Unknown request type [%s]" % body_event["type"]) | |
def make_response(statusCode, headers, body): | |
""" | |
[parameters] | |
statusCode: HTTP status code | |
headers: response headers(if you don't need, pass empty dictionary) | |
body: response as dictionary | |
""" | |
res = { | |
"statusCode": statusCode, | |
"headers": headers, | |
"body": json.dumps(body) | |
} | |
logger.info("Response: " + str(res)) | |
return res | |
def send_msg_to_slack(channel: str, msg: str): | |
""" | |
[parameters] | |
channel: channel ID or name to send message | |
msg: message to post | |
""" | |
url = "https://slack.com/api/chat.postMessage" | |
headers = { | |
"Content-Type": "application/json; charset=UTF-8", | |
"Authorization": "Bearer {0}".format(SLACK_BOT_USER_ACCESS_TOKEN) | |
} | |
data = { | |
"token": SLACK_BOT_USER_ACCESS_TOKEN, | |
"channel": channel, | |
"username": SLACK_BOT_USER_NAME, | |
"icon_emoji": SLACK_ICON_EMOJI, | |
"link_names": True, | |
"text": msg, | |
} | |
req = urllib.request.Request(url, data=json.dumps(data).encode("utf-8"), method="POST", headers=headers) | |
with urllib.request.urlopen(req) as res: | |
body = json.load(res) | |
return make_response(200, {}, body) | |
return make_response(400, {}, body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment