Discussions

Ask a Question
Back to All

Creating Notifications when Employees aren't clocked in on time Issues

We're building an automated system to notify our managers if any employees haven't clocked in by 8:30 AM MDT. We're leveraging AWS Lambda and Connecteam's API to achieve this.

Steps Taken:

API Setup: We set up a Lambda function that queries the /time_clock/v1/time_clocks/{timeclockid}/time_activities endpoint. We've verified that the time clock IDs (8001584 and 8163931) are correct.
Environment Configuration: The API key is securely stored in AWS Lambda's environment variables.
Function Logic: The function fetches time activities for the specified time clocks, checks the clock_in_time field, and compares it to the 8:30 AM MDT deadline. If no one has clocked in by that time, a Slack notification is triggered via AWS Chatbot.
Issues Encountered:

API Response: Despite using the correct endpoint and IDs, the API consistently returns {"detail": "Not Found"}.
Time Activity Data: We’ve verified that employees did clock in before 8:30 AM MDT, but this data isn’t being reflected in the API response.
Troubleshooting: We’ve tried logging the full API response, verified the clock IDs, and ensured the API key has the appropriate permissions.

Request: We’re seeking guidance on why the API might be returning a "Not Found" error and whether there might be any additional steps or configurations needed to correctly retrieve time activities for the specified clocks.

Current Code:

import json
import urllib3
import os
from datetime import datetime

# Retrieve API key from environment variables
CONNECTEAM_API_KEY = os.environ['CONNECTEAM_API_KEY']

# Time Clock IDs
TIME_CLOCK_IDS = {
    "In-House Clock": "8001584",
    "Sub Check in": "8163931"
}

def check_clock_in():
    http = urllib3.PoolManager()
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {CONNECTEAM_API_KEY}"
    }
    
    not_clocked_in = []

    for clock_name, clock_id in TIME_CLOCK_IDS.items():
        url = f"https://api.connecteam.com/time-clock/v1/time-clocks/{clock_id}/time_activities"
        
        try:
            response = http.request('GET', url, headers=headers)
            
            # Log the raw response for debugging
            print("Full Response from API:")
            print(response.data.decode('utf-8'))  # Log entire response for better insight
            
            activities = json.loads(response.data.decode('utf-8'))

            # Check for clock-in activities by 8:30 AM
            for activity in activities:
                if 'clock_in_time' in activity and activity['clock_in_time'] is not None:
                    clock_in_time = datetime.fromisoformat(activity['clock_in_time'])
                    if clock_in_time.time() <= datetime.now().replace(hour=8, minute=30).time():
                        break
            else:
                # If no one clocked in by 8:30 AM
                not_clocked_in.append(f"No one clocked in for {clock_name} by 8:30 AM")
        
        except Exception as err:
            print(f"An error occurred for {clock_name}: {err}")

    if not_clocked_in:
        notify_managers(not_clocked_in)

def notify_managers(employee_names):
    # This function will trigger the AWS Chatbot to notify Slack
    print("The following issue was detected:\n" + "\n".join(employee_names))

def lambda_handler(event, context):
    check_clock_in()
    return {
        'statusCode': 200,
        'body': json.dumps('Check completed successfully')
    }