Twilio短信/Python,尝试捕获短信内容

2024-06-25 23:42:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在这里的第一篇文章,如果有任何不正确的地方,我很抱歉。我一直试图通过twilio找回一条短信的尸体。最终的目标是有一个基本的聊天机器人,我可以在文本上使用,但我不想在一个Python文件中编写所有的代码。这就是拔掉它的目的。你知道吗

我想知道下一步该去哪里。 从以下错误开始:

Traceback (most recent call last):
File "main.py", line 24, in <module>
print (sms_reply().message_body)
File "/home/pi/PythonScripts/BasicSMSBot/SMSIncoming.py", line 18, in 
sms_reply
message_body = request.form['Body']
File "/usr/local/lib/python3.4/dist-packages/werkzeug/local.py", line 347, 
in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/local/lib/python3.4/dist-packages/werkzeug/local.py", line 306, 
in _get_current_object
return self.__local()
File "/usr/local/lib/python3.4/dist-packages/flask/globals.py", line 37, in 
_lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

当它缝合的时候,我想把尸体拉出来的方式有点不对劲。你知道吗

以下是SMSIncoming当前的代码:

#importing nessisary scripts and files
import os
#import SMSOutgoing
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse


#initilizing the global user variable
#glo_user_var = (' T ');

#initilizing app
app = Flask(__name__)

@app.route("/sms", methods=["GET", "POST"])
def sms_reply():
    resp = MessagingResponse()
    #body = request.values.get('Body', None)
    message_body = request.form['Body']
    #for debuging the SMS instercept
    #saveFile = open('bodyfile.txt', 'w')
    #saveFile.write(body)
    #saveFile.close()

    resp.message("Testting the SMS responce")
    return str(resp)
    return str(body)



    #lets main app process run
    if __name__ == "__main__":
           app.run(debug=True)

这是当前的代码主.py你知道吗

   #place all imports and scripts to be run
   import os
   import SMSOutgoing
   from SMSIncoming import sms_reply
   #from SMSIncoming.py import app, sms_reply
   import time



   #call up ngrok and make server on port 5000
   #os.system("./ngrok http 5000");


   #start running the SMSIncoming app
   #if __name__ == "__main__":
   #       app.run(debug=True)

   #       have to start apps separately!  #


   #this block will handle incoming SMS (User Input)
   while True:
   #       global glo_user_var
          print (sms_reply().message_body)
          #file = open('test_user_input.txt', 'w')
          #file.write(glo_user_var)
          #file.close()
          time.sleep(1)

里面的一切主.py已注释掉或已准备好进行测试。你知道吗

我想要打印身体的原因是为了确保它到达我想要的地方。之后,我将使用它作为用户的输入来定义回复。你知道吗

我还使用ngrok作为我的httpwebhook。你知道吗


Tags: inpyimportappmessagemainrequestlocal
1条回答
网友
1楼 · 发布于 2024-06-25 23:42:11

Twilio开发者福音传道者。你知道吗

首先,您遇到的问题是,您正在将flask应用程序分解为多个文件,并尝试每秒调用一次同样是路由的函数。因此,您肯定需要从while True循环开始。你知道吗

接下来,我了解到,如果您在一个文件中构建整个应用程序,您会发现事情可能变得非常复杂。然而,你目前过早地优化。我建议您首先在一个文件中运行它,然后将应用程序重构为更易于管理的块。你知道吗

作为记录,你做的是正确的事情得到短信的正文。Twilio message webhook将把主体作为表单参数Body发送,这样您就可以在对请求的响应中使用

message_body = request.form['Body']

一旦到了那个阶段,请先从building larger applications上的Flask看一下这个文档,然后再看building modular applications with Blueprints上的信息。你知道吗

相关问题 更多 >