2

I am using microsoft bot framework and trying to integrate it with external HTTP calls.

However when i invoke the BotWorker.say in the handler BotKitConversation's ask i start getting

 (node:5711) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot perform 'get' on a proxy that has been revoked

Below is my code

In my handler function's callback i get some values returned from external function. Then i try to perform bot.say on the response returned and i recieve the above mentioned error.

 myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async (response, convo, bot) => {
            await YoutubeHelper.getChannel(response, convo,async function(channels){
            console.log("value returned " + channels.length);
            try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {

                await bot.say('No items found.');
              } else {
                await bot.say('This items\'s ID is %s. Its title is \'%s\', and ' ,
                            channels[0].id,
                            channels[0].snippet.title
                            );
              }
            }catch (err) {
                console.log('error occurred' , err);

            }
            });
        }
    }

],  {key: 'name'});
}

where myDialog is an object of BotkitConversation.

Below is the code for my external utility class

 /**
 * Lists the names and IDs of up to 10 files.
 *
 * 
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot")

const getChannel = function getChannel(searchTerm, convo,callback) {


    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});


    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      callback(channels);
    });
    //

  }

  module.exports ={
    getChannel
}

I found this document regarding the error . I am coding according to the guidelines mentioned.

Best Regards,

Saurav

saurav
  • 5,388
  • 10
  • 56
  • 101

2 Answers2

0

Try to put first await function in a try-catch block:

myDialog.ask('What would like to hear?', [
  {
    pattern: '.*',
    handler: async (response, convo, bot) => {
      try {
        await YoutubeHelper.getChannel(response, convo, async function (channels) {
          console.log("value returned " + channels.length);
          try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {
              await bot.say('No items found.');
            }
            else {
              await bot.say('This items\'s ID is %s. Its title is \'%s\', and ',
                channels[0].id,
                channels[0].snippet.title
              );
            }
          }
          catch (err) {
            console.log('error occurred', err);

          }
        });
      }
      catch (error) {
        console.log('----::error::----', error)
      }
    }
  }

], { key: 'name' });
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • thanks for the reply ...i assume you wanted to check the error ....but doing this is giving me the same error – saurav Apr 12 '20 at 15:22
  • you get `UnhandledPromiseRejectionWarning` again?! Are you sure the problem is this function?! @saurav – Saeed Apr 12 '20 at 15:26
  • error comes when i invoke bot.say function...if i remove the call to bot.say then there is no error... – saurav Apr 12 '20 at 15:28
  • did you check [this](https://github.com/Microsoft/botbuilder-js/issues/606) or [this](https://stackoverflow.com/questions/52969703/bot-framework-v4-typeerror-cannot-perform-get-on-a-proxy-that-has-been-revo)?? does `bot.say` return promise? – Saeed Apr 12 '20 at 15:29
  • yes it does https://github.com/howdyai/botkit/blob/77e309b9522f1c654dcc68332c11d80fae3d2a53/packages/botkit/src/botworker.ts#L99 – saurav Apr 12 '20 at 17:32
  • 2
    Ok. Follow [this](https://github.com/howdyai/botkit/issues/1951) issue or create new one in its repo. I can't help here. Good luck. @saurav – Saeed Apr 12 '20 at 18:14
0

Problem was with my helper class which was not an awaitable function.

I made changes in my helper function to make it return a promise and it started working.

Below is the updated code

 function askMusicPreferences(answer, convo, bot){


myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async(response, convo, bot, message) => {
            try {
            var channels = await YoutubeHelper.getChannel(response);

            if (channels.length == 0) {

               await bot.say('No items found.');
            }
               else {
                await bot.say(`This items\'s ID is ${channels[0].id}. Its title is ${channels[0].snippet.title}`);
              }

            }catch (error){
                console.log( 'error occurred ', err);
            }


       // }
        // catch (err){
        //     console.log('error occurred', err);
        // }

    }
}
],  {key: 'name'});
}

My helper class

    /**
 * Lists the names and IDs of up to 10 files.
 *
 * 
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot");


const getChannel = function getChannel(searchTerm) {
    return new Promise(function(resolve,reject) {

    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});


    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        reject(err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      resolve(channels);
      //callback(channels);
    });
    //
  }); 

  }

  module.exports ={
    getChannel
}
saurav
  • 5,388
  • 10
  • 56
  • 101