WhatsApp chatbots with python

Get started with pywce.

Make sure to read core section before proceeding

Pywce is a library for creating WhatsApp chatbots using a template-driven approach. It decouples the engine from the WhatsApp client library, allowing developers to use them independently or together.

You can use pywce as just another WhatsApp client library without relying on the template engine.

Setup

  • Install library

    Install latest version from vcs

    # async version, using await
    $ pip install git+https://github.com/DonnC/pywce.git@async
    
    # sync version
    $ pip install git+https://github.com/DonnC/pywce.git@sync
    
  • Setup config keys

    Safely store your WhatsApp account config keys in a safe .env file for best practise

      ACCESS_TOKEN=your-whatsapp-access-token
      PHONE_NUMBER_ID=your-phone-id
      APP_SECRET=your-dev-app-secret
      WEBHOOK_HUB_TOKEN=your-preffered-webhook-token
    
      START_STAGE=your-selected-initial-template-stage
    
      # optional, used if using file based template source e.g YAML or JSON
      TEMPLATES_DIR=folder-with-template-files
      TRIGGERS_DIR=your-trigger-folder
    
  • Configure engine settings

    Examples given will be using FastApi, but any framework will do

    $ pip install fastapi[standard]
    

    Create client & engine instance

    # config.py
    import os
    from dotenv import load_dotenv
    
    from pywce import Engine, client, storage, EngineConfig
    
    load_dotenv()
    
    yaml_template_storage = storage.YamlStorageManager(os.getenv("TEMPLATES_DIR"), os.getenv("TRIGGERS_DIR"))
    
    _wa_config = client.WhatsAppConfig(
        token=os.getenv("ACCESS_TOKEN"),
        phone_number_id=os.getenv("PHONE_NUMBER_ID"),
        hub_verification_token=os.getenv("WEBHOOK_HUB_TOKEN"),
        app_secret=os.getenv("APP_SECRET")
    )
    
    whatsapp = client.WhatsApp(_wa_config)
    
    _eng_config = EngineConfig(
        whatsapp=whatsapp,
        storage_manager=yaml_template_storage,
        start_template_stage=os.getenv("START_STAGE")
    )
    
    engine = Engine(config=_eng_config)