abhishek-ram/django-pyas2

Callback when message has been received

Opened this issue · 6 comments

Hi,
There is a more elegant way to trigger a received message event without monkey patching 'run_post_receive' like I did below?

from pyas2 import utils
import requests


old_run_post_receive = utils.run_post_receive


def new_run_post_receive(message, full_filename):
    old_run_post_receive(message, full_filename)
    payload = dict(
        id='1',
        access_token='xyz'
    )
    r = requests.post('http://127.0.0.1/url/to/post', data=payload)


utils.run_post_receive = new_run_post_receive

Why not just define this logic in the post receive command?
Create a post receive script post_as2_message_receive.py which would be something like

#!/usr/bin/env python3
import requests

payload = dict(
        id='1',
        access_token='xyz'
)
r = requests.post('http://127.0.0.1/url/to/post', data=payload)

# Put additional post receive logic here

And set Command on Message Receipt: on the partner to path_to_script/post_as2_message_receive.py

Thanks for your answer.
Good idea but I am not a fan on using external scripts.
Anyway I guess a "Command on Message Receipt" can handle received data too (passing that as a parameter).
Do you have other approaches I could dig into?
What do you think implementing a feature to handle callbacks on such events? (I might work on that)

It would be part of your repo so I don't see it as an external script. I dont see a reason to implement callbacks as it can be handled in this command.

I have the same question here.
For my opinion: I prefer to use callback instead of external command.

I guess with callbacks you can have some pros, for example a return code to refuse the message or the chance to handle the content of the message directly or change headers data too before saving.

Hi @abhishek-ram , @bluebytech ,
I tried an implementation for this and did a pull request: #53.

Could you please review it and share your suggestions?

with the PR, the integration can be done in another Django apps in this way instead of defining an external command

from pyas2.utils import pyas2Utils


def cutmize_run(*args, **kwargs):
    print(args, kwargs)


pyas2Utils.cust_run_post_receive = cutmize_run

Thanks,