Matthew N.
Matthew N.

Reputation: 17

"if" statement acting weird in twilio texting project

I am trying to create a texting project where I can text a twilio number, and based off what the message is I can reply with a custom message. I am using a string of if statements to check the body of the text and reply with certain things

app = Flask(__name__)

@app.route("/sms", methods=['GET', 'POST'])

def sms_reply():
  reply_pointer = 0
  body = request.values.get('Body', None)
  resp = MessagingResponse()

  print(body)

  if(body == "Test1"): reply_pointer = 0
  elif(body == "Test2"): reply_pointer = 1
  elif(body == "Test3"): reply_pointer = 2
  elif(body == "Test4"): reply_pointer = 3


  print(reply_pointer)

I am texting these messages from my phone and if I send Test1 through Test3 I get normal results, but when I text Test4, reply_pointer gets set to 0 and it stays 0 if I text Test1, Test2, etc. I've been trying to fix this for so long and I have no idea whats going on.

Upvotes: 0

Views: 47

Answers (1)

niceman
niceman

Reputation: 361

The problem is with the body content. I tested the bottom part of your code locally, and it works correctly if the body is indeed "Test4":

>>> reply_pointer = 0
>>> body = "Test4"
>>> 
>>> if(body == "Test1"): reply_pointer = 0
... elif(body == "Test2"): reply_pointer = 1
... elif(body == "Test3"): reply_pointer = 2
... elif(body == "Test4"): reply_pointer = 3
... 
>> reply_pointer
3

Check if the body doesn't contain any other characters. You can replace the body == "Test4" with "Test4" in body, to check for matching substring.

Upvotes: 1

Related Questions