{"id":8152,"library":"fbmessenger","title":"fbmessenger","description":"fbmessenger is a Python library designed to simplify communication with the Facebook Messenger API. It provides a structured way to handle webhooks, send various message types (text, attachments, templates), and manage Facebook Page interactions. The current version is 6.0.0, with the last significant update in April 2019. While not actively developed with new features, it provides core functionality for Messenger bot development.","status":"maintenance","version":"6.0.0","language":"en","source_language":"en","source_url":"https://github.com/rehabstudio/fbmessenger","tags":["facebook","messenger","bot","api","webhook","flask"],"install":[{"cmd":"pip install fbmessenger","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used internally for making HTTP requests to the Facebook Messenger API.","package":"requests"}],"imports":[{"symbol":"BaseMessenger","correct":"from fbmessenger import BaseMessenger"},{"note":"MessengerClient is located in fbmessenger.client, not directly under fbmessenger.","wrong":"from fbmessenger import MessengerClient","symbol":"MessengerClient","correct":"from fbmessenger.client import MessengerClient"}],"quickstart":{"code":"import os\nfrom flask import Flask, request\nfrom fbmessenger import BaseMessenger, MessengerClient\n\napp = Flask(__name__)\napp.debug = True\n\n# Extend BaseMessenger and implement webhook handlers\nclass Messenger(BaseMessenger):\n    def __init__(self, page_access_token, app_secret=None):\n        self.page_access_token = page_access_token\n        self.app_secret = app_secret\n        self.client = MessengerClient(self.page_access_token, app_secret=self.app_secret)\n\n    def message(self, message):\n        # Handle incoming text messages\n        if 'text' in message['message']:\n            received_text = message['message']['text']\n            sender_id = message['sender']['id']\n            self.client.send({'text': f'Received: {received_text}'}, 'RESPONSE', recipient_id=sender_id)\n\n    def delivery(self, message):\n        pass # Handle message delivery confirmations\n\n    def read(self, message):\n        pass # Handle message read confirmations\n\n    def account_linking(self, message):\n        pass # Handle account linking events\n\n    def postback(self, message):\n        pass # Handle postback events\n\n    def optin(self, message):\n        pass # Handle opt-in events\n\n# Initialize Messenger with tokens from environment variables\nmessenger = Messenger(\n    os.environ.get('FB_PAGE_TOKEN', ''),\n    app_secret=os.environ.get('FB_APP_SECRET', '')\n)\n\n@app.route('/webhook', methods=['GET', 'POST'])\ndef webhook():\n    if request.method == 'GET':\n        # Webhook verification\n        if request.args.get('hub.verify_token') == os.environ.get('FB_VERIFY_TOKEN'):\n            return request.args.get('hub.challenge')\n        raise ValueError('FB_VERIFY_TOKEN does not match.')\n    elif request.method == 'POST':\n        # Handle incoming messages\n        messenger.handle(request.get_json(force=True))\n        return ''\n\nif __name__ == '__main__':\n    # Remember to set FB_PAGE_TOKEN, FB_VERIFY_TOKEN, and FB_APP_SECRET (optional) environment variables.\n    # Example: export FB_PAGE_TOKEN='your_page_access_token'\n    #          export FB_VERIFY_TOKEN='your_verify_token_string'\n    #          export FB_APP_SECRET='your_app_secret' (if enabled)\n    app.run(host='0.0.0.0', port=5000)\n","lang":"python","description":"This Flask example demonstrates how to set up a basic webhook for the Facebook Messenger API using fbmessenger. It listens for incoming messages, verifies the webhook, and echoes back received text messages. Ensure you have Flask installed (`pip install Flask`) and configured environment variables for `FB_PAGE_TOKEN`, `FB_VERIFY_TOKEN`, and optionally `FB_APP_SECRET` based on your Facebook App setup."},"warnings":[{"fix":"Update your method calls to pass 'recipient_id' explicitly instead of 'message' object for identifying the recipient.","message":"Version 6.0.0 introduced a breaking change by switching the input parameter from 'message' to 'recipient_id' for methods like send().","severity":"breaking","affected_versions":"6.0.0 and above"},{"fix":"Ensure that the user IDs you receive from Messenger webhooks are stored and used as `recipient_id` for outgoing messages.","message":"Facebook Page-Scoped IDs (PSIDs) must be used when interacting with the Messenger API. These are unique to your page and differ from global Facebook user IDs or app-scoped IDs from other integrations. Using incorrect IDs will result in failed API calls.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly consult the official Meta Messenger Platform documentation for any breaking changes or new requirements. This library might not receive updates to match every new API version.","message":"The Facebook Messenger API frequently updates, sometimes introducing changes to message types, permissions, or security features (e.g., end-to-end encryption by default). While fbmessenger aims to abstract this, underlying API changes can affect compatibility or require updates to your bot's logic.","severity":"gotcha","affected_versions":"All versions, due to external API changes"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Double-check that the `FB_VERIFY_TOKEN` environment variable in your application exactly matches the 'Verify Token' string you entered in your Facebook App's Messenger Platform settings. Pay attention to case sensitivity and leading/trailing spaces.","cause":"The `hub.verify_token` sent by Facebook during webhook setup does not match the `FB_VERIFY_TOKEN` configured in your application's environment or code.","error":"ValueError: FB_VERIFY_TOKEN does not match."},{"fix":"Verify your server's internet connectivity and firewall rules. Implement retry logic with exponential backoff for API calls. Check Facebook's developer status page for any ongoing API issues. Consider increasing the `timeout` parameter for `MessengerClient` methods if connections are slow.","cause":"The application cannot establish a stable network connection to Facebook's API endpoints, or the API request timed out. This can be due to local network issues, firewalls, or temporary Facebook server problems.","error":"API calls failing with connection errors (e.g., requests.exceptions.ConnectionError, timeout errors)."},{"fix":"Regenerate your Page Access Token in the Facebook Developer Portal. Ensure your Facebook App has all necessary permissions (e.g., `pages_messaging`). Confirm that `recipient_id` values are valid Page-Scoped IDs obtained from incoming webhook events.","cause":"This often indicates issues with the Page Access Token (expired, incorrect), insufficient permissions granted to your Facebook App, or using an invalid `recipient_id` (e.g., not a Page-Scoped ID, or a user who has not interacted with the page).","error":"Messages are sent but not received by users, or messages fail with unexpected errors related to permissions or recipient_id."}]}