dify-whatsapp-1 | INFO: Started server process [68]dify-whatsapp-1 | INFO: Waiting for application startup.dify-whatsapp-1 | INFO: Application startup complete.
# Check if the number is enrolled if whatsapp_number not in enrolled_numbers: message = client.messages.create( from_=f"whatsapp:{twilio_number}", body="You are not enrolled in this service.", to=f"whatsapp:{whatsapp_number}" ) return ""
5.5.2 将WhatsApp号码作为Dify会话ID,确保用户持续保持该会话
Copy
Ask AI
conversation_ids = {}
对应
Copy
Ask AI
url = dify_url headers = { 'Content-Type': 'application/json', 'Authorization': f"Bearer {dify_api_key}", } data = { 'inputs': {}, 'query': Body, 'response_mode': 'streaming', 'conversation_id': conversation_ids.get(whatsapp_number, ''), 'user': whatsapp_number, } response = requests.post(url, headers=headers, data=json.dumps(data), stream=True) answer = [] for line in response.iter_lines(): if line: decoded_line = line.decode('utf-8') if decoded_line.startswith('data: '): decoded_line = decoded_line[6:] try: json_line = json.loads(decoded_line) if "conversation_id" in json_line: conversation_ids[whatsapp_number] = json_line["conversation_id"] if json_line["event"] == "agent_thought": answer.append(json_line["thought"]) except json.JSONDecodeError: print(json_line) continue merged_answer = ''.join(answer)