0

I keep getting caught up tracking delivery status of messages in Flask, Twilio, and Python. I found the below code on Twilio's docs for tracking delivery but am having a hard time implementing it.

How would I go from the below code to actually seeing the delivery status of a message within Python? I have ngrok running but am confused where to put that url. Any help is appreciated!

from flask import Flask, request
import logging

logging.basicConfig(level=logging.INFO)

app = Flask(__name__)

@app.route("/MessageStatus", methods=['POST'])
def incoming_sms():
    message_sid = request.values.get('MessageSid', None)
    message_status = request.values.get('MessageStatus', None)
    logging.info('SID: {}, Status: {}'.format(message_sid, message_status))

    return ('', 204)

if __name__ == "__main__":
    app.run(debug=True)
Daman
  • 27
  • 4

1 Answers1

0

You provide the URL as status_callback when you send the SMS:

from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages \
    .create(
         body='McAvoy or Stewart? These timelines can get so confusing.',
         from_='+15017122661',
         status_callback='http://postb.in/1234abcd',
         to='+15558675310'
     )

print(message.sid)

So instead of the postb.in URL above, change that to your ngrok URL. However be aware that the ngrok URL changes every time you restart the connector, unless on the paid plan (apparently). So you'd need to change the URL in the above code, every time you restart the ngrok connector.

With that in mind, you can then set up the separate app to handle the callback as per the code block in your question.


UPDATE

I'm able to do the call out but I am still having issues with Flask and the call back status.

I don't have an account with Twillio to test, but I'd first check that the callback request is actually hitting your Flask server.

I would assume from the doc's code samples, that with status_callback URL of http://postb.in/1234abcd that request actually hits the endpoint http://postb.in/1234abcd/MessageStatus, however it's possible that's a mistake and the flask code should be: @app.route('/1234abcd') if you provided such a URL. (This isn't too clear in the docs).

If you're certain the request is reaching the server, but your code isn't handling it properly, it may be wise to set up a catch-all route like:

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

@app.route('/', defaults={'path': ''}, methods=['POST'])
@app.route('/<path:path>', methods=['POST'])
def catch_all(path):
    print (path, request.url, request.get_data())
    return ('', 204)

This will allow you to inspect any requests reaching the server, regardless of the path.

You could also test this catch-all route with the requests library (not to be confused with Flask's request object by the way!):

# From any other terminal
import requests
r = requests.post('http://localhost:5000/',json={1:2})
r = requests.post('http://localhost:5000/asd',data={3:4})

Server console output:

http://localhost:5000/ b'{"1": 2}'
172.17.0.1 - - [17/Jul/2020 23:11:55] "POST / HTTP/1.1" 200 -
asd http://localhost:5000/asd b'3=4'
172.17.0.1 - - [17/Jul/2020 23:12:00] "POST /asd HTTP/1.1" 200 -
v25
  • 7,096
  • 2
  • 20
  • 36
  • Thanks, I'm able to do the call out but I am still having issues with Flask and the call back status. How would you do that? – Daman Jul 17 '20 at 22:15