Skip to main content

A modern, easy to use, feature-rich ready API wrapper for WhatsApp Business Cloud written in Python

Project description

wa_me

A modern, easy to use, feature-rich ready API wrapper for WhatsApp Business Cloud written in Python.

Features

Installing

Stable release

To install wa_me, run this command in your terminal:

$ pip install wa_me

This is the preferred method to install wa_me, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for wa_me can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/leandcesar/wa_me

Or download the tarball:

$ curl -OJL https://github.com/leandcesar/wa_me/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Quick Example

Mirror Bot

from wa_me import Bot, Ctx

class MirrorBot(Bot):
    def before_event_message(self, ctx: Ctx):
        ctx.read()

    def on_event_message_audio(self, ctx: Ctx):
        ctx.send_audio(ctx.message.audio.id)

    def on_event_message_document(self, ctx: Ctx):
        ctx.send_document(
            ctx.message.document.id,
            caption=ctx.message.document.caption,
        )

    def on_event_message_image(self, ctx: Ctx):
        ctx.send_image(
            ctx.message.image.id,
            caption=ctx.message.image.caption,
        )

    def on_event_message_location(self, ctx: Ctx):
        ctx.send_location(
            latitude=ctx.message.location.latitude,
            longitude=ctx.message.location.longitude,
            address=ctx.message.location.address,
            name=ctx.message.location.name,
        )

    def on_event_message_sticker(self, ctx: Ctx):
        ctx.send_sticker(ctx.message.sticker.id)

    def on_event_message_text(self, ctx: Ctx):
        ctx.send_text(ctx.message.text.body)

    def on_event_message_video(self, ctx: Ctx):
        ctx.send_video(
            ctx.message.video.id,
            caption=ctx.message.video.caption,
        )

Run using Flask

from flask import Flask, request
from wa_me import Bot

app = Flask(__name__)
bot = Bot()
bot.start(phone_id="PHONE_ID", token="ACCESS_TOKEN")

@app.get("/")
async def ping():
    if request.args.get("hub.verify_token") == "VERIFY_TOKEN":
        return request.args.get("hub.challenge")
    return "Invalid verify token"

@app.post("/")
def root():
    data = request.get_json()
    bot.handle(data)
    return "Success"

Run using Fast API

from fastapi import FastAPI, Request
from wa_me import Bot

app = FastAPI()
bot = Bot()
bot.start(phone_id="PHONE_ID", token="ACCESS_TOKEN")

@app.get("/")
async def ping(
    token: str = Query(alias="hub.verify_token"),
    challenge: str = Query(alias="hub.challenge"),
):
    if token == VERIFY_TOKEN:
        return challenge
    return "Invalid verify token"

@app.post("/")
async def root(request: Request):
    data = await request.json()
    bot.handle(data)
    return "Success"

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2022-12-25)

  • First release on PyPI.

  • Implement data classes and converters with dacite.

  • Implement HTTP and Client.

  • Implement Bot and Ctx.

  • Implement TTLDict.

  • Implement Routine.

  • Add docs.

  • Add examples.

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

wa_me-0.1.0.tar.gz (71.0 kB view hashes)

Uploaded Source

Built Distribution

wa_me-0.1.0-py2.py3-none-any.whl (3.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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