【发布时间】:2016-08-17 20:31:58
【问题描述】:
我一直在尝试使用 Raspberry Pi 创建一个人员日志来记录谁在家里,并回复 Twilio 短信并回复谁在家。我正在使用烧瓶将服务器构建到 Twilio,但是当我向“whoshome”查询发送短信时,我根本没有得到任何响应。它应该回复谁在家,虽然目前只分配了一个人!此外,Twilio 应该向仪表板中的预定义客户端发送 POST 请求,然后在收到 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
【问题讨论】:
-
你没有对客户做任何事情......你期待它神奇地知道该怎么做吗?还是 Twilio 应该向您的客户发送 GET/POST 请求? (同样当你粘贴它时,你的格式被降价吓倒了。请edit你的帖子并修复它)
-
@WayneWerner Twilio 文档给人的印象是它应该发送 POST 请求,因为它会在接收到消息(来自我的手机)时采取行动,然后将 POST 请求发送到预定义的服务器(如设置在仪表板中)询问该怎么做。另外,恐怕我不太明白您对格式的含义是什么?
-
看看
whos_home。 twilo 是否正在向您的服务器发送 post 请求? -
@WayneWerner 对外可用,已转发。此外,Flask 客户端用于接收然后发送 TwiML 指令,实际上根本不需要服务器来简单地发送消息,只需在接收时接收或发送 TwiML 指令。是的,我已经完成了 tcp 转储,并且收到了来自 Twilio 的发布请求。他们提供的示例代码使用完全相同的 Flask 服务器设置,对我来说效果很好。
-
如果 twilio 实际上是在 POST 到您的服务器并且您已经确认您收到了请求,那么您的问题是您返回了错误的响应,或者 twilio 出现了其他问题侧面