Forums

Challenge getting a POST to work

Hello wizards!

I got my Plivo import to work (yay!) and I can get my webapp to trigger a send email/text script when it's pulled up. send_sms is a sub-function of send_email, which is why you don't see it here.

However, I cannot seem to get it to trigger upon a POST request.

This is a tricky problem, because it COULD be my code and it COULD be my Plivo setup. I've asked them about it but haven't received word back--they don't seem terribly responsive. So I'd like to tackle it from the code side. If there is an incoming POST request with a 'text' item, SHOULD this catch it and trigger the send_email function?

Also I know the code isn't incredibly clean--I'm still trying to just prove the concept works, and I'm pulling from tutorials at Plivo. In theory, I should be able to text the number from my phone, which sends a POST request to mrg.pythonanywhere.com/foo, which sends the 'from' and 'text' fields into my processing.py file, which does things with the message and calculates a reply.

Thank you in advance for your help!! :-D

@app.route('/foo', methods=["GET", "POST"])
def receive_sms():
    # Sender's phone numer
    from_number = request.values.get('From')
    # Receiver's phone number - Plivo number
    to_number = request.values.get('To')
    # The text which was received
    text = request.values.get('Text')

    try:
        send_email(text)
    except:
        message = "code failed."
        send_email(message)
    return'''
    this page is designed to receive texts.
    '''

UPDATE--I got it to work!

It turns out that there's no "www" in front of mrg.pythonanywhere.com. Fixed it on the Plivo side and we're in business!

yay!