add Dad Jokes sample

This commit is contained in:
Evgeny
2022-01-27 09:03:35 +03:00
parent 740e4f9fd5
commit 7566e4e275
2 changed files with 42 additions and 2 deletions

View File

@@ -342,6 +342,28 @@ function on_network_result(result)
end end
``` ```
Another example:
```
{
"attachments": [
{
"fallback": "Whats Forest Gumps Facebook password? 1forest1",
"footer": "<https://icanhazdadjoke.com/j/7wciy59MJe|permalink> - <https://icanhazdadjoke.com|icanhazdadjoke.com>",
"text": "Whats Forest Gumps Facebook password? 1forest1"
}
],
"response_type": "in_channel",
"username": "icanhazdadjoke"
}
```
To extract "text" value we need to use this code:
```
joke = ajson:get_value(result, "object array:attachments object:0 string:text")
```
Please note that the last element of the line should always be an instruction for extracting primitive data types: Please note that the last element of the line should always be an instruction for extracting primitive data types:
* `string:name` * `string:name`
@@ -349,8 +371,6 @@ Please note that the last element of the line should always be an instruction fo
* `double:name` * `double:name`
* `boolean:name` * `boolean:name`
Also, instead of `object`, you can use `array` if the JSON contains an array.
To summarize: ajson works well (and very fast) when you need to retrieve one or two values. If you need to get a large amount of data (or all data) from JSON, then it is better to use the `json.lua` library (see below). It turns JSON into a set of easy-to-use nested Lua tables. To summarize: ajson works well (and very fast) when you need to retrieve one or two values. If you need to get a large amount of data (or all data) from JSON, then it is better to use the `json.lua` library (see below). It turns JSON into a set of easy-to-use nested Lua tables.
## Other ## Other

View File

@@ -0,0 +1,20 @@
-- name = "Dad jokes"
-- description = "icanhazdadjoke.com"
-- type = "widget"
-- author = "me"
-- version = "1.0"
function on_resume()
http:get("http://icanhasdadjoke.com/slack")
end
function on_network_result(result)
joke = ajson:get_value(result, "object array:attachments object:0 string:text")
ui:show_text(joke)
end
function on_click()
if joke ~= nil then
system:copy_to_clipboard(joke)
end
end