WhatsApp Chatbot Session Flow
Overview
This package provides a Django-based session management system for building a WhatsApp chatbot. It enables:
- Dynamic page-based navigation
- Session tracking
- Dynamic method execution for custom tasks (e.g., adding to cart)
- Middleware support
- Admin integration
Installation
Install the package via pip:
pip install chatbot_session_flow
Environment Variables
Set the following environment variables in your shell or a .env file:
export pub_key=""
export secret=""
export SECRET_KEY="xx56r_@x_jb8(rqiw5+xw^+@l#m(=%9h+!98k1o$ex3_zogx"
export MODE=dev
export DEBUG=True
export DB_NAME=<dbname>
export DB_USER=<db connection user>
export DB_PASSWORD=<database password>
export DB_HOST="<database connection host>"
export DB_PORT=<database running port>
export NGUMZO_API_KEY=<your api key if using ngumzo APIs>
export DYNAMIC_METHOD_VIEWS="<your_app.dynamic_methods>"
Django Settings
Add it to your INSTALLED_APPS:
INSTALLED_APPS = [
...,
'chatbot_session_flow',
]
Add middleware to enable session handling:
MIDDLEWARE = [
...,
'chatbot_session_flow.middleware.WhatsappSessionMiddleware',
]
Migrations
Run the migrations:
python manage.py makemigrations chatbot_session_flow
python manage.py migrate chatbot_session_flow
Internal Logic
The chatbot flow is managed through a sequence of Page instances. Each page can reference the next, allowing dynamic navigation.
Sessions are tied to users via the Session and Stepper models.
Dynamic Methods
Custom tasks such as saving data, calling APIs, or formatting messages are handled by dynamic methods specified in a dynamic file defined by DYNAMIC_METHOD_VIEWS. These include:
sendWhatsappMessage(session, phoneNumber, message)– your logic to send messages.formatRequest(request)– used by middleware to shape incoming data.- Any task-specific functions (e.g.,
add_to_cart) that can be dynamically executed.
Dynamic Method Example
# your_app/dynamic_methods.py
def add_to_cart(session, page, message, *args, **kwargs):
print(f"Adding product to cart for user {session.user.phoneNumber}")
return "Item added to cart"
Call it dynamically:
from chatbot_session_flow.views import perform_dynamic_action
response = perform_dynamic_action(session, page, "Item name")
Dynamic Message Templating
Use collected data within page content dynamically:
Hello {firstName}, your order has been placed!
The values in {} are replaced by corresponding session or user data.
Models
Profile
Handles user identity:
from chatbot_session_flow.models import Profile
profile = Profile.objects.create(
firstName="John",
lastName="Doe",
phoneNumber="+1234567890",
email="john@example.com"
)
Session
Tracks the user’s chatbot session:
session = Session.objects.create(user=profile, isValidSession=True)
print(session.is_session_expired())
Page
Each page drives a single step in the conversation:
page = Page.objects.create(title="Welcome", pageType="text", content="Hello {firstName}")
Common Issues & Fixes
No Migrations Detected
- Ensure your environment is correct.
- Use
pip install -e .if developing locally. - Run:
python manage.py makemigrations chatbot_session_flow python manage.py migrate
Dynamic Methods Not Found or Not Executing
- Confirm the file path in
DYNAMIC_METHOD_VIEWSis correct. - Check that the function name matches the
taskattribute in yourPage. - Ensure the function signature accepts dynamic args/kwargs:
def custom_task(session, page, message, *args, **kwargs): ...
known Issues
- As of yet there are no knows issues but you are encouragedd to test and create github issues for any issues faced in your development.
- Feel free to reachout To me via email
kamadennis05@gmail.com
Conclusion
This package offers a powerful framework for building WhatsApp-based conversational flows with dynamic logic and session handling. Feel free to contribute or extend the package to fit your business logic!
Metadata
Release files for chatbot-session-flow 0.1.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| chatbot_session_flow-0.1.13.tar.gz | 11.4 kB | Without provenance | Details for chatbot_session_flow-0.1.13.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| chatbot_session_flow-0.1.13-py3-none-any.whl | Python 3 | none | any | Details for chatbot_session_flow-0.1.13-py3-none-any.whl |
Total release size: 23.3 kB
Release files / chatbot_session_flow-0.1.13.tar.gz
| Download URL | chatbot_session_flow-0.1.13.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
657f409f5636386ca0bb94afaf05e9b391f7f7fb957a6a632bbef0842d18215b
|
|
BLAKE2b-256 checksum How to use checksums |
a4959a0e56f4ac0841c97247df6745f5de3d3cac4e89486d840af42425bd45fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.13
|
Release files / chatbot_session_flow-0.1.13-py3-none-any.whl
| Download URL | chatbot_session_flow-0.1.13-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e580f154c4294cb8337b53fa1e479e39c9faf45f57247d08d9ecf4cf24e66d4
|
|
BLAKE2b-256 checksum How to use checksums |
d6d2872938e588f93cc74d1018058e967179fec9f5bc6dfa5e9de68968e75140
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.13
|
Security for chatbot-session-flow 0.1.13
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.