Error 405 while receiving dialogflow webhook

Question:
Hi,

I’m trying to build a webhook in Python to answer Dialogflow webhook. The code seems to be correct but generates a HTTP 405 . I checked several sites and the code should work. I really do not understand where the problem is coming from.
Any idea ?
thank you in advance.

Repl link:
https://SpoonDFpy.aisperto.repl.co

from flask import Flask, request
app = Flask(__name__)

@app.route('/') # this is the home page route
def hello_world(): # this is the home page function that generates the page code
    return "Hello world!"
    
@app.route('/webhook', methods=['POST'])
def webhook():
  req = request.get_json(silent=True, force=True)
  sum = 0
  query_result = req.get('queryResult')
  num1 = int(query_result.get('parameters').get('number'))
  num2 = int(query_result.get('parameters').get('number1'))
  sum = str(num1 + num2)
  print('here num1 = {0}'.format(num1))
  print('here num2 = {0}'.format(num2))
  return {
        "fulfillmentText": 'The sum of the two numbers is: '+sum,
        "source": "webhookdata"
    }
    
   
if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8080) # This line is required to run Flask on repl.it

Are you attempting to create a Flask API that will add two numbers and return the sum?

Change that to @app.route('/webhook', methods=['GET', 'POST'])

1 Like

I don’t think thats his error.

That is what 405 is in flask, it is putting the wrong http method. If it is coming from the dialogflow webhook then I have no idea.

1 Like

yea he could be using get requests for some odd reason.

1 Like

Perhaps try this:

from flask import Flask, request, jsonify

app = Flask("app")
app.secret_key = "wowsecret123#"

@app.route("/")
def _(): return "Hello World"

@app.route("/webhook", methods=["GET", "POST"])
def web():
  if request.method == "POST":
    data = request.get_json(silent=True, force=True)
    num1 = int(data["number_1"])
    num2 = int(data["number_2"])
    sum = num1 + num2
    return jsonify(result=sum, source="webhook_data")
  return "Invalid request method."

app.run(
  host="0.0.0.0",
  port=80,
  debug=True
)

because this works for me, I’ve included the code I used to test it:

import requests

z = requests.post(
  "https://test.beliefs.repl.co/webhook",
  json = {
    "number_1": 3,
    "number_2": 5
  }
).json()

print(z)

No this a sample just to collect 2 numbers from dialogflow and then sent them to the webhook to be summed as a test. but nothing happen as I get a 405…

According to what I saw, you were creating an API to add two numbers, and a 405 error in flask means you were using the incorrect request method, as @dragonhunter1 stated.

I tried to add POST and GET but I received the same error ! HTTP 405

can you invite me to your repl (invite @beliefs)?

I simplify my code, just to reply to the webhook and get the same result…

from flask import Flask, request
app = Flask(name)

@app.route(‘/’) # this is the home page route
def hello_world(): # this is the home page function that generates the page code
return “Hello world 2!”

@app.route(‘/webhook’, methods=[‘GET’,‘POST’])
def webhook():
print(‘webhook received !’)
return {
“fulfillmentText”: ‘Hello from webhook !’,
“source”: “webhookdata”
}

if name == ‘main’:
app.run(host=‘0.0.0.0’, port=8080) # This line is required to run Flask on repl.it

can you possibly invite me to the repl.

Can you show me your server logs, like It will say GET at ROUTE. Just copy some of the console output. That should help us determine the invalid request type.

I just asked to be invited to the repl for further evaluation because I’m not sure what his problem is right now.

Hi Functionally. this is working now . I don’t know why but the change in the method is working. I did not restart my webhook properly.
Thank you very much !

1 Like

Requests will not work unless you use the correct method because it is similar to a regular webpage. So I’m just as perplexed as you are because both me and @dragonhunter1 gave you advice to resolve your issue and it took so long.

Strnge that the change did not take into account quickly but now It is working. Thanks to you again and to @dragonhunter1

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.