Make a call to Amazon LEX asynchronously

Viewed 117

I have working code to call Amazon Lex and get a response from a bot. Here is a snippet of the code:

client = boto3.client(
    'lexv2-runtime', 
    region_name=region_name, 
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key
)

    response = client.recognize_text(
        botId=bot_id,
        botAliasId=bot_alias_id,
        localeId='en_US',
        sessionId=session_id,
        text=user_message,
    )

I now want to make this call be async. How do I achieve that using boto3 or any other python library that is supported by Amazon Lex

2 Answers

As I understand it RecognizeTextAsync is only available in the DotNet SDK. It is, therefore, not available in the Python, Java or JavaScript SDK's.

You would need to wrap your boto3 call with an asynch library. I have no experience with the below, but it might be the answer to solving your problem.

https://pypi.org/project/aioboto3/

Got this working with aiboto3

    async with session.client('lexv2-runtime', region_name=region_name, aws_access_key_id=access_key_id,
                              aws_secret_access_key=secret_access_key) as client:
        response = await client.recognize_text(
            botId=bot_id,
            botAliasId=bot_alias_id,
            localeId='en_US',
            sessionId=session_id,
            text="hi",
        )
Related