pythoncelerypython-telegram-botpytransitions

Telebot + Celery + pytransitions: response to task


I want to send some message after time delay, using Celery. After users get message, its trigger new state. For this I need telebot.types.Message object to be send as argument in Celery task. How can I do this correctly?

My transition function to start Celery task:

 def delay_message(self, event):
        celery_utils.delay_message.apply_async(kwargs={'response': self.response}, countdown=1) # self.response is telebot.types.Message

Celery task:

@celery.task()
def delay_message(response):
    machine = routes.DialogMachine(transitions=app.config['transitions'])
    machine.response = response
    machine.send_random_motivation_message()

In send_random_motivation_message() I need telebot.types.Message as self.response, but can't send this type to Celery task.


Solution

  • I assume you can't send it because it is not serializable, right? If that is the case, your only option is to send as many parameters as needed as dictionary, or tuple, and create the telebot.types.Message inside the Celery task.

    You could try the jsonpickle to generate JSON out of the pickled telebot.types.Message object, pass it to your Celery task, and inside the task use jsonpickle to recreate the object.