-1

I am trying to create a connection to whatsapp and my python script via Twilio, to generate automatic conversations. I follow every step in order of this link https://www.pragnakalp.com/create-whatsapp-bot-with-twilio-using-python-tutorial-with-examples/?unapproved=4864&moderation-hash=3861b2bc00104a39b5f3211076dda854#comment-4864

I started with a problem of ngrok http 5000 that said "POST/ 403 Forbidden", in the section of https requests. But now it dissapeared, which I guess is because I add in the command vi "path of ngyrok" a different web address. The reason what I did this is because I was realizing that putting a different port of my localhost to connect the web server for twilio is not compatible. In other words, the connection was not going to the final destinary, which is Twilio. Despite of all these changes, the port of local host is still appearing 4040, as in the following image I show:

ngrok http 5000

When I activate the connection for my whatsapp number, and save the forwarding url of ngrok in twilio, as I said before, it dissapears the warning "POST/ 403 Forbidden", instead it is appearing nothing:

HTTP requests

I found that adding in the forwarding url the /message Twilio receives my request and it answers me, but is not the case, and that´s why you can see in the https request the POST /message

Before sending the message to the bot I run my python script:

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)


#@app.route("/wa")
#def wa_hello():
 #   return "Hello, World!"


@app.route("/wasms", methods=['POST'])
def wa_sms_reply():
    """Respond to incoming calls with a simple text message."""
    # Fetch the message

    msg = request.form.get('Body').lower()  # Reading the message from the whatsapp

    print("msg-->", msg)
    resp = MessagingResponse()
    reply = resp.message()
    # Create reply
    if msg == "hi":
        reply.body("hello!")

    return str(resp)


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

I tried running commands like ngrok http https://localhost:5000, turning off firewalls on my Mac, allowing my country on the Twilio´s web page, inserting config.hosts << /.*.ngrok.io/ in my terminal and restaring my laptop, config.hosts << "a0000000.ngrok.io" and skip_before_action :verify_authenticity_token but nothing of this work for me. Does anyone knows what is the problem?

1 Answers1

0

It looks like your webhook requests are going to localhost:5000/message but your app only defines /wasms as a route that can be used, which would explain why you are getting a 403 error.

Either modify line 11 of your app to accept a POST to /message, or go to the Twilio console and update your webhook to post to /wasms.

As a side note, Ngrok uses port 4040 just to show a diagnostic web interface. Your tunnel is still connecting to port 5000, which should be correct for a Flask app.

Charlie Weems
  • 1,682
  • 1
  • 16
  • 28
  • Still with problems... POST /wasms 403 Forbidden and I saved the new forwarding url in sandboxsettings with dash wasms. Any idea? – Mateo Severi Feb 24 '23 at 19:52
  • Can you define any route in your local app (e.g. GET /) that is reachable via your Ngrok URL? – Charlie Weems Feb 24 '23 at 21:45
  • Is denied... GET / 403 Forbidden. I copy the forwarding url to google chrone and when I put visit here it says access denied – Mateo Severi Feb 24 '23 at 23:34
  • Maybe related to this issue? https://stackoverflow.com/questions/72795799/how-to-solve-403-error-with-flask-in-python – Charlie Weems Feb 25 '23 at 01:39
  • I still don't get it. I am using ngrok http 800 instead of flask in the running command. I wrote on a separate answer the code and what I am doing. Because I am running flask run --port 8000 and it says ''Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.'' – Mateo Severi Feb 28 '23 at 21:13