Search code examples
javascriptnode.jsbotframeworkmicrosoft-teamsamazon-lex

Amazon Lex and BotFramework integration TypeError: Cannot perform 'get' on a proxy that has been revoked at Response


I was doing a proof of concept trying to integrate BotFramework with Amazon lex and finally integrate the bot to Microsoft teams channel. The AWS-SDK is used to call the Amazon Lex bot.

async callLex(context) {
    let msg 
    var lexruntime = new AWS.LexRuntime();
    const params = {
         botAlias: 'tutorialbot',
         botName: 'TutorialBot',
         inputText: context.activity.text.trim(), /* required */
         userId: context.activity.from.id,
         //inputStream: context.activity.text.trim()
    }

    await lexruntime.postText(params, function(err,data) {
        console.log("Inside the postText Method")
        if (err) console.log(err, err.stack); // an error occurred
        else {
            console.log(data)
            msg = data.message
            console.log("This is the message from Amazon Lex" + msg)
            context.sendActivity(MessageFactory.text(msg));
            //turnContext.sendActivity(msg);
        }

        console.log("Completed the postText Method")
    })

    return msg; 
}

The response from Lex is received and When i try to return the same response back context.sendActivity(MessageFactory.text(msg)) in the callback function to BotFramework throws an error

Blockquote TypeError: Cannot perform 'get' on a proxy that has been revoked at Response. (E:\playground\BotBuilder-Samples\samples\javascript_nodejs\02.echo-bot\lexbot.js:93:25) at Request. (E:\playground\BotBuilder-Samples\samples\javascript_nodejs\02.echo-bot\node_modules\aws-sdk\lib\request.js:369:18) at Request.callListeners (E:\playground\BotBuilder-Samples\samples\javascript_nodejs\02.echo-bot\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (E:\playground\BotBuilder-Samples\samples\javascript_nodejs\02.echo-bot\node_modules\aws-sdk\lib\sequential_executor.js:78:10)

It seems once the message is sent to Lex, the proxy that the bot uses is no longer available. Can you give some pointers on how to fix this.

This is the calling code invoking the async function callLex

class TeamsConversationBot extends TeamsActivityHandler {
    constructor() {
        super();
        this.onMessage(async (context, next) => {
            TurnContext.removeRecipientMention(context.activity);
            var replyText = `Echo: ${ context.activity.text }`;
                    
            await this.callLex(context)
          
            console.log("After calling the callLex Method")
         
            await next();
        });

        this.onMembersAddedActivity(async (context, next) => {
            context.activity.membersAdded.forEach(async (teamMember) => {
                if (teamMember.id !== context.activity.recipient.id) {
                    await context.sendActivity(`Hi, I'm a TutorialBot. Welcome to the team ${ teamMember.givenName } ${ teamMember.surname }`);
                }
            });
            await next();
        });
    }

Solution

  • This error message always means you have not awaited something that should be awaited. You can see the following line in your constructor:

    await context.sendActivity(`Hi, I'm a TutorialBot. Welcome to the team ${ teamMember.givenName } ${ teamMember.surname }`);
    

    This should imply to you that sendActivity needs to be awaited, and yet you are not awaiting it in your postText callback:

    await lexruntime.postText(params, function(err,data) {
        console.log("Inside the postText Method")
        if (err) console.log(err, err.stack); // an error occurred
        else {
            console.log(data)
            msg = data.message
            console.log("This is the message from Amazon Lex" + msg)
            context.sendActivity(MessageFactory.text(msg));
            //turnContext.sendActivity(msg);
        }
    
        console.log("Completed the postText Method")
    })
    

    You are awaiting the call to postText itself, but that doesn't do anything because postText returns a request and not a promise. And you may have noticed that you can't await anything in the callback because it's not an async function. It seems the Lex package is callback-based and not promise-based, which means it's hard to use it with the Bot Framework since the Bot Builder SDK is promise-based.

    You may want to call the PostText API directly using a promise-based HTTP library like Axios: use await inside callback (Microsoft Bot Framework v4 nodejs)

    Alternatively you can try creating your own promise and then awaiting that: Bot Framework V4 - TypeError: Cannot perform 'get' on a proxy that has been revoked