设置从PostgreSQL到Heroku托管Python脚本的事件触发器事件

j2cgzkjk  于 2023-02-26  发布在  Python
关注(0)|答案(2)|浏览(347)

我正在尝试找出在向pSQL数据库自动添加新行后自动触发python脚本的最佳方法。我正在考虑在pSQL中创建一个“TRIGGER”,或者使用一些轮询逻辑(没有xp这样做)来不断地检查数据库的变化。我担心的是轮询可能是资源密集型的,并且不确定它在处理异步任务时有多高效。3个新行同时添加到数据库中。
有没有人有一个例子/见解,以有效的方式做到这一点?使用案例的背景如下:
用户完成一个typeform,一个新行在提交时自动添加到pSQL数据库中。(到这里为止一切都很好)然后我需要触发Heroku中托管的python脚本从数据库中获取新行,做一些事情并向用户发送一封电子邮件(SendGrid API),其中包含一些很酷的见解。提前感谢!
(NOTE- 我对Heroku一点都不感冒,任何云数据库都可以工作,只是提到这一点,因为有人可能知道一些很酷的客户端/无代码特性,可以为与轮询相关的数据库设置)

esbemjvw

esbemjvw1#

我认为这是Webhook的一个很好的用例。
如果你不熟悉Webhook,它是一种在你选择的URL上发生了什么事情时实时通知你的方法。在这种情况下,当有人提交了一个Typeform,你会在服务器上收到一个JSON有效载荷,其中包含提交的所有答案。
一旦你方收到有效载荷,你可以做两件事:将答案存储在数据库中并调用API。
以下是有效负载的示例:

{
    "event_id": "LtWXD3crgy",
    "event_type": "form_response",
    "form_response": {
        "form_id": "lT4Z3j",
        "token": "a3a12ec67a1365927098a606107fac15",
        "submitted_at": "2018-01-18T18:17:02Z",
        "landed_at": "2018-01-18T18:07:02Z",
        "calculated": {
            "score": 9
        },
        "definition": {
            "id": "lT4Z3j",
            "title": "Webhooks example",
            "fields": [
                {
                    "id": "DlXFaesGBpoF",
                    "title": "Thanks, {{answer_60906475}}! What's it like where you live? Tell us in a few sentences.",
                    "type": "long_text",
                    "ref": "[readable_ref_long_text",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "SMEUb7VJz92Q",
                    "title": "If you're OK with our city management following up if they have further questions, please give us your email address.",
                    "type": "email",
                    "ref": "readable_ref_email",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "JwWggjAKtOkA",
                    "title": "What is your first name?",
                    "type": "short_text",
                    "ref": "readable_ref_short_text",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "KoJxDM3c6x8h",
                    "title": "When did you move to the place where you live?",
                    "type": "date",
                    "ref": "readable_ref_date",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "PNe8ZKBK8C2Q",
                    "title": "Which pictures do you like? You can choose as many as you like.",
                    "type": "picture_choice",
                    "ref": "readable_ref_picture_choice",
                    "allow_multiple_selections": true,
                    "allow_other_choice": false
                },
                {
                    "id": "Q7M2XAwY04dW",
                    "title": "On a scale of 1 to 5, what rating would you give the weather in Sydney? 1 is poor weather, 5 is excellent weather",
                    "type": "number",
                    "ref": "readable_ref_number1",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "gFFf3xAkJKsr",
                    "title": "By submitting this form, you understand and accept that we will share your answers with city management. Your answers will be anonymous will not be shared.",
                    "type": "legal",
                    "ref": "readable_ref_legal",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "k6TP9oLGgHjl",
                    "title": "Which of these cities is your favorite?",
                    "type": "multiple_choice",
                    "ref": "readable_ref_multiple_choice",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "RUqkXSeXBXSd",
                    "title": "Do you have a favorite city we haven't listed?",
                    "type": "yes_no",
                    "ref": "readable_ref_yes_no",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "NRsxU591jIW9",
                    "title": "How important is the weather to your opinion about a city? 1 is not important, 5 is very important.",
                    "type": "opinion_scale",
                    "ref": "readable_ref_opinion_scale",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "WOTdC00F8A3h",
                    "title": "How would you rate the weather where you currently live? 1 is poor weather, 5 is excellent weather.",
                    "type": "rating",
                    "ref": "readable_ref_rating",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                },
                {
                    "id": "pn48RmPazVdM",
                    "title": "On a scale of 1 to 5, what rating would you give the general quality of life in Sydney? 1 is poor, 5 is excellent",
                    "type": "number",
                    "ref": "readable_ref_number2",
                    "allow_multiple_selections": false,
                    "allow_other_choice": false
                }
            ]
        },
        "answers": [
            {
                "type": "text",
                "text": "It's cold right now! I live in an older medium-sized city with a university. Geographically, the area is hilly.",
                "field": {
                    "id": "DlXFaesGBpoF",
                    "type": "long_text"
                }
            },
            {
                "type": "email",
                "email": "laura@example.com",
                "field": {
                    "id": "SMEUb7VJz92Q",
                    "type": "email"
                }
            },
            {
                "type": "text",
                "text": "Laura",
                "field": {
                    "id": "JwWggjAKtOkA",
                    "type": "short_text"
                }
            },
            {
                "type": "date",
                "date": "2005-10-15",
                "field": {
                    "id": "KoJxDM3c6x8h",
                    "type": "date"
                }
            },
            {
                "type": "choices",
                "choices": {
                    "labels": [
                        "London",
                        "Sydney"
                    ]
                },
                "field": {
                    "id": "PNe8ZKBK8C2Q",
                    "type": "picture_choice"
                }
            },
            {
                "type": "number",
                "number": 5,
                "field": {
                    "id": "Q7M2XAwY04dW",
                    "type": "number"
                }
            },
            {
                "type": "boolean",
                "boolean": true,
                "field": {
                    "id": "gFFf3xAkJKsr",
                    "type": "legal"
                }
            },
            {
                "type": "choice",
                "choice": {
                    "label": "London"
                },
                "field": {
                    "id": "k6TP9oLGgHjl",
                    "type": "multiple_choice"
                }
            },
            {
                "type": "boolean",
                "boolean": false,
                "field": {
                    "id": "RUqkXSeXBXSd",
                    "type": "yes_no"
                }
            },
            {
                "type": "number",
                "number": 2,
                "field": {
                    "id": "NRsxU591jIW9",
                    "type": "opinion_scale"
                }
            },
            {
                "type": "number",
                "number": 3,
                "field": {
                    "id": "WOTdC00F8A3h",
                    "type": "rating"
                }
            },
            {
                "type": "number",
                "number": 4,
                "field": {
                    "id": "pn48RmPazVdM",
                    "type": "number"
                }
            }
        ]
    }
}

下面是处理webhook有效负载的示例:https://glitch.com/~tf-webhook-receiver
Typeform documentation

tuwxkamq

tuwxkamq2#

更简单的方法是先将表单提交给您的函数,然后由该函数处理新数据的DB存储和通过SendGrid发送电子邮件通知,而不是先存储到DB。
Typeform有webhook functionality,您可以使用它来实现这一点。
您可以将它配置为将新填写的表单发送到您的函数(托管在Heroku上或您的情况下的任何地方),然后在该函数上执行任何必要的操作。

相关问题