0

The concepts of webhooks are quite simple, yet I am here for some guidance. Consider a webhook program in FLASK:

@app.route('/webhook', methods = ['POST'])
def webhook():
    data = json.loads(request.data)

    return data

Such a program will easy return a json POST request. And result will look like this:

{
"passphrase": "SkZSV1YwZz0=",
"timenow": "2021-09-15T21:18:00Z",
"item": "apple",
"quantity": "50",
"account": "9999"
}

Now I have successfully read in the webhook data. Next step is to do something with the data. Say I would like to search my database 'df' for item: apple and return the price.

def get_item_price(df, item_name):
    # Returns price by filtering df.
    df = df[df['name'] == item_name]
    df.reset_index(inplace = True)
    price = df['price']
    return price

So, price_of_item = get_item_price(df, item_name) can now be used.

I can see if I implement the function above I can get the price of the apple but cant proceed further on how to use that price. Say I have a home page where i would like to render the price of apple.

@app.route("/")
def index():
    return index_template.format(message = price_of_item)

index_template = """
    <div> the price of apple is: {message} </div>
    """

But since webhook is a POST request url I cant get values out of it. I can save the price_of_item on a file and read it but thats pointless. Any help with filling out this missing link would be really helpful.

davidism
  • 121,510
  • 29
  • 395
  • 339
SamAct
  • 529
  • 4
  • 23

1 Answers1

0

Ok after some tutorial hunting, I have come up with 2 solutions:

  1. Create a database or file to store the values and read them as and when they get updated outside the webhook route. See here: https://www.tutorialspoint.com/flask/flask_sqlite.htm

  2. Set the price_of_the_item as cookies and then get the cookies from the home page. Best described here: https://www.tutorialspoint.com/flask/flask_cookies.htm

SamAct
  • 529
  • 4
  • 23
  • Cookies won't work - they'll only be available to the browser, and a webhook is meant to be called from another service when an event happens; i.e. outside of the browser context. – MatsLindh Sep 16 '21 at 08:12