1 year ago

#378217

test-img

A Bear

Twilio & Flask SMS response never being triggered

We are working on a bot that sends and receives SMS with a conditional response system. Using Twilio, we've followed the documentation to give us the below code. Our group has limited experience with both Flask and Twilio and we're not sure which this is a question of, but our incoming_sms() method is never called. The documentation seems to lack a direct calling of this so we assume it's an event triggered on a text coming into our Twilio number, but when we test this nothing is done. Our web hook shows that the message is being picked up (shown below).

Are we missing a step to trigger the incoming_sms() function?

from twilio.rest import Client
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
from twilio import twiml

app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():
    body = request.values.get('Body', None)
    resp = twiml.Response()
    if body == 'Hello':
        resp.message("Answer 1")
    else:
        resp.message("Answer 2")
    
    return str(resp)

app.run(debug=True)

POST method picked up by Webhook:

ToCountry=US
ToState=FL
SmsMessageSid=SM8990a3baba3e88f412c9eed1b1f4ae14
NumMedia=0
ToCity=
FromZip=07960
SmsSid=SM8990a3baba3e88f412c9eed1b1f4ae14
FromState=XX
SmsStatus=received
FromCity=XXXXX
Body=Oh+Jac&FromCountry=US
To=%2B12393091468
ToZip=
NumSegments=1
ReferralNumMedia=0
MessageSid=SM8990a3baba3e88f412c9eed1b1f4ae14
AccountSid=AC87f189511d21d6480cdc84350cbbbb84
From=+1XXXXXXXXX
ApiVersion=2010-04-01

python

flask

twilio

twilio-api

0 Answers

Your Answer

Accepted video resources