feat: implement Facebook bot automation with UI interaction and image attachment capabilities

This commit is contained in:
Victor Phan
2026-07-23 15:02:09 +07:00
parent fea2789bd4
commit 0b722006d2
2 changed files with 24 additions and 9 deletions
+22 -8
View File
@@ -31,7 +31,7 @@ class FacebookBot:
time.sleep(2)
return True
def post_comment(self, group_id: str, content: str, local_image_path: str = None) -> dict:
def post_comment(self, group_id: str, content: str, local_image_path: str = None, post_index: int = 1) -> dict:
try:
self.d.screen_on()
@@ -55,10 +55,11 @@ class FacebookBot:
return {"status": "error", "message": "Group không tồn tại hoặc không có quyền truy cập."}
# Quét nút Bình luận, nếu không thấy thì vuốt xuống (vì ảnh bìa group che mất)
logging.info("Tìm nút Bình luận của bài viết đầu tiên...")
logging.info(f"Tìm nút Bình luận của bài viết thứ {post_index}...")
comment_btn = None
current_post_count = 1
for _ in range(7): # Thử vuốt tối đa 7 lần
for _ in range(30): # Tăng số lần vuốt tối đa lên để có thể cuộn qua nhiều bài
# Sửa lỗi: Nút Thích có description là "bày tỏ cảm xúc về bình luận" nên Regex .*bình luận.* sẽ bắt nhầm nút Thích!
# Do đó ta chỉ tìm đúng chữ Bình luận đứng đầu, hoặc chữ Comment.
c_desc_vn = self.d(descriptionMatches="(?i)^bình luận$")
@@ -66,12 +67,25 @@ class FacebookBot:
c_desc_en = self.d(descriptionMatches="(?i)^comment$")
c_text_en = self.d(textMatches="(?i)^comment$")
if c_desc_vn.exists: comment_btn = c_desc_vn; break
if c_text_vn.exists: comment_btn = c_text_vn; break
if c_desc_en.exists: comment_btn = c_desc_en; break
if c_text_en.exists: comment_btn = c_text_en; break
btn = None
if c_desc_vn.exists: btn = c_desc_vn
elif c_text_vn.exists: btn = c_text_vn
elif c_desc_en.exists: btn = c_desc_en
elif c_text_en.exists: btn = c_text_en
# Vuốt lên một chút
if btn and btn.exists:
if current_post_count == post_index:
comment_btn = btn
break
else:
logging.info(f"Đã thấy bài viết thứ {current_post_count}, đang bỏ qua để tìm bài {post_index}...")
# Vuốt qua bài viết hiện tại bằng 1 cú vuốt dài (70% màn hình)
self.d.swipe_ext("up", scale=0.7)
time.sleep(2)
current_post_count += 1
continue
# Nếu không thấy nút nào, vuốt lên một chút để tìm tiếp
self.d.swipe_ext("up", scale=0.5)
time.sleep(1.5)
self.dump_ui_for_debug("fb_step2_scrolling.xml")
+2 -1
View File
@@ -39,6 +39,7 @@ class FacebookCommentRequest(BaseModel):
content: str
image_url: Optional[str] = None
image_base64: Optional[str] = None
post_index: int = Field(default=1, description="Số thứ tự bài viết (1 là bài đầu tiên)")
@app.post("/get_commission")
def get_commission_endpoint(req: CommissionRequest):
@@ -114,7 +115,7 @@ def post_facebook_comment(req: FacebookCommentRequest):
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)
result = bot.post_comment(str(req.group_id), str(req.content), local_image_path, req.post_index)
# Dọn dẹp ảnh tạm sau khi xong
if local_image_path and os.path.exists(local_image_path):