https://discord.gg/botpress logo
#🤝help
What im supposed to do inorder to fetch data from an external URL?
# 🤝help
w

witty-motherboard-16719

01/08/2024, 1:55 PM
hello fellow botfriends, how do I get parameters values from an URL and save those values as variables to use them in other nodes? for example, if the url is "google.com?name=james", i need to extract the "james" value and save it as a variable so that, for example, the first message of the bot would be "hello james!".
f

fresh-fireman-491

01/08/2024, 3:31 PM
document.addEventListener('DOMContentLoaded', function () { let currentUrl = window.location.href; console.log("Current URL: " + currentUrl); let url = new URL(currentUrl); let searchParams = new URLSearchParams(url.search); let name = searchParams.get('name'); if(name) { console.log("Hello " + name + "!"); } else { console.log("Name parameter not found in URL."); } });
Add that JavaScript to your website code.
And you will see something like this
w

witty-motherboard-16719

01/08/2024, 3:37 PM
thx for the help but the things is that im used the studio version, so basically i need to put this code in a node, i actually read in the docs that i can actually use Hooks to setup action before an event occours. But im trying to figure all out, im i missing something?
f

fresh-fireman-491

01/08/2024, 3:39 PM
You can use hooks to setup an action before an event but I don't think that you can read the url from the chatbot
w

witty-motherboard-16719

01/08/2024, 3:41 PM
so this is the real life scenario, an external file calls the bot via a redirect calling , now before the conversation starts i need to fetch the data in the url and store them(the urls are dynamic)
f

fresh-fireman-491

01/08/2024, 3:43 PM
From the website URL that the bot is on or do you have an URL in the redirect calling?
w

witty-motherboard-16719

01/08/2024, 3:45 PM
the bot is not in the same website, ill try to reformulate the issues: after the redirect, the user lands on the bot page, that page has the URL with the parameters I should fetch
f

fresh-fireman-491

01/08/2024, 7:58 PM
If you are trying to do something like this:
@quick-musician-29561 just fixed my problem with it so it now works if you want to try it out🛠️
Make sure to put in your own BotID, ClientID and WebhookID
w

witty-motherboard-16719

01/09/2024, 9:06 AM
ok thx, one more question where im supposed to put this script? cause as i said im using the cloud version so im actually working with nodes, should i put the function handleUrl inside an "execute code" block?
f

fresh-fireman-491

01/09/2024, 9:19 AM
I added an index.html file for you which is just for a template. You would have to add it to your own JavaScript on your website
w

witty-motherboard-16719

01/09/2024, 9:23 AM
so basically i need to add it on my client side? right? and after that how im supposed to fetch the url?
f

fresh-fireman-491

01/09/2024, 9:24 AM
Depends on how your website is setup but yes most likely.