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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import line_fucntion as LINE
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
LINE_AUDIENCE_UPLOAD = "https://api.line.me/v2/bot/audienceGroup/upload"
|
| 8 |
+
LINE_NARROWCAST = "https://api.line.me/v2/bot/message/narrowcast"
|
| 9 |
+
LINE_REQUEST_DATA = "https://api.line.me/v2/bot/insight/message/event?requestId="
|
| 10 |
+
LINE_AUDIENCE_DATA = "https://api.line.me/v2/bot/audienceGroup/"
|
| 11 |
+
LINE_DELETE_AUDIENCE = "https://api.line.me/v2/bot/audienceGroup"
|
| 12 |
+
LINE_PUSH = "https://api.line.me/v2/bot/message/push"
|
| 13 |
+
|
| 14 |
+
def push_manneger(line_access_token, file_option, list_uid, line_uid_file, option, image_address, image_alt, image_width, image_height, text_message=None, endpoint_url=None, dynamic_parameter=None):
|
| 15 |
+
if file_option == "Text":
|
| 16 |
+
list_uid_to_push = list_uid.split("\n")
|
| 17 |
+
else:
|
| 18 |
+
if line_uid_file is None:
|
| 19 |
+
return "Error: No LINE UID file uploaded."
|
| 20 |
+
|
| 21 |
+
else:
|
| 22 |
+
file_path = line_uid_file.name
|
| 23 |
+
df = pd.read_csv(file_path)
|
| 24 |
+
list_uid_to_push = df['line_uid'].unique()
|
| 25 |
+
|
| 26 |
+
if list_uid_to_push:
|
| 27 |
+
|
| 28 |
+
headers = LINE.line_header(line_access_token)
|
| 29 |
+
|
| 30 |
+
amount_to_push = len(list_uid_to_push)
|
| 31 |
+
push_result = 0
|
| 32 |
+
|
| 33 |
+
if "?" in endpoint_url:
|
| 34 |
+
final_url = endpoint_url + "&"
|
| 35 |
+
else:
|
| 36 |
+
final_url = endpoint_url + "?"
|
| 37 |
+
|
| 38 |
+
if option == "Single Image map":
|
| 39 |
+
for uid in list_uid_to_push:
|
| 40 |
+
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
|
| 41 |
+
image_object = LINE.image_map_single(altText=image_alt,
|
| 42 |
+
imageLink=image_address,
|
| 43 |
+
ladingpage=final_url_with_uid,
|
| 44 |
+
width=image_width,
|
| 45 |
+
height=image_height
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
push_message = LINE.message_push(to=uid, messageObject=[image_object])
|
| 49 |
+
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
|
| 50 |
+
|
| 51 |
+
if res.status_code == 200:
|
| 52 |
+
push_result += 1
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
|
| 56 |
+
|
| 57 |
+
return message_result
|
| 58 |
+
|
| 59 |
+
elif option == "Image map with text":
|
| 60 |
+
for uid in list_uid_to_push:
|
| 61 |
+
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
|
| 62 |
+
image_object = LINE.image_map_single(altText=image_alt,
|
| 63 |
+
imageLink=image_address,
|
| 64 |
+
ladingpage=final_url_with_uid,
|
| 65 |
+
width=image_width,
|
| 66 |
+
height=image_height
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
text_object = LINE.single_message(text_message)
|
| 70 |
+
|
| 71 |
+
push_message = LINE.message_push(to=uid, messageObject=[image_object, text_object])
|
| 72 |
+
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
|
| 73 |
+
|
| 74 |
+
if res.status_code == 200:
|
| 75 |
+
push_result += 1
|
| 76 |
+
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
|
| 77 |
+
|
| 78 |
+
return message_result
|
| 79 |
+
|
| 80 |
+
elif option == "Text with Image map":
|
| 81 |
+
for uid in list_uid_to_push:
|
| 82 |
+
final_url_with_uid = final_url + f"{dynamic_parameter}=" + str(uid)
|
| 83 |
+
image_object = LINE.image_map_single(altText=image_alt,
|
| 84 |
+
imageLink=image_address,
|
| 85 |
+
ladingpage=final_url_with_uid,
|
| 86 |
+
width=image_width,
|
| 87 |
+
height=image_height
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
text_object = LINE.single_message(text_message)
|
| 91 |
+
|
| 92 |
+
push_message = LINE.message_push(to=uid, messageObject=[text_object, image_object])
|
| 93 |
+
res = requests.post(LINE_PUSH, headers=headers, json=push_message)
|
| 94 |
+
|
| 95 |
+
if res.status_code == 200:
|
| 96 |
+
push_result += 1
|
| 97 |
+
|
| 98 |
+
message_result = f'''Total LINE UID: {amount_to_push}\nTotal push success: {push_result}'''
|
| 99 |
+
return message_result
|
| 100 |
+
else:
|
| 101 |
+
return '''Out of condition, Please select message object in "Single Image", "Image with text", "Text with Image"'''
|
| 102 |
+
|
| 103 |
+
def reset_inputs():
|
| 104 |
+
return "", None, "Single Image", "", "", "" # Reset inputs to default values
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# Gradio UI Layout
|
| 108 |
+
def main():
|
| 109 |
+
with gr.Blocks() as ui:
|
| 110 |
+
# Set page title and favicon using HTML
|
| 111 |
+
gr.HTML("""
|
| 112 |
+
<head>
|
| 113 |
+
<title>LINE Audience Manager</title>
|
| 114 |
+
<link rel="icon" href="https://media.licdn.com/dms/image/v2/C560BAQE6RcoYoG2AKw/company-logo_200_200/company-logo_200_200/0/1631385097016?e=1736380800&v=beta&t=KjZtErojYcSYLGCH1OskFpEAUMXsfqSMTbnmbRdETXU" type="image/x-icon">
|
| 115 |
+
</head>
|
| 116 |
+
""")
|
| 117 |
+
|
| 118 |
+
gr.Markdown("# YDM RDA: LINE Push message dynamic URL link")
|
| 119 |
+
|
| 120 |
+
# User input fields
|
| 121 |
+
line_access_token = gr.Textbox(label="LINE Channel Access Token")
|
| 122 |
+
audience_main_id = gr.Textbox(label="Main LINE Audience")
|
| 123 |
+
option = gr.Radio(label="Select Option", choices=["Single message","Single Image map", "Image map with text", "Text with Image map"])
|
| 124 |
+
gender = gr.CheckboxGroup(["Male", "Female"], label="Gender")
|
| 125 |
+
age = gr.Slider(15, 70, value=5, label='Age', info="Choose age range")
|
| 126 |
+
system = gr.CheckboxGroup(["ios", "Android"], info="Select Mobile system", label='Mobile System')
|
| 127 |
+
region = gr.CheckboxGroup(['Bangkok', 'Pattaya', 'Northern', 'Central', 'Southern', 'Eastern', 'NorthEastern', 'Western'], label="Thailand Region", info="Select Thailand Region")
|
| 128 |
+
friend_duration = gr.Slider(30, 180, label="Friend Duration", value=30, info="Choose friend duration")
|
| 129 |
+
|
| 130 |
+
# Message Conditional Inputs
|
| 131 |
+
image_address = gr.Textbox(label="Your Image address", visible=False)
|
| 132 |
+
image_alt = gr.Textbox(label="Your Image ALT", visible=False)
|
| 133 |
+
image_width= gr.Textbox(label="Your Image Width", visible=False)
|
| 134 |
+
image_height= gr.Textbox(label="Your Image Height", visible=False)
|
| 135 |
+
text_message = gr.Textbox(label="Your Text message", visible=False)
|
| 136 |
+
endpoint_url = gr.Textbox(label="Your Endpint URL", visible=False)
|
| 137 |
+
dynamic_parameter = gr.Textbox(label="Your dynamic LINE UID parameter name", visible=False)
|
| 138 |
+
|
| 139 |
+
def toggle_inputs_file(option):
|
| 140 |
+
if option == "Text":
|
| 141 |
+
return (
|
| 142 |
+
gr.update(visible=True), gr.update(visible=False)
|
| 143 |
+
)
|
| 144 |
+
else:
|
| 145 |
+
return (
|
| 146 |
+
gr.update(visible=False), gr.update(visible=True)
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
def toggle_inputs(option):
|
| 150 |
+
if option == "Single Image map":
|
| 151 |
+
# Show only image-related fields
|
| 152 |
+
return (
|
| 153 |
+
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
| 154 |
+
gr.update(visible=True), gr.update(visible=False), gr.update(visible=True),
|
| 155 |
+
gr.update(visible=True)
|
| 156 |
+
)
|
| 157 |
+
elif option == "Image map with text":
|
| 158 |
+
# Show all fields
|
| 159 |
+
return (
|
| 160 |
+
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
| 161 |
+
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
| 162 |
+
gr.update(visible=True)
|
| 163 |
+
)
|
| 164 |
+
elif option == "Text with Image map":
|
| 165 |
+
# Show only text-related fields
|
| 166 |
+
return (
|
| 167 |
+
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
| 168 |
+
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
|
| 169 |
+
gr.update(visible=True)
|
| 170 |
+
)
|
| 171 |
+
# file_option.change(fn=toggle_inputs_file, inputs=file_option, outputs=[list_uid, line_uid_file])
|
| 172 |
+
# option.change(fn=toggle_inputs, inputs=option, outputs=[image_address, image_alt, image_width, image_height, text_message, endpoint_url, dynamic_parameter])
|
| 173 |
+
|
| 174 |
+
# # Submit button and output
|
| 175 |
+
# submit_btn = gr.Button("Submit")
|
| 176 |
+
# reset_btn = gr.Button("Reset")
|
| 177 |
+
# output = gr.Textbox(label="Result", interactive=False)
|
| 178 |
+
|
| 179 |
+
# submit_btn.click(fn=push_manneger,
|
| 180 |
+
# inputs=[line_access_token, file_option, list_uid, line_uid_file, option, image_address, image_alt, image_width, image_height, text_message, endpoint_url, dynamic_parameter],
|
| 181 |
+
# outputs=output)
|
| 182 |
+
|
| 183 |
+
# # Connect the reset function to Gradio UI
|
| 184 |
+
# reset_btn.click(fn=reset_inputs,
|
| 185 |
+
# inputs=[],
|
| 186 |
+
# outputs=[line_access_token, file_option, list_uid,line_uid_file, option, image_address, image_alt, image_width, image_height, text_message, endpoint_url, dynamic_parameter, output])
|
| 187 |
+
|
| 188 |
+
ui.launch(share=True)
|
| 189 |
+
|
| 190 |
+
if __name__ == "__main__":
|
| 191 |
+
main()
|