Search code examples
htmlnode.jsbotframeworkquery-stringazure-bot-service

Passing query strings from bot to html page


In Microsoft Bot Framework - botbuilder v.3.15.0

When opening url from bot without any query strings - it perfectly works - and opens external url (previously defined var urlExt, or just plain 'https://some.html') when clicking on a button in the bot... - in Bot Framework Emulator, Azure Web Chat, Messenger & Telegram - perfectly ok.

lib.dialog('/', [
  function (session, args) {
    args = args || {};
    // var pkey1 = 'fdsa'; // dummy variable, url with querystring with this dummy works ok on all channels!
    // var rkey1 = 'asdf'; // dummy variable, url with querystring with this dummy works ok on all channels!

    var pkey1 = session.logger.address.conversation.id;
    var rkey1 = session.logger.address.user.id;
    console.log(pkey1); // correctly shows conversation id in console locally
    console.log(rkey1); // correctly shows user id in console locally

    var urlMap = `https://mymap.azurewebsites.net/?cid=${pkey1}&uid=${rkey1}`;

    var urlExt = encodeURI(urlMap);

    setTimeout(function () {

      var addressCard = new builder.HeroCard(session)
        .title('address_title')
        .subtitle('address_subtitle')
        .images([
            new builder.CardImage(session)
                .url('https://somedb.blob.core.windows.net/images/ab_small.png') 
                .alt('Here comes some pic')
        ])
        .buttons([
            builder.CardAction.openUrl(session, urlExt, 'Just do it!')
        ]);

      session.send(new builder.Message(session)
        .addAttachment(addressCard));

    }, 1000)

  },

  function (session, results) {    
  // some further code
  }

]);

But when you try to insert query string into urlExt - by taking it's parameters conversation.id and user.id from 'session' - by making variables who take values of conversation.id and user.id from 'session' and then inserting those variables into urlExt (either by '+' concatenation, or by `` ${} method) it works locally in Emulator - but not on Azure Web Chat or Messenger or Telegram.

When I try to find the reason for this problem I tried not to grab conversation.id or user.id from 'session', but just insert some dummy variables with text to forward them into html page by inserting those variables as part of the query string - IT WORKS...:(

Really strange, the problem seems to be with getting conversation.id & user.id from 'session' to variables at Azure Portal.

But why does it perfectly work locally on my laptop?

Logs on Azure say that:

TypeError: Cannot read property 'conversation' of undefined

I have looked in stackoverflow - there is ZERO info about it, I looked at the various code at GitHub - NO ANSWERS...

Please, hint, or help...


Solution

  • The session logger is only enabled on certain channels, which is why your code works in the emulator but not in Test in WebChat on Azure and Microsoft Teams. Instead of getting the conversation and user ids from the logger attribute, access them from the message property.

    var pkey1 = session.message.address.conversation.id;
    var rkey1 = session.message.address.user.id;
    

    Hope this helps!