Giter VIP home page Giter VIP logo

Comments (3)

tdurnford avatar tdurnford commented on May 21, 2024 1

@kkhzz1004

I'm not entirely sure what you are trying to accomplish by using sessions, but I came up with a solution using User State in the BotFramework that might work for you.

Right below where the memory is initialized in the main.py file, uncomment user_state = UserState(memory). It should be on line 32 in the Rich-Cards-Bot example, but it might be further down since I believe you've imported a few packages. You also need to uncomment ADAPTER.use(user_state) on line 35.

Then in the handle_message function, you can either access the User_code from the user_state or create a new User_code in the user_state if it doesn't exist. Your code should look something like this:

# Create MemoryStorage, UserState and ConversationState
memory = MemoryStorage()
user_state = UserState(memory)
...
# Register both State  middleware on the adapter.
ADAPTER.use(user_state)
ADAPTER.use(conversation_state)

...

async def handle_message(context: TurnContext) -> web.Response:
    # Access the state of the user
    user = await user_state.get(context)

    if not hasattr(user, 'User_code'):
        user.User_code = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(16)]) 
    
    print(user.User_code)

    ...

    return web.Response(status=202)

...

async def messages(req: web.web_request) -> web.Response:
    body = await req.json()
    activity = Activity().deserialize(body)
    auth_header = req.headers['Authorization'] if 'Authorization' in req.headers else ''
    try:
        return await ADAPTER.process_activity(activity, auth_header, request_handler)
    except Exception as e:
        raise e

app = web.Application()
app.router.add_post('/', messages)

try:
    web.run_app(app, host='localhost', port=PORT)
except Exception as e:
    raise e

Using the user state, the User_code stays consistent between messages, and the output looks like this:

User_code: BGjvg9FkUCJeUoOa
User_code: BGjvg9FkUCJeUoOa
User_code: BGjvg9FkUCJeUoOa

If this solution doesn't work for what you are trying to do, please let me know and I will look deeper into the issue you are having with sessions.

from botbuilder-python.

kkhzz1004 avatar kkhzz1004 commented on May 21, 2024

Thanks for your reply @tdurnford. simply this is what i want. so.... by using this, we could save user information right? and its for multi user that's all. Thanks alot! and i think this is what @varunreddycs asked for state data. if we have example code with this it will be better for the starter. if you don't mind i would like to ask one thing more that how can i use adaptive_card button data as an input? example i have a Card with button value yes or no. on javascript or c# botframework i could see their value can be in the chat text, or as input value. i'm waiting for you great answer :)

from botbuilder-python.

tdurnford avatar tdurnford commented on May 21, 2024

@kkhzz1004

Yes, you can store user information such as name and id in the user state and the data will be preserved for each user between conversations.

In regards to your second question, the Microsoft Bot Framework team prefers that how to questions be submitted on Stack Overflow. The official Bot Framework Github repos are the preferred platform for submitting bug fixes and feature requests. However, if you send me the link for your question on Stack Overflow, I will gladly take a look at it.

from botbuilder-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.