Geophysics_class / core /notifications.py
cwadayi's picture
Upload 8 files
57b8366 verified
# core/notifications.py
import os
import threading
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
PushMessageRequest,
TextMessage,
ApiException
)
def send_line_notification(message_text):
"""Sends a LINE notification to the specified user."""
access_token = os.environ.get('LINE_CHANNEL_ACCESS_TOKEN')
user_id = os.environ.get('YOUR_LINE_USER_ID')
if not access_token or not user_id:
print("LINE Secrets not found. Skipping notification.")
return
configuration = Configuration(access_token=access_token)
try:
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.push_message(
PushMessageRequest(to=user_id, messages=[TextMessage(text=message_text)])
)
print("LINE notification sent successfully in background!")
except ApiException as e:
print(f"Error sending LINE notification in background: {e.body}")
def send_line_notification_in_background(message_text):
"""Creates and starts a new thread to send a LINE notification, avoiding blocking the main app."""
notification_thread = threading.Thread(
target=send_line_notification,
args=(message_text,)
)
notification_thread.start()