import requests
import time
import logging
import json
API_URL = "https://hotel101app.com/api/auth/login"
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
CHAT_ID = "YOUR_CHAT_ID"
AVAILABLE_CHECK_INTERVAL = 20
UNAVAILABLE_CHECK_INTERVAL = 300
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def check_registration():
try:
time.sleep(1) # Add a small delay
response = requests.get(API_URL)
logger.info(f"API response status code: {response.status_code}")
logger.info(f"API response content: {response.text[:200]}") # Log first 200 characters of response
if response.status_code == 200:
try:
json_response = response.json()
# Check if 'message' key exists and its value indicates availability
if 'message' in json_response and json_response['message'] != 'Registration is closed':
return True
except json.JSONDecodeError:
logger.error("Failed to parse JSON response")
return False
except requests.RequestException as e:
logger.error(f"Error checking registration: {e}")
return False
def send_telegram_message(message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
params = {
"chat_id": CHAT_ID,
"text": message
}
try:
response = requests.get(url, params=params)
logger.info(f"Telegram API response: {response.status_code}")
return response.status_code == 200
except requests.RequestException as e:
logger.error(f"Error sending Telegram message: {e}")
return False
def main():
logger.info("Starting registration checker...")
last_status = None
last_notification_time = 0
while True:
current_status = check_registration()
current_time = time.time()
if current_status:
message = "The registration for hotel101app is currently available!"
check_interval = AVAILABLE_CHECK_INTERVAL
else:
message = "The registration for hotel101app is currently unavailable."
check_interval = UNAVAILABLE_CHECK_INTERVAL
if current_status != last_status or (current_time - last_notification_time >= check_interval):
logger.info(message)
if send_telegram_message(message):
logger.info("Notification sent successfully")
last_notification_time = current_time
else:
logger.error("Failed to send notification")
last_status = current_status
time.sleep(check_interval)
if __name__ == "__main__":
main()