Skip to main content

Python Wrapper for Facebook Messenger Platform.

Disclaimer: This wrapper is NOT an official wrapper and do not attempt to represent Facebook in anyway.

About

This wrapper has the following functions:

  • send_text_message(recipient_id, message)

  • send_message(recipient_id, message)

  • send_generic_message(recipient_id, elements)

  • send_button_message(recipient_id, text, buttons)

  • send_quick_reply(recipient_id, text, buttons)

  • send_attachment(recipient_id, attachment_type, attachment_path)

  • send_attachment_url(recipient_id, attachment_type, attachment_url)

  • send_image(recipient_id, image_path)

  • send_image_url(recipient_id, image_url)

  • send_audio(recipient_id, audio_path)

  • send_audio_url(recipient_id, audio_url)

  • send_video(recipient_id, video_path)

  • send_video_url(recipient_id, video_url)

  • send_file(recipient_id, file_path)

  • send_file_url(recipient_id, file_url)

  • send_action(recipient_id, action)

  • send_raw(payload)

  • get_user_info(recipient_id)

You can see the code/documentation for there in bot.py.

The functions return the full JSON body of the actual API call to Facebook.

Register for an Access Token

You’ll need to setup a Facebook App, Facebook Page, get the Page Access Token and link the App to the Page before you can really start to use the Send/Receive service.

This quickstart guide should help

Installation

pip install pymessenger2

Usage

from pymessenger2.bot import Bot

bot = Bot(<access_token>, [optional: app_secret])
bot.send_text_message(recipient_id, message)

Note: From Facebook regarding User IDs

These ids are page-scoped. These ids differ from those returned from Facebook Login apps which are app-scoped. You must use ids retrieved from a Messenger integration for this page in order to function properly.

If app_secret is initialized, an app_secret_proof will be generated and send with every request. Appsecret Proofs helps further secure your client access tokens. You can find out more on the Facebook Docs

Sending a generic template message:

Generic Template Messages allows you to add cool elements like images, text all in a single bubble.

from pymessenger2.bot import Bot
from pymessenger2 import Element
bot = Bot(<access_token>)
elements = []
element = Element(title="test", image_url="<arsenal_logo.png>", subtitle="subtitle", item_url="http://arsenal.com")
elements.append(element)

bot.send_generic_message(recipient_id, elements)

Output:

Generic Bot Output

Generic Bot Output

Sending an image/video/file using an URL:

from pymessenger2.bot import Bot
bot = Bot(<access_token>)
image_url = "http://url/to/image.png"
bot.send_image_url(recipient_id, image_url)

Todo

  • Structured Messages

  • Receipt Messages

  • Airlines

  • Tests!

Example

Screenshot of Echo Facebook Bot

Screenshot of Echo Facebook Bot

You can find an example of an Echo Facebook Bot in examples/

Release files for pymessenger2 3.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymessenger2 3.1.0
File Size Uploaded
pymessenger2-3.1.0.tar.gz 8.8 kB Details

Release files / pymessenger2-3.1.0.tar.gz

Download URL pymessenger2-3.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6964fa6d95cdcb6be4f54209e5b3270a79502ec5cdb27ba67673b3ae28343b72
BLAKE2b-256 checksum
How to use checksums
b8063d31df36881475dbf8001a1bc690c76dffc7c11575bdde22595ad29e7784
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

3.1.0 This release

1 release file

3.0.5

1 release file

3.0.3

1 release file

3.0.0

1 release file

2.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page