Skip to main content

Open source python wrapper to Messenger API

Project description

banner Pynani

Opensource python wrapper to Messenger API

Features

API

  • Verify the webhook
  • Send text messages
  • Send attachments from a remote file (image, audio, video, file)
  • Send attachments from a local file (image, audio, video, file)
  • Send templates (generic, buttons, media, receipent)
  • Send quick replies

Other functionalities

  • Get sender id
  • Get type of message received
  • Get text of the message received
  • Get the url of the attachment received
  • Get type of the attachment received
  • Download attachments received

Installation

Install Pynani with pip

  pip install pynani

Or install with pipenv (requires pipenv installed)

  pipenv install pynani

Getting started

Prerequisites

  • Python 3.8+ installed
  • To get started using this module, you will need page access token which you can get from the Facebook Developer Portal

A simple echo bot

The Messenger class (defined in Messenger.py) encapsulates all API calls in a single class. It provides functions such as send_xyz (send_message, send_attachment etc.) and several ways to listen for incoming messages.

Create a file called echo_bot.py. Then, open the file and create an instance of the Messenger class.

from pynani import Messenger

PAGE_ACCESS_TOKEN = 'EAAxxxxxxx...'
mess = Messenger(PAGE_ACCESS_TOKEN)

[!IMPORTANT] Make sure to actually replace PAGE_ACCESS_TOKEN with your own page access token.

After that declaration, we need to register some message handlers. First, we need to create and verify a webhook with the help of Flask or FastAPI.

from flask import Flask, request, jsonify

app = Flask(__name__)

TOKEN = "abc123"

@app.get("/")
def meta_verify():
    return mess.verify_token(request.args, TOKEN)

Now let's define a webhook that handles certain messages

@app.post("/")
def meta_webhook():
    data = request.get_json()
    sender_id = mess.get_sender_id(data)
    message = mess.get_message_text(data)
    if message == "Hello":
        mess.send_text_message(sender_id, "Hello, World!")
    if message == "Bye":
        mess.send_text_message(sender_id, "Nice to meet you! 👍🏽")

    return jsonify({"status": "success"}), 200

We now have a basic bot which replies a static message to "hello" and "bye" messages. To start the bot, add the following to our source file:

if __name__ =='__main__':
    app.run(port=8080, debug=True)

Alright, that's it! Our source file now looks like this:

from flask import Flask, request, jsonify
from pynani import Messenger

PAGE_ACCESS_TOKEN = 'EAAxxxxxxx...'
TOKEN = "abc123"

mess = Messenger(PAGE_ACCESS_TOKEN)
app = Flask(__name__)

@app.get("/")
def meta_verify():
    return mess.verify_token(request.args, TOKEN)

@app.post("/")
def meta_webhook():
    data = request.get_json()
    sender_id = mess.get_sender_id(data)
    message = mess.get_message_text(data)
    if message == "Hello":
        mess.send_text_message(sender_id, "Hello, World!")
    if message == "Bye":
        mess.send_text_message(sender_id, "Nice to meet you! 👍🏽")

    return jsonify({"status": "success"}), 200

if __name__ =='__main__':
    app.run(port=8080, debug=True)

To start the bot, simply open up a terminal and enter python echo_bot.py to run the bot! Test it by sending messages ("hello" and "bye").

Related

Here are some related projects that I was inspired by them.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynani-1.4.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynani-1.4.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pynani-1.4.1.tar.gz.

File metadata

  • Download URL: pynani-1.4.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pynani-1.4.1.tar.gz
Algorithm Hash digest
SHA256 484418a1f6215d7417a619ddeaf86abb53c754666865fe4667d2f80b5103527f
MD5 efb67e81905152e890d6d70e37012fd7
BLAKE2b-256 23d223fce6243fc5aa958ef50ad3846ddeb22eeaf9fd35804ea9a63458b7d0b6

See more details on using hashes here.

File details

Details for the file pynani-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: pynani-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pynani-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4571d2fb278ed10eb79798738a7a99d04a69f2f0350248dbd6a21549c4910a4
MD5 9e65ea3d5f4c68565c6dcdd8a3b0c10b
BLAKE2b-256 069a48fa67dc88e02eaa1353eaf36675d5d15217f3dee78866a3ac41a671f66b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page