Python Flask 服务器不能与 Twilio 一起工作

Python Flask Server Not Working With Twilio

我一直在尝试使用 Raspberry Pi 创建人员日志来记录谁在屋子里,回复 Twilio 短信和回复谁在家。我正在使用 flask 将服务器构建到 Twilio,但是当我向 'whoshome' 查询发送短信时,我根本没有收到任何回复。它应该回复谁在家,尽管目前只分配了一个人!此外,Twilio 应该向仪表板中的预定义客户端发送 POST 请求,然后在收到短信后询问说明。

#!/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

这里是 Twilio 布道者。

看起来您正在return从您的路线进行空试:

return ' '

由于您已将路由标记为接受 GET 请求,因此您可以通过在浏览器中打开此路由的 public URL 并查看 return 是什么来检查发生了什么编辑。您还可以 运行 一个针对路由的 cURL 请求来验证它的 return 符合您的预期。

我认为您可能需要 return 来自路由的 TwiML 响应而不是空字符串:

return str(r)

希望对您有所帮助。