Overview
Theenviar_registro() function is the core component of the WhatsApp Attendance Bot. It automatically sends attendance messages (check-in/check-out) to a WhatsApp group via the Ultramsg API based on the current UTC time.
Function Signature
Parameters
The function takes no direct parameters but requires three environment variables:Your Ultramsg instance ID. Obtained from the Ultramsg dashboard after creating an instance.
Your Ultramsg API token for authentication. Found in your Ultramsg instance settings.
The WhatsApp group ID where messages will be sent. Format:
[country_code][phone_number]@g.us (e.g., [email protected])Source Code
Location:bot.py:5-24
Function Behavior
Time-Based Logic
The function determines whether to send a check-in or check-out message based on the current UTC hour:- Check-in message: Sent when UTC hour < 18 (before 6:00 PM UTC)
- Check-out message: Sent when UTC hour >= 18 (6:00 PM UTC or later)
The function uses
datetime.datetime.utcnow().hour to get the current UTC hour. For Mexico CST timezone:- 15:00 UTC = 9:00 AM CST (check-in)
- 21:00 UTC = 3:00 PM CST (check-out)
Message Format
The function sends one of two hardcoded messages: Check-in (before 18:00 UTC):API Integration
Ultramsg API Endpoint
The function makes a POST request to the Ultramsg chat messages endpoint:Request Payload
Authentication token from Ultramsg
WhatsApp group ID in the format
[country_code][phone_number]@g.usThe message text to send to the group
Request Headers
Response Handling
Success Response
HTTP status code.
200 indicates success.JSON response from Ultramsg containing message delivery status
Example Output
Error Handling
Potential errors:- Missing environment variables:
os.getenv()returnsNone, causing the API URL or payload to be invalid - Network errors:
requests.post()may raiseConnectionErrororTimeout - API errors: Ultramsg may return 4xx or 5xx status codes
Usage Example
Direct Execution
GitHub Actions
The function is designed to be triggered automatically by GitHub Actions at scheduled times:Dependencies
- requests: HTTP library for making API calls (
pip install requests) - os: Built-in Python module for environment variable access
- datetime: Built-in Python module for time operations
Customization
To customize the function behavior, modify these sections inbot.py: