$http.check
This method checks whether an HTTP request to one of several external URLs returns 200 OK.
Syntax
The method accepts two arguments.
Argument | Type | Description |
---|---|---|
method | String | Request HTTP method. The preferred ones are GET and HEAD . |
urls | Array of strings | The external URLs to be checked. |
$http.check("HEAD", ["https://example.com", "https://mirror.example.org"]);
The method returns an array with one element: the first URL that returned 200 OK.
If none of them returned 200 OK, the method returns [null]
.
info
There is also a similar method called $http.checkUrls
.
It has the same arguments as $http.check
, but returns an array with all URLs that returned 200 OK.
How to use
This method can be helpful when integrating the bot with audio streaming services. You can configure the main service URL as well as its mirrors in the config, and use the first available URL for streaming.
init:
$global.STREAM_URLS = [
"https://example.com/rock-128.mp3",
"https://example.org:13000/rock-128.mp3",
"https://mirror.example.org:8000/rock-128.mp3"
];
theme: /
state: PlayRadio
q!: * (play/turn on) [the] radio *
script:
$temp.urls = $http.check("GET", $global.STREAM_URLS);
if: typeof $temp.urls[0] === "string"
audio: {{ $temp.urls[0] }}
else:
a: I had no luck connecting to the radio station. Please try again.