[How To] request data from web with command

First we create a custom Variable
You can find this in Registry -> Custom Variables

Give your variable a Name
Set Response after variable set to Default
and Type to Script

now the fun Part begin
we need a simple script to request the data from web

Lets Request current datetime from
http://worldtimeapi.org/

our expected response is in JSON

{
  "week_number": 15,
  "utc_offset": "+02:00",
  "utc_datetime": "2020-04-09T10:12:41.307885+00:00",
  "unixtime": 1586427161,
  "timezone": "Europe/Berlin",
  "raw_offset": 3600,
  "dst_until": "2020-10-25T01:00:00+00:00",
  "dst_offset": 3600,
  "dst_from": "2020-03-29T01:00:00+00:00",
  "dst": true,
  "day_of_year": 100,
  "day_of_week": 4,
  "datetime": "2020-04-09T12:12:41.307885+02:00",
  "client_ip": "92.195.123.123",
  "abbreviation": "CEST"
}

so our return will be data.datetime

here is our Script

Code:

return myExampleFunction('http://worldtimeapi.org/api/ip');    

async function myExampleFunction(param){
    const response = await url(param, { method: 'GET' });
    return response.data.datetime;
}

Now we have a custom Variable that updates its content to current datetime
so we need a Command to trigger this and write return to chat

Manage -> Custom commands

Name your command and set as return the name of our previous created Variable

1 Like