personalize_broadcast / line_fucntion.py
Jeangg's picture
add line_function.py
a300731
import pandas as pd
#DEF ZONE
def line_header(CHANNEL_LINE):
LINE_HEADER = {"Content-Type": "application/json", "Authorization": f"Bearer {CHANNEL_LINE}"}
return LINE_HEADER
def add_new_line_audience(file_path, audience_name, id_column='line_uid', description_key='description', is_ifa_audience="false"):
'''
Generates a payload for creating a new LINE audience based on a CSV file containing user IDs.
Args:
file_path (str): The path to the CSV file containing the audience data.
audience_name (str): The name or description of the audience, which will be included in the payload.
id_column (str, optional): The column name in the CSV file that contains the unique audience IDs. Default is 'line_uid'.
description_key (str, optional): The key name used in the payload for the audience description. Default is 'description'.
is_ifa_audience (str, optional): A flag indicating whether the audience is an IFA audience. Default is "false".
Returns:
dict: A dictionary payload with the following structure:
{
"description_key": "audience_name",
"isIfaAudience": "is_ifa_audience",
"audiences": [{"id": audience_id_1}, {"id": audience_id_2}, ...]
}
The 'audiences' list will contain the unique audience IDs found in the specified `id_column` of the CSV.
'''
df = pd.read_csv(file_path)
payload = {
description_key: audience_name,
"isIfaAudience": is_ifa_audience,
"audiences": []
}
# Loop through the unique audience IDs and add them to the payload
for audience_id in df[id_column].unique():
payload["audiences"].append({"id": audience_id})
return payload
def update_line_audience(file_path, audience_id):
'''
Generates a payload to update an existing LINE audience group with user IDs from a CSV file.
Args:
file_path (str): The path to the CSV file containing the audience data.
audience_id (int or str): The ID of the existing audience group that needs to be updated.
Returns:
dict: A dictionary payload with the following structure:
{
"audienceGroupId": audience_id,
"audiences": [{"id": audience_id_1}, {"id": audience_id_2}, ...]
}
The 'audiences' list will contain the unique audience IDs found in the 'line_uid' column of the CSV.
'''
# Load the CSV into a DataFrame
df = pd.read_csv(file_path)
# Prepare the payload with the audience group ID and empty audiences list
payload = {
"audienceGroupId": int(audience_id),
"audiences": []
}
# Loop through the unique audience IDs and add them to the payload
for audience_id in df['line_uid'].unique():
payload["audiences"].append({"id": audience_id})
return payload
def single_message(message):
json = {
"type": "text",
"text": f"{message}",
}
return json
def message_push(to, messageObject):
json = {
"to": to,
"messages":messageObject
}
return json
def image_map_single(altText, imageLink, ladingpage, width, height):
data = {
"type": "imagemap",
"baseUrl": imageLink,
"altText": altText,
"baseSize": {
"width": int(width),
"height": int(height)
}, "actions": [
{
"type": "uri",
"linkUri": ladingpage,
"area": {
"x": 0,
"y": 0,
"width": int(width),
"height": int(height)
}
}]
}
return data