Skip to main content

A template-driven framework for building WhatsApp chatbots

Project description

Python WhatsApp ChatBot Engine

A framework for creating WhatsApp chatbots using a template-driven approach - allowing you to define conversation flows and business logic in a clean and modular way.

It decouples the engine from the WhatsApp client library, allowing developers to use them independently or together.

Features

  • Template-Driven Design: Use YAML templates for conversational flows.
  • Hooks for Business Logic: Attach Python functions to process messages or actions.
  • Easy-to-use API for WhatsApp Cloud.
  • Supports dynamic messages with placeholders.
  • Built-in support for WhatsApp Webhooks.

Installation

pip install pywce

Why pywce

Most WhatsApp chatbot tutorials or libraries just scraps the surface, only sending a few message or handling simple logic or are client libraries only.

This library gives you a full-blown framework for chatbots of any scale allowing you access to full package of whatsapp client library and chatbot development framework.

Example ChatBot

Here's a simple example template to get you started:

Note: Checkout complete example chatbot with Fast Api here

  1. Define YAML template (Conversation Flow💬):
# path/to/templates
"START-MENU":
  type: button
  template: "example.hooks.name_template.username"
  message:
    title: Welcome
    body: "Hi {{ name }}, I'm your assistant, click below to start!"
    footer: pywce
    buttons:
      - Start
  routes:
    "start": "NEXT-STEP"

"NEXT-STEP":
  type: text
  message: Great, lets get you started quickly. What is your age?
  routes:
    "re://d{1,}": "NEXT-STEP-FURTHER"
  1. Write your hook (Supercharge⚡):
# example/hooks/name_template.py
from pywce import hook, HookArg, TemplateDynamicBody

@hook
def username(arg: HookArg) -> HookArg:
    # set render payload data to match the required template dynamic var
    
    # greet user by their whatsapp name 😎
    arg.template_body = TemplateDynamicBody(
        render_template_payload={"name": arg.user.name}
    )

    return arg
  1. Start the engine:
from pywce import Engine, EngineConfig

config = EngineConfig(
    templates_dir="path/to/templates",
    start_template_stage="START-MENU"
)
engine = Engine(config=config)

WhatsApp Client Library

You can use pywce as a standalone whatsapp client library. See FastApi Example

PyWCE provides a simple, Pythonic interface to interact with the WhatsApp Cloud API:

  • Send messages (text, media, templates, interactive)
  • Receive and process webhooks
  • Media management (upload and download)
  • Out of the box utilities using the WhatsApp.Utils class.

Example usage:

from pywce import WhatsAppConfig, WhatsApp

config = WhatsAppConfig(
    token="your_access_token",
    phone_number_id="your_phone_number_id",
    hub_verification_token="your_webhook_hub_verification_token"
)

whatsapp = WhatsApp(whatsapp_config=config)

# Sending a text message
response = whatsapp.send_message(
    recipient_id="recipient_number",
    message="Hello from PyWCE!"
)

# verify if request was successful, using utils
is_sent = whatsapp.util.was_request_successful(
    recipient_id="recipient_number",
    response_data=response
)

if is_sent:
    message_id = whatsapp.util.get_response_message_id(response)
    print("Request successful with msg id: ", message_id)

Documentation

Visit the official documentation for a detailed guide.

Contributing

We welcome contributions! Please check out the Contributing Guide for details.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

pywce-1.0.2.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

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

pywce-1.0.2-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

Details for the file pywce-1.0.2.tar.gz.

File metadata

  • Download URL: pywce-1.0.2.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.9

File hashes

Hashes for pywce-1.0.2.tar.gz
Algorithm Hash digest
SHA256 dfa860c34009f0da47a9b70f22a0788e2d84749515252b8b408997b72bd33f2f
MD5 1586c05d2182fe359f338863d8797409
BLAKE2b-256 fef711ab5f7c9a0fde41e0a2102a8421b43a85b85ce34242fde3c3ffa3d11f53

See more details on using hashes here.

File details

Details for the file pywce-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pywce-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.9

File hashes

Hashes for pywce-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d083b246018158abf99d042f5b4317d27e304aceeb5317fbf5916da916af1519
MD5 f2357b95fa66faff6b9934b27d292039
BLAKE2b-256 3f6384c6ffea127d096f775d995ffe77c120326bbc8d0308c47033b7934ac290

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