缺少 Facebook Messenger 一次性通知 (OTN) 请求的回发响应

问题描述 投票:0回答:1

我目前正在 Facebook Messenger 上构建一个聊天机器人,并正在努力实现“一次性通知”流程。

我可以成功询问用户是否愿意接收通知,但我的 webhook 上从未收到任何 pingback。我确保我的 webhook 已订阅 optin 事件,但仍然没有任何结果。我还确保我的页面可以访问一次性通知功能

我尝试检查我的 Facebook 帐户的所有权限,认为这一定是 facebook 端错误,因为没有将响应发送到 webhook,但我无法弄清楚。我也尝试过多次从头开始实现这个,看看我是否只是错过了某个步骤但没有改变。

页面权限 Webhook 订阅

这是我收到请求的方式:

def handle_message(self, request):
    data = request.get_json()
    if data['object'] == "page":
        for entry in data['entry']:
            for messaging_event in entry['messaging']:
                sender_id = messaging_event['sender']['id']
                
                if messaging_event.get('optin'):
                    print('Optin event recieved!!!')
                    token = messaging_event['optin']['one_time_notif_token']

                # Check for postback and handle it
                elif 'postback' in messaging_event:
                    payload = messaging_event['postback']['payload']
                    self.handle_payload(sender_id, payload)
                # Check for quick_reply and handle it
                elif 'message' in messaging_event and 'quick_reply' in messaging_event['message']:
                    payload = messaging_event['message']['quick_reply']['payload']
                    self.handle_payload(sender_id, payload)
                # Handle regular text messages
                elif 'message' in messaging_event:
                    message_text = messaging_event['message'].get('text', '')
                    print("Message from {}: {}".format(sender_id, message_text))
                    response_text = self.chatgpt.get_chat_response(message_text)
                    self.send_message(sender_id, response_text)
    return jsonify({'success': True}), 200

这是我如何发送请求的代码:

def send_otn_request(recipient_id, page_access_token):
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "recipient": {
            "id": recipient_id
        },
        "message": {
            "attachment": {
                "type": "template",
                "payload": {
                    "template_type": "one_time_notif_req",
                    "title": "Want us to remind you of your appointment?",
                    "payload": "YES_REMINDER"  # Payload to track the type of notification
                }
            }
        }
    }
    print(f"Sending OTN request to recipient: {recipient_id}")
    print(f"Request headers: {headers}")
    print(f"Request data: {data}")

    response = requests.post(
        f'https://graph.facebook.com/v14.0/me/messages?access_token={page_access_token}',
        headers=headers,
        json=data
    )
    print(f"OTN request response: {response.status_code}, {response.text}")
    
    # Check for errors in the response
    if response.status_code != 200:
        print(f"Error sending OTN request: {response.text}")
    else:
        print("OTN request sent successfully!")

    return response.json()
python facebook-messenger facebook-messenger-bot
1个回答
0
投票

为下一个遇到此问题的人更新此内容 - 从开发模式切换到实时后,OTN 回发已成功发送到 webhook。

不适合测试,希望 Facebook 尽快解决这个问题。

我的用例是用于预约通知,因此解决此问题后,我发现 CONFIRMED_EVENT_UPDATE 标签更适合。

© www.soinside.com 2019 - 2024. All rights reserved.