Using variables I define in a urlfetch in eval?

This seems like it should be easy, but has anyone used variables they define as part of the URL in urlfetch? I can’t seem to get it to work.
Something like:
const topic=“12”;
response = $(urlfetch json$(topic)&difficulty=easy&type=multiple&encode=url3986);


I also tried putting an eval nested in the urlfetch, but that doesn’t seem to embed the string.
$(eval response =$(urlfetch json $(eval topic = ‘$(1)’; my_url = “"+topic+"&difficulty=easy&type=multiple&encode=url3986”; my_url)); s = JSON.stringify(response); )

Is the problem here that I don’t have access to $(1) from the embedded eval?

I’ve tried many different methods to access ‘topic’, but none seem to work. Help?

Nightbot variables evaluate inner to outer and variable nesting can’t exceed 2 levels, so in example 1 the urlfetch is evaluated and stored as a JS string before the eval runs the JS and in example 2 the JS string '$(1)' is parsed exactly as is.

I’d edit the command to output a URL then set the command an alias of another command that runs a urlfetch on the input.

1 Like

That approach worked! Here are some details.

I have a command to fetch the content that takes a string as input.

Then, I have another command (which is what the user will call) which formats the string and calls the first command via an Alias.

Thank you @RokettoJanpu !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.