Facebook Graph API: Send message from page

892 Views Asked by At

I have been trying to send an automated message from my Facebook page to an open conversation. I have been using Graph API's /me/messages endpoint using Page Access Token, but it doesn't seem to work.

Following is the code

def sendMessage(to_id, msg):
    print(f"Sending message to {to_id}")

    url = f"https://graph.facebook.com/v13.0/me/messages?access_token={page_access_token}"
    payload = {
        "messaging_type": "RESPONSE",
        "recipient": {
            "id": to_id
        },
        "message":{
            "text": msg
        }
    }

    res = requests.post(url, payload)

    print(res.content)

After calling this function, when I print the content of the response, it shows this:

b'{"error":{"message":"(#100) param recipient must be non-empty.","type":"OAuthException","code":100,"fbtrace_id":"A4QA9xqFj9Uch12oi6KRPQb"}}'
1

There are 1 best solutions below

1
Kent Dong On

The error message is quite straightforward.

I think you should check again your param: to_id, it could be empty or undefined/null in your case. Further check msg if needed.