0

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 ;)

Ahmed Zeini
  • 93
  • 1
  • 7
  • Welcome to [so]. This is not a code-writing or tutoring service. It is not possible to provide a specific answer without you providing sufficient information to understand your problem. Please see: [Why is Can someone help me? not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) for more details. – itprorh66 Jul 14 '23 at 23:20
  • @itprorh66 thanks for your feedback, you can check now if it's good. – Ahmed Zeini Jul 15 '23 at 01:00
  • It's entirely possible that Facebook has limited you as an anti-spam measure. – Tim Roberts Jul 15 '23 at 01:02
  • i don't think because rate limit is 80 messages per [second](https://developers.facebook.com/docs/whatsapp/cloud-api/overview/) – Ahmed Zeini Jul 15 '23 at 01:13
  • Log the response status after post, what happens when frozen? – Jemshit Jul 18 '23 at 09:55
  • @Jemshit `[2023-07-19 16:01:36,942: WARNING/ForkPoolWorker-9] 19 camp03 {"messaging_product":"whatsapp","contacts":[{"input":"535>[2023-07-19 16:01:36,942: WARNING/ForkPoolWorker-9] ok 200 [2023-07-19 16:01:36,957: WARNING/ForkPoolWorker-9] +11111111 [2023-07-19 16:01:36,957: WARNING/ForkPoolWorker-9] 200 [2023-07-19 16:01:36,957: WARNING/ForkPoolWorker-9] 19 camp03 {"messaging_product":"whatsapp","contacts":[{"input":"967>[2023-07-19 16:01:36,957: WARNING/ForkPoolWorker-9] ok 200` after that there is no response or output its hang – Ahmed Zeini Jul 19 '23 at 16:23
  • Try to understand what causes the freeze, even if api returns error, you should see it and aiohttp should timeout the request at worst case. Try to mock aiohttp post and it will probably still freeze, cause of bug might be sth related to number of requests & number of async tasks? – Jemshit Jul 20 '23 at 03:55
  • @Jemshit thanks so much, i found the issue it caused by [celery](https://docs.celeryq.dev/en/stable/) `time_limit` celery kill the task after 5 minutes – Ahmed Zeini Jul 20 '23 at 18:04
  • you also have limits on the number of messages you can send https://developers.facebook.com/docs/whatsapp/messaging-limits. But you should receive an error response back in that case instead of hanging. Glad you've found the issue already – Navjot Singh Aug 03 '23 at 07:10

0 Answers0