I have 100k whatsapp numbers and i try to send message for each number.
I have two server one to send messages and the other one to receive whatsapp webhook notification.
The issue that the webhook server some time can take time to response to webhooks with status code 200.
Meanwhile the other server can send 30 to 40 message per second, but always when it reaches ~ 7k requests it hang and doesn't send any request anymore.
there is the code that i use to send messages (can send 30 to 40 messages per second):
async def send_msg(phone: str, session: ClientSession):
json = {
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": phone,
"type": 'template',
"template": {
"name": 'template_123',
"language": {
"code": 'ar'
},
"components": []
}
}
headers = {
'Authorization': f'Bearer {whatsapp_api_key}',
}
async with session.post(f'https://graph.facebook.com/v14.0/{campaign.user.phone_number_id}/messages', json=json,
headers=headers) as response:
result = await response.text()
print(phone)
print(response.status)
async def send_all(phones: list):
my_conn = aiohttp.TCPConnector(limit=10)
async with aiohttp.ClientSession(connector=my_conn, json_serialize=ujson.dumps) as session:
tasks = []
for phone in phones:
task = asyncio.ensure_future(send_msg(phone=phone, session=session))
tasks.append(task)
await asyncio.gather(*tasks, return_exceptions=True)
start = time.time()
asyncio.run(send_all(data))
end = time.time()
The problem that when it reaches ~ 7k requests sent, it hang or stopped the tcp connection, doesn't send anymore request. Did whatsapp hang my connection because there is no response for some webhooks ? or there is another solution.
Thanks for any help ;)