2016-08-17 52 views
-1

我一直在嘗試使用Raspberry Pi創建一個人員日誌來記錄誰在屋內並回復Twilio文本消息並回復誰在家。我使用燒瓶來形成Twilio的服務器,但是當我輸入'whoshome'查詢時,我沒有得到任何迴應。它應該回復誰在家,儘管只有當前分配的一個人! Twilio還應該向儀表板中的預定義客戶端發送POST請求,然後在收到SMS時詢問指示信息。Python燒瓶服務器不能與Twilio一起使用

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

你沒有對客戶做任何事情......你期待它神奇地知道該怎麼做?或者Twilio應該向你的客戶發送GET/POST請求? (也是當你粘貼它時,你的格式化會與降價掛鉤。請編輯你的帖子並修復) –

+0

@WayneWerner Twilio文檔給人的印象是它應該發送一個POST請求,因爲它在收到消息(來自我的手機),然後將POST請求發送到預定義的服務器(如在儀表板中設置)以詢問要執行的操作。另外我恐怕我不太明白你的意思是什麼格式? –

+0

您是否配置Twilio將請求發送到此位置?這個位置是否可以被外部世界訪問? (可能不是,它位於路由器後面)客戶端與接收* Twilio消息的應用程序有什麼關係? (客戶端用於*發送*消息。) – davidism

回答

2

Twilio evangelist here。

看起來你是返回一個空的從你的路線嘗試:

return ' ' 

既然你已經標記爲接受GET請求的路線,你可以檢查,這是什麼打開公共網址這條路發生在瀏覽器中查看返回的內容。您也可以針對路線運行一個cURL請求,以驗證其返回的結果。

我想你也許需要從路線,而不是空字符串返回TwiML響應:

return str(r) 

希望有所幫助。

+0

感謝這工作 –