I'm attempting to run code that responds to a text message sent through twilio and using ngrok. The code works to reply but since it halts the program, I am trying to have it run alongside other things in a loop. The current error is
"Traceback (most recent call last):
File "/home/pi/JustTheMessagePost.py", line 32, in <module>
sms()
File "/home/pi/JustTheMessagePost.py", line 17, in sms
number = request.form['From']
File "/usr/lib/python3/dist-packages/werkzeug/datastructures.py", line 431, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand."
The code I have relating to this:
#Setup
import time
from twilio.rest import Client
from flask import Flask, request, current_app
from twilio.twiml.messaging_response import Message, MessagingResponse
# Example value given by sensors
temperature = 5
#def inbound_sms():
app = Flask(__name__)
@app.route("/twilio", methods=['POST'])
def sms():
global CPUTemp
number = request.form['From']
message_body = request.form['Body']
resp = MessagingResponse()
response_message = 'This is the general Reply text'
resp.message(response_message)
if message_body == 'Tester':
resp.message(temperature)
else:
print('No text')
return str(resp)
#Loop
while True:
print("Test Text")
with app.test_request_context():
sms()
time.sleep(5)
I'm very new to using flask and webhooks so any help is appreciated.