File size: 1,371 Bytes
57b8366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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()