| import sqlite3 |
| import os |
| import sys |
| import time |
| import asyncio |
|
|
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| from orchestrator.config import DB_PATH |
| from orchestrator.agent_utils import get_llm, get_browser_with_auth |
| from browser_use import Agent |
|
|
| async def agentic_reply(user_name: str, message: str, account_id: int) -> bool: |
| """ |
| Use Browser Agent to automatically send a private message to a high-intent user on Xiaohongshu. |
| """ |
| llm = get_llm() |
| browser = get_browser_with_auth() |
| |
| prompt = f""" |
| You are an automated customer service assistant for Xiaohongshu. |
| Please go to the Xiaohongshu messages center (https://creator.xiaohongshu.com/creator/message). |
| Find the user named '{user_name}' in the message list or comments. |
| Open a chat with them and send the following message: |
| "{message}" |
| |
| Click the send button. Return success if the message was sent, otherwise fail. |
| """ |
| |
| agent = Agent( |
| task=prompt, |
| llm=llm, |
| browser=browser |
| ) |
| |
| print(f"Starting agent to reply to {user_name} (Account {account_id})...") |
| try: |
| result = await agent.run() |
| print(f"Agent finished replying to {user_name}.") |
| return True |
| except Exception as e: |
| print(f"Agent reply failed: {e}") |
| return False |
| finally: |
| await browser.close() |
|
|
| async def main_async(): |
| conn = sqlite3.connect(DB_PATH) |
| cursor = conn.cursor() |
| |
| |
| try: |
| cursor.execute("ALTER TABLE interaction_record ADD COLUMN lead_processed INTEGER DEFAULT 0;") |
| conn.commit() |
| except sqlite3.OperationalError: |
| pass |
| |
| cursor.execute(""" |
| SELECT i.id, i.publish_record_id, i.user_name, i.content, i.intent_score, i.intent_type, p.account_id |
| FROM interaction_record i |
| LEFT JOIN publish_record p ON i.publish_record_id = p.id |
| WHERE i.is_high_intent = 1 AND i.lead_processed = 0 |
| """) |
| high_intent_interactions = cursor.fetchall() |
| |
| if not high_intent_interactions: |
| print("No new high intent interactions found.") |
| conn.close() |
| return |
| |
| lead_count = 0 |
| reply_count = 0 |
| for interaction_id, pub_rec_id, user_name, content, intent_score, intent_type, account_id in high_intent_interactions: |
| |
| reply_message = "您好!感谢您的关注,咨询详情请添加微信:SpiderXHS_Official,备注:小红书咨询" |
| |
| success = await agentic_reply(user_name, reply_message, account_id or 1) |
| if success: |
| reply_count += 1 |
| |
| |
| cursor.execute(""" |
| INSERT INTO lead (interaction_id, contact_info, status, created_at) |
| VALUES (?, ?, ?, datetime('now')) |
| """, (interaction_id, f"User: {user_name}, Intent: {intent_type}, Score: {intent_score}", "new")) |
| |
| lead_count += 1 |
| |
| |
| cursor.execute("UPDATE interaction_record SET lead_processed = 1 WHERE id = ?", (interaction_id,)) |
| |
| conn.commit() |
| print(f"Successfully generated {lead_count} new leads and replied to {reply_count} users.") |
| conn.close() |
|
|
| def main(): |
| asyncio.run(main_async()) |
|
|
| if __name__ == "__main__": |
| main() |
|
|