Python Flask Server Not Working With Twilio

半腔热情 提交于 2019-12-13 22:24:50

问题


I have been trying to create a personnel log using Raspberry Pi to record who is in the house and to respond to a Twilio text message and reply who is home. I'm using flask to form the server to Twilio however I am getting no response at all when I text the 'whoshome' query. It should reply who is home, although only with one person assigned currently! Also Twilio should be sending a POST request to the predefined client in the dashboard to then ask for instructions upon receiving an SMS.

#!/usr/bin/python
import time
import thread
from twilio import twiml
import Adafruit_CharLCD as LCD
import os
import logging
import twilio.twiml
from twilio.rest import TwilioRestClient
from flask import Flask, request, redirect

lcd_rs        = 21                                              #lcd setup
lcd_en        = 22
lcd_d4        = 25
lcd_d5        = 24
lcd_d6        = 23
lcd_d7        = 18
lcd_backlight = 4

lcd_columns = 16
lcd_rows    = 4

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

logging.basicConfig(filename='wifilog.log', level=logging.INFO) #logging setup

ACCOUNT_SID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"              #Twilio credentials setup
AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

user1status = 0                                                 #variables

def wifiping(): #check who is present
    while True:
        ret = os.system("ping -c 1 -s 1 192.168.1.118")
        lcd.clear()
        if ret != 0:
            lcd.message('Sam is not home')
            print "Sam is not home"
            logging.info('Sam not home at' + time.strftime("%H:%M:%S", time.gmtime()))
            user1status = 0
            time.sleep(5)
        else:
            lcd.message('Sam is home')
            print "Sam is home"
            logging.info('Sam home at' + time.strftime("%H:%M:%S", time.gmtime()))
            user1status = 1
            time.sleep(5)

thread.start_new_thread(wifiping, ()) #new thread

r = twiml.Response()                  #Flask server setup
app = Flask(__name__)
app.config.from_object(__name__)

@app.route("/", methods={'GET', 'POST'})
def whos_home():                           #Twilio message detection
    body = request.values.get('Body', None)
    if body == 'Whos home':
        if user1status == 0:
            r.message("Sam is not home.")
        elif user1status == 1:
            r.message("Sam is home.")
    else:
        pass
    return ' '

app.run(debug=True, host='0.0.0.0', port=80) #Flask app start

回答1:


Twilio evangelist here.

It looks like you are returning an empty trying from your route:

return ' '

Since you've marked the route as accepting GET requests you can check this is what's happening by opening the public URL for this route in a browser and seeing what's returned. You could also run a cURL request against the route to verify its returning what you expect.

I think you probably need to return the TwiML Response from the route instead of the empty string:

return str(r)

Hope that helps.



来源:https://stackoverflow.com/questions/39005768/python-flask-server-not-working-with-twilio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!