feat: add FastAPI server and Facebook automation bot for device-based task execution

This commit is contained in:
Victor Phan
2026-07-23 14:50:39 +07:00
parent 384be28c2b
commit fea2789bd4
2 changed files with 54 additions and 13 deletions
+36 -6
View File
@@ -38,6 +38,7 @@ class FacebookCommentRequest(BaseModel):
group_id: Any
content: str
image_url: Optional[str] = None
image_base64: Optional[str] = None
@app.post("/get_commission")
def get_commission_endpoint(req: CommissionRequest):
@@ -79,18 +80,47 @@ def post_facebook_comment(req: FacebookCommentRequest):
bot = FacebookBot(device_ip=raw_ip)
local_image_path = None
if req.image_url:
# Tải ảnh về máy chủ Python tạm thời
if req.image_base64:
import base64
# Bỏ phần header data:image/jpeg;base64, nếu có
b64_data = req.image_base64
if "," in b64_data:
b64_data = b64_data.split(",", 1)[1]
local_image_path = f"temp_fb_{uuid.uuid4().hex}.jpg"
urllib.request.urlretrieve(str(req.image_url), local_image_path)
logging.info(f"Đã tải ảnh tạm thời: {local_image_path}")
with open(local_image_path, "wb") as f:
f.write(base64.b64decode(b64_data))
logging.info(f"Đã tạo ảnh từ chuỗi Base64: {local_image_path}")
elif req.image_url:
import re
url = str(req.image_url)
# Kiểm tra xem url có phải là một đường dẫn file nội bộ trên máy hay không
if os.path.exists(url):
local_image_path = url
logging.info(f"Sử dụng file ảnh nội bộ có sẵn: {local_image_path}")
else:
# Tự động chuyển link Google Drive sang link tải trực tiếp
if "drive.google.com" in url:
file_id_match = re.search(r"/d/([a-zA-Z0-9_-]+)", url)
if file_id_match:
file_id = file_id_match.group(1)
url = f"https://drive.google.com/uc?export=download&id={file_id}"
logging.info(f"Đã convert link Google Drive thành direct link: {url}")
# Tải ảnh về máy chủ Python tạm thời
local_image_path = f"temp_fb_{uuid.uuid4().hex}.jpg"
urllib.request.urlretrieve(url, local_image_path)
logging.info(f"Đã tải ảnh tạm thời từ internet: {local_image_path}")
result = bot.post_comment(str(req.group_id), str(req.content), local_image_path)
# Dọn dẹp ảnh tạm sau khi xong
if local_image_path and os.path.exists(local_image_path):
os.remove(local_image_path)
logging.info(f"Đã xóa ảnh tạm: {local_image_path}")
if "temp_fb_" in local_image_path:
os.remove(local_image_path)
logging.info(f"Đã dọn dẹp file tạm: {local_image_path}")
if isinstance(result, dict) and "status" in result:
# Trả về luôn JSON kết quả, n8n sẽ nhận HTTP 200 và tiếp tục vòng lặp