Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Copilot Studio - Bot Administration
Unanswered

My bot is in a Custom Canvas, how to pass dynamic values to bot.Variables via query parameters in the custom page's url?

(0) ShareShare
ReportReport
Posted on by

Previously, my customers used to receive a direct URL to our chatbot (not a custom canvas). That URL needs to have query string parameters to assign values to some bot variables. These variable values are dynamic; the URL is constructed on the fly in Power Automate specific to the customer and situation when it gets sent to the customer. This worked. My bot variables are properly configured to accept values from external sources.

Lexicon_0-1671601156978.png

 

 

But now we need the chatbot to start the conversation immediately. So I have used a custom canvas (index.html) following this instruction: https://learn.microsoft.com/en-us/power-virtual-agents/configure-bot-greeting#deploy-a-custom-canvas-for-your-bot. It works to start right away. But it is prompting for values for the bot variables.

 

So I need to combine the two. Start automatically, and also pull values from the URL parameters.

 

I add my query string parameters again at the end of the https://mycompany.com/index.html url and followed this instruction to add the bot variables to a custom canvas: https://learn.microsoft.com/en-us/power-virtual-agents/authoring-variables-bot#set-a-bot-variables-value-from-external-sources. I also add some code to pull variables from the URL. The full URL looks like this:

 

/index.html?VarWorkOrder=1234&VarItem=ball&VarTrigger=5de00478

 

 

It only works 1 out of over 7 times, I have to keep refreshing my page many times before it finally starts the conversation with the bot variables pulling values from the URL parameters. Most times it doesn't start at all, and occasionally it starts with the prompts for bot variables.

 

My custom canvas HTML code is below. The bot variables I have are

  • VarWorkOrder
  • VarItem
  • VarTrigger

 

It looks like the two dispatches in my store const are not reliably running together. Can someone advice please how the code should be written?

 

<!DOCTYPE html>
 <html>
 <head>
 <title>My Web Chat</title> 
 </head>
 <body>
 <div>
 <div class="heading">
 <h1>My Chatbot</h1>
 </div>
 <div id="webchat" role="main"></div>
 </div> 
 <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
 <script>
 const styleOptions = {hideUploadButton: true};

 const params = new URLSearchParams(document.location.search);
 const varWorkOrder= params.get("VarWorkOrder");
 const varItem= params.get("VarItem");
 const varTrigger= params.get("VarTrigger");

 var BOT_ID = "redacted bot ID because duh"; 
 var theURL = "https://powerva.microsoft.com/api/botmanagement/v1/directline/directlinetoken?botId=" + BOT_ID;

 const store = window.WebChat.createStore(
 {},
 ({ dispatch }) => next => action => {
 if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {
 dispatch({
 type: "WEB_CHAT/SEND_EVENT",
 payload: {
 name: "pvaSetContext",
 value: {
 "VarWorkOrder": varWorkOrder,
 "VarItem": varItem,
 "VarTrigger": varTrigger
 }
 },
 });
 dispatch({
 type: "DIRECT_LINE/POST_ACTIVITY",
 meta: { 
 method: "keyboard",
 },
 payload: {
 activity: {
 channelData: {
 postBack: true,
 },
 name: 'startConversation',
 type: "event"
 }
 },
 });
 }
 return next(action);
 }
 );
 fetch(theURL)
 .then(response => response.json())
 .then(conversationInfo => {
 window.WebChat.renderWebChat(
 {
 directLine: window.WebChat.createDirectLine({
 token: conversationInfo.token,
 }),
 store: store,
 styleOptions: styleOptions
 },
 document.getElementById('webchat')
 );
 })
 .catch(err => console.error("An error occurred: " + err));
 </script>
 </body>
 </html>

 

 

  • nikviz Profile Picture
    242 on at
    Re: My bot is in a Custom Canvas, how to pass dynamic values to bot.Variables via query parameters in the custom page's url?

    Hi @Anonymous @Lexicon 

     

    You've to use the promise function. Use the below section, add the context variables in PVASetContext (in this example, i have added a variable to capture page URL)

     

     

     

    const store = window.WebChat.createStore({},
    	({
    		dispatch
    	}) => next => action => {
    		if (action.type === "DIRECT_LINE/CONNECT_FULFILLED") {
    			new Promise(function(resolve) {
    				dispatch({
    					type: 'WEB_CHAT/SEND_EVENT',
    					payload: {
    						name: 'pvaSetContext',
    						value: {
    							pageURL: window.location.href,
    						},
    					}
    				});
    				setTimeout(() => resolve(1), 1000);
    			}).then(function(result) {
    				dispatch({
    					meta: {
    						method: "keyboard",
    					},
    					payload: {
    						activity: {
    							channelData: {
    								postBack: true,
    							},
    							name: "startConversation",
    							type: "event",
    						},
    					},
    					type: "DIRECT_LINE/POST_ACTIVITY",
    				})
    			});
    		}
    		return next(action);
    	}
    );
    
    fetch(theURL)
    	.then(response => response.json())
    	.then(conversationInfo => {
    		window.WebChat.renderWebChat({
    				directLine: window.WebChat.createDirectLine({
    					token: conversationInfo.token,
    				}),
    				// onTelemetry: 
    				store: store,
    				styleOptions: styleOptions
    
    			},
    			document.getElementById('webchat')
    		);
    	})
    	.catch(err => console.error("An error occurred: " + err));

     

     

     

  • Community Power Platform Member Profile Picture
    on at
    Re: My bot is in a Custom Canvas, how to pass dynamic values to bot.Variables via query parameters in the custom page's url?

    Hello,

     

    I'm having the same issue. When I remove the code that make chatbot start the conversation immediately, parameters are directly read by the bot. But when the two pieces of code are here, bot is prompting for values for the bot variables and params are not filled automatically.

    Is there any solution please?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Copilot Studio - Bot Administration

#1
stampcoin Profile Picture

stampcoin 2

#1
Meekou Profile Picture

Meekou 2 Super User 2025 Season 1

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 2 Super User 2025 Season 1

Overall leaderboard

Featured topics