import json from urllib2 import Request, urlopen, URLError, HTTPError import logging logger = logging.getLogger() logger.setLevel(logging.INFO) slack_hook_url = "" #get yours from the incoming webhooks you created in Slack display_username = "GitHub Alert from repo" #repo name will be tacked onto the end of this display_emoji = ":speak_no_evil:" #change the emoji from the list @ http://www.emoji-cheat-sheet.com/ def lambda_handler(event, context): #get the values we want from the GitHub pull request payload (https://developer.github.com/v3/activity/events/types/#pullrequestevent) username = event['pull_request']['user']['login'] repotitle = event['pull_request']['title'] actionmess = event['action'] pullrequestnum = event['number'] reponame = event['pull_request']['head']['repo']['name'] pullrequesturl = event['pull_request']['html_url'] #create the slack message slack_message = { 'text': "%s - %s pull request <%s|#%s> from %s"%(username, actionmess, pullrequesturl, str(pullrequestnum), reponame), 'username' : "%s %s"%(display_username, reponame), 'icon_emoji' : display_emoji } #send the message to slack try: req = Request(slack_hook_url, json.dumps(slack_message)) response = urlopen(req) response.read() logger.info(slack_message) except HTTPError as e: logger.error("Request failed: %d %s", e.code, e.reason) except URLError as e: logger.error("Server connection failed: %s", e.reason) return None