How to Manage Automated WhatsApp Bots for Multiple Tenants with Unique Phone Numbers in a Multi-Tenant Application?
I have a multi-tenant application where each tenant (client) can manage their products and interact with their customers through WhatsApp. Here’s my setup and the problem I’m facing:
- I have 70 tenants in my application, and each tenant needs their own WhatsApp bot to communicate with customers.
- Each bot requires a unique phone number to function.
- I am using the WhatsApp Business Cloud API and have an account named XYZ.
- I was able to add two phone numbers to the XYZ account, but I encountered a limitation when trying to add more numbers.
- Each bot needs to send and receive messages independently, and I also need to process webhook events for each bot separately to handle tenant-specific logic.
Questions:
- How can I support 70 tenants, each with a unique phone number and WhatsApp bot, using the WhatsApp Business Cloud API?
- Is there a way to overcome the limitation of adding multiple phone numbers to a single WhatsApp Business Account?
- How can I set up and handle webhook events efficiently for each tenant in a multi-tenant architecture?
What I’ve Tried:
- Adding multiple phone numbers to the XYZ account but encountered a limit after two numbers.
- Researching WhatsApp’s documentation and community forums but couldn’t find a clear solution for this type of multi-tenant setup.
What I Need:
I’m looking for a scalable solution that:
- Allows each tenant to have their own bot and unique phone number.
- Ensures webhook events are processed correctly for each tenant, considering their specific logic.
Your question has two distinct parts, and the second one is more tricky/complex as it depends on the architecture you want to implement. Please provide more design info, so we can assist you better.
Is there a way to overcome the limitation of adding multiple phone numbers to a single WhatsApp Business Account?
Yes, you need to Verify Your Business. Once verified, you can add more business numbers. Follow Meta's instructions here: Meta Business Verification
How can I support 70 tenants, each with a unique phone number and WhatsApp bot, using the WhatsApp Business Cloud API?
This depends on the architecture and programming language you’re using. Here’s a scalable approach to handle this scenario in a multi-tenant setup:
Pub/Sub Architecture: Use a central queue to route messages, needed because whatsapp only accepts ONE WEBHOOK URL. and workers consume messages when they have available capacity. Redis Queues with python workers: Each tenant gets a queue and process these messages independently.Message Gateway:
import redis redis_client = redis.StrictRedis() def enqueue_message(message): tenant_id = message["metadata"]["phone_number_id"] # Unique for each WhatsApp number queue_name = f"queue:{tenant_id}" redis_client.rpush(queue_name, message)Worker:
import time def processor_1(tenant_id, message): print(f"First Processor {tenant_id}: {message}") def processor_2(tenant_id, message): print(f"Second Processor {tenant_id}: {message}") def worker(tenant_id): queue_name = f"queue:{tenant_id}" while True: message = redis_client.lpop(queue_name) if message: if tenant_id == "foo": processor_1(tenant_id, message) else: processor_2(tenant_id, message) else: time.sleep(1) # No messages, wait and retryThis is a simplified example. For a more comprehensive implementation (and I run in production), you can refer to this example:
Whatsapp Message Gateway + Multiple workers Example