- - - - - - - - - - - - 编辑 - - - - - - - - - - - -
我仍然不确定如何让 request.form['Body'] 正常工作。我可能没有正确使用此请求,但我不确定如何使用它。一个建议是做一个听众,但我不确定从哪里开始。
所以我想现在是:
1)我在哪里完全搞砸了?2)关于如何成为听众的任何建议?
这是我的设置的骨架:
from flask import Flask, request, redirect
import twilio.twiml
from twilio.rest import TwilioRestClient
from sys import exit
twil_number = "XXXXXXXXX"
sid = "XXXXXXXXXXXXXXXXXXXX"
token = "XXXXXXXXXXXXXXXXX"
tos = "XXXXXXXXXX"
client = TwilioRestClient(sid,token)
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
##### get the SMS ####
def smsGet():
    try:
        action = request.form['Body']
    except RuntimeError:
        action = raw_input("> ")
    return action.lower()
### Snd the SMS ####
def smsSend(bods):
    client.sms.messages.create(to=tos, from_=twil_number, body= bods)
class TestClass(object):
    def doStuff(self):
        smsSend("Intro Text")
        ## I don't know why this is buggering up. ###
        go = smsGet()
        if go == "yes":
            smsSend("yes")
            exit(1)
        else:
            pass
            exit(1)
a = TestClass()
a.doStuff()
#### running the app ###
if __name__ == "__main__":
    app.run(debug=True)
----------------------- 年长 ----------- 我是用 twilio/python 制作文字冒险游戏。没有数据库或任何东西,它只是一个基于 python 的文本冒险,我在本地从 shell 运行。
所有发送短信的东西都很好,但我怎么得到这样的东西:
request.form['Body'].lower()
像这样工作:
raw_input("> ").lower()
到目前为止,我尝试过的大多数事情都只是让 Flask 抛出这个错误:
RuntimeError('在请求上下文之外工作')
所以对于上下文。我的很多场景都是这样设置的:
class Hallway(Scene):
    def enter(self):
        hall = "You pause in the Hallway. You could: Go to the bedroom. Go to the kitchen to see if there's food. Check on your human in the office."
        send_sms(hall)
        #action = raw_input("> ").lower() 
        #would like this to work like raw _input()
        action = request.form['Body'].lower() 
        if action == "kitchen":
            return 'kitchen'
        elif action == "bedroom":
            return 'bedroom'
        elif action == "office":
            return 'office'
        else:
            nope = "But I don't want to hang in the hallway..."
            send_sms(nope)
            return 'hallway'
而 send_sms() 就是这样:
def send_sms(bods):
    client.sms.messages.create(body=bods,to=tos,from_=froms)
关于我完全搞砸的地方有什么建议吗?
谢谢!:)