如何调用这个检查是否收到消息然后发送响应的函数?

How to call this function that checks if a message as been received and then sends response?

我有一个单独运行良好的函数,但是当我尝试在另一个程序中调用它时它不起作用,我想用它来检查是否已收到消息,然后在收到消息时做出响应。


from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
from flask import Flask
app = Flask(__name__)

@app.route("/sms", methods =['POST'])
def sms_reply():
    number = request.form['From']
    m_b = request.form['Body']
    response_message='unknown response, type KEW for a list of known words'
    resp = MessagingResponse()
    print(m_b)
    if m_b=='hello' or m_b== 'Hello':
        response_message='Hi,how are you?'
         
    elif m_b=='KEW' or m_b== 'kew' or m_b== 'Kew':
        response_message='hello, ..... yeh thats only one i have set up so far im still testing it'

    resp.message(response_message)

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)

正在调用的程序

def bot():
    
       
    timer=time.time()
    sold_out=True 
    
    print('Bot is starting')                                                                                                                                                                         
    print('Loading item page')
    still_running_check=0        
    while sold_out==True:                                                                                                                                       #while item is sold out reload the page and print sold out to the console
       driver.get(item_url)                      
       #time.sleep(1)
       sold_out=check_exists_by_xpath("/html/body/div[3]/main/div[2]/div[3]/div[2]/div/div/div[6]/div[1]/div/div/div/button")                                   
       still_running_check+=1
       sms_reply()
       #if still_running_check%100==0:
           #sms('Bot is still running and is on attempt '+str(still_running_check)+'\n '+str(round((time.time()-timer)/60,2))+' minute(s) have passed')
       if sold_out==False:
           break
       else:
           print ("Sold out")
    
    cart_button = driver.find_element_by_class_name("fulfillment-add-to-cart-button")                                                                   #once sold out is no longer true add to cart button is clicked
    cart_button.click()
    time.sleep(1)
    driver.get("https://www.bestbuy.com/checkout/r/fast-track")                                                                                             #navigates to checkout page
    time.sleep(1)
    code= driver.find_element_by_xpath('//*[@id="credit-card-cvv"]')                                                                                        #csv input location
    code.send_keys(security_code)                                                                                                                           #inputs previously given csv code
    time.sleep(2)
    #confirm_button= driver.find_element_by_class_name("button--place-order-fast-track")
    #confirm_button.click()
    
    print('Order submission screenshot saved as "OrderSubmission.png" in repository location')                                                              #order completion code
    driver.get_screenshot_as_file('OrderSubmission.png')
    sms('Bot Program executed succesfully for '+title.text)
    end=input(title.text+' program executed succesfuly hit enter to exit browser or type retry to start bot again:')                                                    #if program ran all the way through will print program ran succesfully
    if end=='retry':
                bot()

此 returns 错误“在请求上下文之外工作。

这通常意味着您试图使用需要的功能 一个活跃的 HTTP 请求。请参阅有关测试的文档 有关如何避免此问题的信息。"

我查看了烧瓶文档并看到了这个 “这通常应该只在测试需要活动请求的代码时发生。一种选择是使用测试客户端来模拟完整请求。或者您可以在 with 块中使用 test_request_context() ,并在其中运行的所有内容该块将有权访问请求,其中填充了您的测试数据。"

但我不太明白它在说什么以及如何将其实现到我的代码中

这里是 Twilio 开发人员布道者。

您的第一个函数 sms_reply,看起来好像设置为 receive an incoming webhook from Twilio when an SMS message is sent to your Twilio phone number。该函数接收传入的 HTTP 请求,检查消息正文,构造回复并returns它作为对请求的响应。

问题是您随后试图在您的第二段代码中调用该函数,该函数期望响应 HTTP 请求,而它似乎根本没有与之相关的 HTTP 请求。

如果您想要 list messages that you have received or send a message,您可能会发现您想要使用 Twilio REST API。我不确定您的代码到底想做什么,所以我不能说得更具体了。