Example #1
0
def listener():
    logger.info("Event Received:")
    slack_event = json.loads(request.data)

    logger.debug(slack_event)

    #  Slack URL Verification
    #  Slack will post a challenge event in order to verify who we are.  This will be how we respond

    if "challenge" in slack_event:
        logger.info("Challenge Received")
        return make_response(slack_event["challenge"], 200, {"content_type":  "application/json"})

    #  Validation that the event is truly from slack
    if not validate_call.validate_request(request):
        logger.critical("Failed message validation!")
        message = "Invalid Slack Token Verification! \nExpected:  %s, \nReceived:  %s" % (slack_event["token"],
                                                                                          jbot.verification)

        make_response(message, 403, {"X-Slack-No-Retry": 1})

    #  Process incoming events
    if "event" in slack_event:
        event_type = slack_event["event"]["type"]
        logger.debug("Received event type:  %s", event_type)
        logger.debug("Full event data:  \n %s", slack_event)

    return make_response("[Test] These are not the droids\
                             you're looking for.", 200, {"X-Slack-No-Retry": 1})
Example #2
0
def adduser():
    """
    Adds users to the database:
    /adduser @name (user|manager)
    :return:
    """

    logger.info("Add User function called")
    logger.debug("Event Received: \n %s", request)

    #  Check to make sure arguments are passed
    if not request.form['text']:
        message = {'text':  'No arguments specified, please try again'}
        return jsonify(message)

    #  Get the arguments
    regex = '^<@([^|]+)\|([^>]+)>\s(manager|user)'

    #  Check to find there are three matches:
    logger.debug("Found this many options given:  %s", re.compile(regex).groups)
    

    #regex_match = re.match(r'^<@([^|]+)\|([^>]+)>\s(manager|user)', request.form['text'], re.M|re.I)
    #arguments = request.form['text']
    #(user, add_role) = arguments.split(" ")



    #match_objects = re.match(r'^\<\@([^|]+)\|([^>]+)\>\s(manager|user)', user, re.M|re.I)
    #regex_match = re.match(r'^<@([^|]+)\|([^>]+)>', user, re.M|re.I)
    #logger.debug("Match_objects:  %s", match_objects)

    #if regex_match:
    #    add_username = match_objects.group(1)
    #    add_userid = match_objects.group(2)
    #    logger.info("found add_name:  %s", add_username)
    #    logger.info("found add_userid:  %s", add_userid)


    #logger.debug("Received following request:  %s", request.form)

    heartbeat_message = {'text':  'gotit'}
    return jsonify(heartbeat_message)
Example #3
0
def validate_request(request):
    """
    Validates the request is officially from slack.  See https://api.slack.com/docs/verifying-requests-from-slack
    for more information around this.
    :param request:
    :return:
    """
    #  Get the our signing secret from the config
    internal_slack_signing_secret = app_config['slack']['slack_signing_secret']
    encoded_internal_signing = internal_slack_signing_secret.encode()

    #  Get what Slack sent us
    sent_slack_signature = request.headers.get('X-Slack-Signature')
    request_timestamp = request.headers.get('X-Slack-Request-Timestamp')

    #  Get the body of the request.  This was seriously a pain.
    request_body = request.get_data()
    request_body = request_body.decode('utf-8')
    version = "v0"
    separator = ":"

    #  Build the signature line
    request_signature_line = version + separator + request_timestamp + separator + request_body
    encoded_signature_line = request_signature_line.encode()

    #  Now to hash it
    hashed_signature = hmac.new(encoded_internal_signing,
                                encoded_signature_line, hashlib.sha256)
    hexhashedsignature = "v0=" + hashed_signature.hexdigest()

    #  This took me all day, but it works!
    if hexhashedsignature != sent_slack_signature:
        logger.critical("Message not validated!  Something is wrong!")
        return False

    else:
        logger.info("Validated request from Slack")
        return True
Example #4
0
def heartbeat():
    logger.info("Heartbeat function called")
    heartbeat_message = {'text':  'I\'m Alive'}
    return jsonify(heartbeat_message)
Example #5
0
def echo():
    logger.info("Echo function called")
    logger.info(request.form)
    echo_message = {'text': 'Echo Completed'}
    return jsonify(echo_message)