Forums

Python flask app running on Pythonanywhere not able to send whatsapp messages through ngrok and twilio

My ngrok tunnel web interface is http://127.0.0.1:4040. However, I am not able to open the interface as it says 127.0.0.1 refused to connect. So I am not able to get the webhook link from the interface to add to Twilio Sandbox. Hence my python flask app is not able to reply back to messages. The app is python code using flask. It replies to whatsapp messages through Twilio API using ngrok tunnel. The code works fine in my local computer. However I added the Forwarding Link "https://c664-54-226-131-176.ngrok-free.app" to Twilio sandbox and found that when a whatsapp message is sent to the app, in pythonanywhere I see the following: HTTP Requests POST /twilio However, the reply message is not shooted back. Please help to fix this issue.

Could you add some more details? In particular, what code do you have running on PythonAnywhere?

Hi, I am new to all this. I created a SSH key and pulled my repo from Github. So it created a directory "cbot". I have a "venv" virtual environment. Previously I was able to start the local server at port 5000 Then from another console I was runnung a ngrok tunnel at 5000 and I was pasting the Forwarding Link "https://c664-54-226-131-176.ngrok-free.app" to Twilio sandbox. But the code is not not able to send back messages. When i type a HI from my whatsapp, the ngrok tunnel shows that a twilio post was made but has no response. This process works seamlessly in VSCode. Now of sudden I am not able start the app as it says port 5000 is used by another program.

On using : ss -nltp
I find: LISTEN       0             128                                        127.0.0.1:5000                     0.0.0.0:*

However I am not able to kill the process. I badly need your help to deploy this working app on pythonanywhere. If you want you can take access of my account and check what I am doing wrong. Thanks in advance. The flask code is as follows

from flask import Flask, request
from document_gpt.helper.conversation import create_conversation
from document_gpt.helper.twilio_api import send_message
qa = create_conversation()
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    return 'MAN', 200

@app.route('/twilio', methods=['POST'])
def twilio():
    query = request.form['Body']
    sender_id = request.form['From']
    print(sender_id, query)

    res = qa(
        {
        'question': query,
        'chat_history': {}
        }
    )

    print(res)

    send_message(sender_id, res['answer'])

    return 'MAN', 200

This is the run.py

from document_gpt.src.main import app

if __name__ == '__main__':
    app.run(debug=True)

If you want your app to be accessible from outside of PythonAnywhere, you need to expose it as web app, running it in a console would not work.

Thanks for the reply. In that case what would be the webhook of the app that I need to put in twilio, so that it can take care of the whatsApp message part. Also how do I kill the process in my account that is running at 127.0.0.1:5000. Please help.

You don't want to run your web app in a bash console. Check out this help page to get your flask app up and running - https://help.pythonanywhere.com/pages/Flask/

About ngrok - You shouldnt need to use ngrok in PythonAnywhere. You need to use the ngrox url on your local machine because your local computer isn't accessible via the internet (for your security). But, when you start your pythonanywhere web app it'll have a publically accessible url. So you'd use that in twilio instead of the url ngrok generates.

Great! let me try this. However can you please help me to kill the following process LISTEN 0 128 127.0.0.1:5000 0.0.0.0:*

What process is this? Why do you need to kill it?