0

I implemented MessageBird WhatsApp Integration. but my problem is how to build a logic where I can calculate the remaining sms limit and reset it after 24hrs.

  1. total limit is 1000 per day in 24 hrs

  2. if I send 500 sms at 2 pm and the remaining 500 at 5 pm, so next day at 2 pm I will get 500 limits and the remaining 500 at 5pm.

can someone help me to build this logic so that the API can send the accurate remaing limit for sending sms for that time period.

Apoorva
  • 11

1 Answers1

0

It's really hard to answer to your question without further details about your project's architecture.

Assuming you have means to persist and query your data, you could simply store an entity for each message sent by a user along with a created_at timestamp, eg.:

+-----+------------+---------+
| id  | created_at | user_id |
+-----+------------+---------+
|   1 |        ... |      25 |
|   2 |        ... |      25 |
+-----+------------+---------+

Then you could easily query this data and verify the 1000 message limit against the total count of records found for the given user_id and created_at >= now() - 24h

Domenico Sibilio
  • 1,189
  • 1
  • 7
  • 20
  • Thank you for your response. Below is a complete end to end flow of my project 1. we group mobile numbers and send WhatsApp messages to that group. 2. and in a day we can send only 1000 messages, due to WhatsApp limit. 3. now from the front-end if somebody sends 500 messages at 10 pm, and 800 messages at 6 pm it should send back a message that you have only 500 limit remaining. 4. the above scenario can be maintained but how to calculate the remaining count and reset limit after 24 hrs – Apoorva Feb 12 '21 at 18:10
  • As per my answer, assuming you can persist an entity for each message that is sent, I don't see the problem with calculating the "remaining count", which would also take care of "resetting the limit". count(*) of messages where created_at >= now() - 24h = your message count over 24h at any given time. If you send many messages in a batch, rather than storing each message as a single record, you can add a "quantity" field containing the number of messages sent per record. Then you perform the same query as specified earlier, but this time with the sum of "quantity" rather than count of records. – Domenico Sibilio Feb 12 '21 at 18:44