如何使用Flask会话在Twilio中分离应用程序用户之间的会话?

2024-04-19 19:50:19 发布

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

在使用ngrok测试我的应用程序时,我的两个用户同时发了Twilio号码,他们在同一个会话中登陆,因此我使用的任何计数器都会因各自的用户体验而出错。我如何才能确保每个用户短信号码使用该应用程序将有自己的独立体验?在Flask会话中创建动态密钥的诀窍是什么?你知道吗

下面是一些代码

from flask import Flask, request, session
from twilio.twiml.messaging_response import MessagingResponse

SECRET_KEY = 'a secret key'
app = Flask(__name__)
app.config.from_object(__name__)


@app.route("/sms", methods=['GET','POST'])
def sms_logic():
    try:
        # Increment the counter
        counter = session.get('counter',0)

        # Initiate conversation + Save the new counter value in the session
        body = request.values.get('Body', None)
        counter += 1
        session['counter'] = counter

        # start our TwiML response
        resp = MessagingResponse()

        if counter == 1 and body.lower() == 'target_string':

            resp.message('response 1')

        elif counter == 1 and body.lower() != 'target_string':

            resp.message('Unknown entry. Please try again.')
            session.clear()

        else:

            resp.message('response 2 ')
            session.clear()

        #print(counter)
        return(str(resp))
    except Exception as e:

        pass



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

我正在清除会话以重置计数器,而不是等待4小时等待会话过期,特别是如果用户希望再次通过应用程序进行另一项输入。你知道吗

提前谢谢!你知道吗


Tags: the用户namefromapp应用程序flaskmessage