I'm running a periodic task on Celery that executes the same code once every 3 minutes. If a condition is True, an action is performed (a message is sent), but I need that message to be sent only once.
The ideal would be that that message, if sent, it could not be sent in the next 24 hours (even though the function will keep being executed every 3 minutes), and after those 24 hours, the condition will be checked again and message sent again if still True. How can I accomplish that with Python? I put here some code:
if object.shussui:
client.conversation_start({
'channelId': 'x',
'to': 'user',
'type': 'text',
'content': {
'text': 'body message')
}
})
Here the condition is being checked, and if shussui
is True
the client.start_conversation
sends a message.
You could keep the last time the message was sent and check that 24 hours have passed:
from time import time
lastSent = None
if object.shussui and (not lastSent or (time() - lastSent) >= 24 * 60 * 60):
client.conversation_start({
'channelId': 'x',
'to': 'user',
'type': 'text',
'content': {
'text': 'body message')
}
})
lastSent = time()