簡體   English   中英

如何在 twilio 中使用 python 在入站短信中捕獲用戶沒有響應,然后退出入站,然后發送出站短信?

[英]How to capture no response from user in inbound sms with python in twilio to then exit the inbound and then send an outbound sms message?

我正在嘗試使用 twilio 構建一個提醒應用程序,並且在 twilio 的 API 等待響應時在后台運行計時器時遇到問題。 在運行 confirm_message 函數中的任何代碼之前,代碼似乎只是在 @app.route("/sms", methods=["POST"]) 等待響應。 從長遠來看,我想做的是每 24 小時發送一次出站消息,但是在每條出站消息之后,我都會發送一條入站消息供用戶回復,但如果用戶不回復,則出站消息在send_message 函數無法執行。 我任何人都可以幫助我解決這種情況,我將不勝感激。

謝謝

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
import time
import os
import signal
from datetime import datetime

#sending inbound message
app = Flask(__name__)
@app.route("/sms", methods=["POST"])
def confirm_message():
    #after user replies it takes the time
    current_time = time.strftime("%H:%M:%S %p")
    while True:
        # if the time is midnight then os.kill will go back to main and the code restarts
        if current_time == "00:00:00 AM":
            os.kill(signal.CTRL_C_EVENT, 0)
        # else it will get the body of the text and if its "OK" then return back to main, otherwise record the time until its midnight
        # and the code restarts
        else:
            body = request.values.get("Body")
            if body == "OK":
                os.kill(signal.CTRL_C_EVENT, 0)
            else:
                current_time = time.strftime("%H:%M:%S %p")

#sending outbound message
def send_message():
    account_sid = str(input()) 
    auth_token = str(input())
    client = Client(account_sid, auth_token) 

    client.messages.create(  
            messaging_service_sid= str(input()), 
            body='Hi, its time to take your vitamins. \n\nReply OK if you have taken vitamins.',      
            to='+##########'
            )
    print ("Message has been sent")


while True:
    #gets the initial time the outbound message is send
    val1 = datetime.now()
    #sends outbound
    send_message()
    #runs inbound
    app.run(debug=False)
    #gets the send time after outbound and inbound message complete
    val2 = datetime.now()
    #takes difference and converts the time into seconds
    difference = (val1 - val2)
    difference_seconds = 86400.0 - (difference.total_seconds())
    #checks to see if the difference is greater than the number of seconds in 24 hours (86400)
    #if the seconds are greater, then keep adding 86400 untill the difference becomes positive
    while difference_seconds <= 0.0:
        difference_seconds += 86400
    
    #start timer
    starttime = time.time()
    totaltime = 0.0

    #timer continues until it reaches the difference number, which will end of sending another outbound at the same time it did yesterday
    while totaltime <= difference_seconds:
        totaltime = round((time.time() - starttime), 2)

這里有幾件事我認為不會很好。 我不認為主線程是運行while True:循環來做后台事情的正確位置。 此外,您不應將 Flask 應用程序作為該循環的一部分運行,因為這也是一種阻塞方法,在您的應用程序關閉之前不會返回。

我建議您將發送預定消息和接收入站消息的代碼分開。 我不確定您在收到入站消息時設置的current_time做什么,但這可能更好地存儲在數據庫中而不是本地變量中。

您可能想從研究如何在 Python 中運行后台線程開始

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM