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.