Search code examples
pythonflaskfacebook-messengerfacebook-messenger-bot

Python keeps KeyError: 'message' when fetch data from Messenger Callback - Python/flask


I've tried to build a simple bot on Messenger that echoing back whenever we send a message to it.

The message is sent successfully so far but in the log, it keeps displaying error:

message_id = data['entry'][0]['messaging'][0]['message']['mid']
KeyError: 'message'

I have no idea why it happen, this is the logic operations:

import requests
import traceback
from flask import request

from ...config.default import VERIFY_TOKEN, ACCESS_TOKEN

ROOT_URL = "https://graph.facebook.com/v2.6/me/messages?access_token="

def reply(user_id, page_id, msg):
    data = {
        "sender": {"id": page_id},
        "recipient": {"id": user_id},
        "message": {"text": msg}
    }
    requests.post(ROOT_URL + ACCESS_TOKEN, json=data)


def verification_handler():
    if request.args['hub.verify_token'] == VERIFY_TOKEN:
        return request.args['hub.challenge'], 200
    return "Invalid verification token"


def handle_incoming_messages(data):
    try:
        # Get all data from Messenger callback - Text
        recipient_id = data['entry'][0]['messaging'][0]['recipient']['id']
        sender_id = data['entry'][0]['messaging'][0]['sender']['id']
        timestamp = data['entry'][0]['messaging'][0]['timestamp']

        message_id = data['entry'][0]['messaging'][0]['message']['mid']
        text = data['entry'][0]['messaging'][0]['message']['text']

        reply(sender_id, recipient_id, text)
    except KeyError:
        print(traceback.format_exc())

    return ''

this is the routes:

from flask import Blueprint, request

from .ops import verification_handler, handle_incoming_messages


mod = Blueprint('messenger', __name__)


# Route: /messenger/
@mod.route('/', methods=['GET'])
def verify():
    """Facebook will GET request to this endpoint for verification."""
    return verification_handler()


# Route: /messenger/
@mod.route('/', methods=['POST'])
def handle():
    data = request.json
    handle_incoming_messages(data)

    return ''


# Route: /messenger/hello
@mod.route('/hello')
def hello():
    return 'hello there'

Solution

  • I do this to extract the info

    if keys_exist(event, ['body']):
                event_entry=json.loads(event['body'])
                if ((len(event_entry['entry'])>0) & (keys_exist(event_entry['entry'][0],['messaging'])) ):
                    messaging_event = event_entry['entry'][0]['messaging'][0]
                    if (keys_exist(messaging_event,['message'])):
                        msg_txt   = messaging_event['message']['text']
                        sender_id = messaging_event['sender']['id']