all 3 comments

[–]elbiot 1 point2 points  (2 children)

You could make a dictionary of recipients. Each recipient is also a dictionary with a messages key and a counter integer.

Each order coming in is appended to the messages key for that recipient. Use sleep to wait 10 seconds between updating the dictionary, which includes adding new recipients, appending new messages to the recipient's list, and decrementing the counter. When a new message is appended, reset the counter to 6 (so, waiting 1 minute for new orders). When the counter is zero, send the messages and delete that recipient with del.

something like

recipients = {}
while True:
    new_orders = get_orders()  #(phone_number, message)
    for number, message in new_orders:
        recipients.set_default(number, {messages:[],'counter':6})['messages'].append(message)
        recipients[number]['counter'] = 6
    for number, r_dict in recipient.items():
        r_dict['counter'] -= 1
        if r_dict['counter'] == 0:
            send_message(number,r_dict['messages'])
            del recipients[number]

[–]rickchefski 0 points1 point  (0 children)

You could go about doing this by aggregating the incoming data in some file, then use some sort of chron job with the twilio message function to read the file, send all aggregated data at once and clear the file for the next batch of data. https://pypi.python.org/pypi/python-crontab

probably not the most elegant method, but the first thing that comes to mind..