1

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.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Kate Gurman
  • 73
  • 1
  • 8

1 Answers1

1

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.

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • Thanks for your reply! To create `telebot.types.Message`, you need too many dependencies, maybe there is some kind of built-in method to turn `telebot.types.Message` into a json, then send it to Celery and create `telebot.types.Message` in it? – Kate Gurman Nov 04 '19 at 14:47