BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

How to read values from C#, if I am passing values in query-string to BOT (Web Chat) in iFrame Tag?

Open virendrak opened this issue 9 years ago • 2 comments

I have created a BOT using C#, which I have added in my SharePoint Page in Script Editor Web Part. I followed this link to create Web Chat -https://github.com/billba/botchattest

Page 1 (SharePoint Page, with Script Editor Web Part):

Added a Script Editor WebPart in SharePoint Page (Page 1), where I can get the user details Example: UserId and UserName , which I am passing the to the BOT page (Page 2).

PFB: Code/Html of this page....

	<div id "chat-bot" class="popup-messages">
	<iframe id="botchat" width="480" height="450" src="" >
	</iframe>
	
<script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript"></script>		
<script type="text/javascript">
	var userid = _spPageContextInfo.userId;
	var loginname = _spPageContextInfo.userLoginName;
	var username = 'You';
	
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept": "application/json;odata=verbose" };
	
    $.ajax({
        url: requestUri,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: onSuccess,
        error: onError
    });
    function onSuccess(data, request) {
        var Logg = data.d;
        
			//get login name
			var loginName = Logg.LoginName.split('|')[1];
			//alert(loginName);
        
			//get display name
			
			username = Logg.Title;
			//Update IFrame SRC
			document.getElementById("botchat").src =
             "/chatbot/SiteAssets/Index.aspx?s=<directline secret>&username="+username+"&userid="+ userid+"&loginname="+loginname;
    }
    function onError(error) {
        alert("error");
    }     
</script>
</div>

Page 2 (Index.aspx)

Here, I am able to read the QueryString, at JS side. This user name is visible in Bot UI. image

PFB: Code/Html of this page....

<link href="botchat.css" rel="stylesheet" />
      <div id="BotChatGoesHere"></div>
      <script src="botchat.js"></script>
      <script>
          var params = {};
          location
              .search
              .substring(1)
              .split("&")
              .forEach(function (pair) {
                  var p = pair.split("=");
                  params[p[0]] = p[1];
              });

          var botConnection = new BotChat.DirectLine({
              secret: params['s'],
              token: params['t']
              }, params["domain"]
          );

          var user = {
              id: params['userid'] || 'userid',
              name: params["username"] || 'username'
              };
          
          var bot = {
              id: params['botid'] || 'botid',
              name: params["botname"] || 'botname'
          };

          BotChat.App({
              botConnection: botConnection,
              user: user,
              bot: bot,
          }, document.getElementById("BotChatGoesHere"));
      </script>

Could you please let me know, how I can get/show/access these User Id/Username values in C#, so that I can show username in welcome message, or I can perform actions according to logged in user.

When I am using Request.Url or Request.Uri in C#, am getting the azure url (http://xyz.azurewebsites.net/), where I have published this BOT.

Thanks, Viren

virendrak avatar Jan 10 '17 09:01 virendrak

My question is similar to https://github.com/Microsoft/BotFramework-WebChat/issues/273 Basically we want to get/access these values from C#

virendrak avatar Jan 10 '17 10:01 virendrak

See my answer for #273.

billba avatar Jan 10 '17 19:01 billba