ngrok server doesn't connect with dialogfow - python

I am an absolute beginner in chat bot. I am learning on my own and went on developing a very simple chat bot using Dialog flow. I have a python code for responding the request to my Dialog flow bot. I have enabled "webhook" in fulfillment and also enabled in "Intent".My ngrok url is http://ae3df23b.ngrok.io/. I have written a function in my python code which respond to ngrok which connects Dialog flow. Now problem is that It is showing error "404 not found" and The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. Please help me guys. Thanks in advance.
My code is
#import necessary packages and libraries
import urllib
import os
import json
from flask import Flask
from flask import request
from flask import make_response
app=Flask(__name__)
#app.route('/webhook', methods=['POST'])
def webhook():
req=request.get_json(silent=True, force=True)
print("Request:")
print(json.dumps(req, indent=4))
res=makeWebhookResult(req)
res=json.dumps(res, indent=4)
print(res)
r=make_response(res)
r.headers['Content-Type']='application/json'
return r
def makeWebhookResult(req):
if req.get("result").get("action")!="interest":
return {}
result=req.get("result")
parameters=result.get("parameters")
name=parameters.get("Banknames")
bank={'SBI':'10%', 'HDFC Bank':'9%', 'Bank of Baroda':'11', 'Federal Bank':'8.9%', 'ICICI Bank': '11.5%'}
speech='The interest rate of '+ name + "is" + str(bank[name])
print("Response:")
print(speech)
return {
"speech":speech,
"displayText":speech,
"source":"BankInterestRates"
}
if __name__ == "__main__":
port=int(os.getenv('PORT', 80))
print("Starting app on port %d", (port))
app.run(debug=True, port=port, host='0.0.0.0')

I think you should use https://ae3df23b.ngrok.io/webhook. You are missing the path. Also, use https and generate a new ngrok URL and update the fulfilment.

Try using this format instead https://ae3df23b.ngrok.io/[replace-with-your-project-id]/us-central1/dialogflowFirebaseFulfillment
and make sure u start ngrok with port 5000 and host your functions with "firebase serve" command

Related

I can't connect my code to my local host webserver from terminal

I'm trying to run my code from the terminal to my local host using python3 command but its not running on the server for some reason. Can anyone help?
Here is my code to connect to web server.
from flask import Flask
from flask import request
from flask import Response
import keywordnum
app_first = Flask(__name__)
#app_first.route("/")
def key():
word=request.args.get('word')
len=keywordnum.count(word)
reply=str(len)
r= Response(response=reply, status=200)
return r
if__name__=='__main__'
app_first.run(host="0.0.0.0",port=4000)
Check your indentation, python is strict with it. The following line
if__name__=='__main__'
should be rewritten as follows:
if __name__=='__main__':
Indentation in python allows for a clearer code.

How to invoke/run a Python script using a web URL?

I have a Python script that pulls data from a 3 rd party API. Currently this Pyhton script is automated on server side.
There are few instances where I have to toggle the script manually for new data updates. For the manual toggle I have to login to the server each time and run it from command line. Is there a way where I can create web url or something similar and just run that URL to make that script run from the browser address bar.
One approach you could take is to use Flask, which is a minimal web framework. Here's an example of how you could use it:
from flask import Flask
from your_script import your_func
app = Flask(__name__)
#app.route('/run')
def run_command():
your_func()
return 'Executed your function!'
if __name__ == '__main__':
app.run(debug=False, port=8080)
If you run this code you'd get a web server running on port 8080 that executes your function when you access the url. Here's a tutorial in the Flask documentation to get you started.
I think the easiest way to do this is by using Flask.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
# your code here
return 'Hello, World!'

Flask requests not working when application is deployed

I deployed a flask application in Windows Server 2016 on IIS. Everything was working fine, then I updated windows and now, I'm unable to use requests.get() when deployed.
Here is a simple code:
from flask import Flask
from flask_cors import CORS
from flask import request
import requests
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}}, send_wildcard=True)
#app.route("/")
def hello():
r = requests.get('http://google.com')
return r.text
if __name__ == "__main__":
print ("\nAttempting to start server ...")
app.run(threaded = True, port=5052)
When I run it, it browser will hang for 30s or so, and then returns a 500 error.
The server has access the internet: i can browse, use pip and I'm even able to do requests from the python command line or as a stand alone program, but not when it's deployed.
I have tried headers, cors, played with pratically all possible settings. I have no idea why it doesn't work anymore.
can u change your protocol as https..It works fine for me..:) I too spent nearly one hour for trying out all possibilities.
Sorry for turning out this to be simple :)
Interesting this is my first solution on stack overflow..

How to properly return http response from Python Requests library (want a Python reverse proxy)

This question continues from here.
I want to create a reverse proxy of sorts that will allow me to host an application that runs on a specified port on a server that does not have that port open. But I do not want take over ports 80/443 because I need to have other apps running on that server. Instead, I want an app running at localhost:#### to be served from a suburl or subdomain such as http://myserver/myapp.
Here is what I tried:
helloflask.py
#!/usr/bin/env python
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
middle.py
#!/usr/bin/env python
print ("Content-Type: text/html")
print()
import requests
#response = requests.get("http://localhost:5000")
response = requests.get("http://localhost:8888/token=8a387fe88d662e2568f9b8ec2398191452492e7184536670")
print(response.text)
When I run helloflask.py it runs on port 5000, and I can visit mydomain/middle.py and get the correct response of "Hello World!" displayed in the browser.
However, when I run a jupyter notebook on port 8888, I get a malformed web page. Strangely, the page sources are identical (see the diff).
So, what do I need to do to make jupyter run from the proxy?
Here is an image of how the page appears through middle.py. The link to IPythonParallel works, but no other links or controls function in any way.

Method not allowed - 405 Error - with Twilio API, using flask and python, heroku

I was following a Twilio tutorial on sending SMS through the API. I followed all the steps, however, I am receiving a 405 error. My code:
from flask import Flask
from twilio import twiml
import os
app = Flask(__name__)
#app.route('/sms', methods=['POST'])
def sms():
r = twiml.Response()
r.sms("This is awesome!")
return str(r)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
if port == 5000:
app.debug = True
app.run(host='0.0.0.0', port=port)
I am getting a 405 error (method not allowed), when calling my url, which looks like: http://my-url.herokuapp.com/sms, which is also associated like this to the twilio account. When I include 'GET', everything works, this is not according to tutorial however. Any hints?
Looking at the repository it seems that you will actually need to text to the number that Twillo is proxying for you. If you want to access the URL in your browser as well you will need to add 'GET' to the methods list (as you discovered).

Categories