Assist wikipedia skill

Hi,
I’d like to give assist a dynamic api url. The url should be based on stt slot. That’s what I tried with rest_command. It executes correctly but no output

rest_command:
  wiki_get_rawdata:
    url: "https://de.wikipedia.org/api/rest_v1/page/summary/{{ url }}"
    method: GET
    content_type:  'application/json; charset=utf-8'
intent_script:
  Wikipedia:
    action:
    - service: "rest_command.wiki_get_rawdata"
      data:
        url: "{{ name }}"
      response_variable: summary
    speech:
      text: "{{ summary }}"

custom sentence:

language: de
intents:
  Wikipedia:
    data:
      - sentences:
          - "suche {name}"
          - "was ist {name}"
        lists:
          name:
            wildcard: true

example output if rest called alone:

content:
  type: standard
  title: Hauskatze
  displaytitle: <span class="mw-page-title-main">Hauskatze</span>
  namespace:
    id: 0
    text: ""
  wikibase_item: Q146
  titles:
    canonical: Hauskatze
    normalized: Hauskatze
    display: <span class="mw-page-title-main">Hauskatze</span>
  pageid: 18747
  thumbnail:
    source: >-
      https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Hauskatze_langhaar.jpg/320px-Hauskatze_langhaar.jpg
    width: 320
    height: 239
  originalimage:
    source: https://upload.wikimedia.org/wikipedia/commons/8/8e/Hauskatze_langhaar.jpg
    width: 2288
    height: 1712
  lang: de
  dir: ltr
  revision: "243925950"
  tid: 10010336-f713-11ee-b1a9-92580edda682
  timestamp: "2024-04-10T08:19:34Z"
  description: domestizierte Katze
  description_source: central
  content_urls:
    desktop:
      page: https://de.wikipedia.org/wiki/Hauskatze
      revisions: https://de.wikipedia.org/wiki/Hauskatze?action=history
      edit: https://de.wikipedia.org/wiki/Hauskatze?action=edit
      talk: https://de.wikipedia.org/wiki/Diskussion:Hauskatze
    mobile:
      page: https://de.m.wikipedia.org/wiki/Hauskatze
      revisions: https://de.m.wikipedia.org/wiki/Special:History/Hauskatze
      edit: https://de.m.wikipedia.org/wiki/Hauskatze?action=edit
      talk: https://de.m.wikipedia.org/wiki/Diskussion:Hauskatze
  extract: >-
    Die Hauskatze ist die Haustierform der Falbkatze. Sie zählt zu den
    beliebtesten Heimtieren. In der Rassekatzenzucht werden oft nur die Tiere
    als Hauskatze bezeichnet, die keiner anerkannten Katzenrasse angehören.
  extract_html: >-
    <p>Die <b>Hauskatze</b> ist die Haustierform der Falbkatze. Sie zählt zu den
    beliebtesten Heimtieren. In der Rassekatzenzucht werden oft nur die Tiere
    als Hauskatze bezeichnet, die keiner anerkannten Katzenrasse angehören.</p>
status: 200

I have it working now, but how to extract values from json. Without extract it returns everything. When I add the key extract it returns empty. thats the json.

{"type":"standard","title":"Hauskatze","displaytitle":"<span class=\"mw-page-title-main\">Hauskatze</span>","namespace":{"id":0,"text":""},"wikibase_item":"Q146","titles":{"canonical":"Hauskatze","normalized":"Hauskatze","display":"<span class=\"mw-page-title-main\">Hauskatze</span>"},"pageid":18747,"thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Hauskatze_langhaar.jpg/320px-Hauskatze_langhaar.jpg","width":320,"height":239},"originalimage":{"source":"https://upload.wikimedia.org/wikipedia/commons/8/8e/Hauskatze_langhaar.jpg","width":2288,"height":1712},"lang":"de","dir":"ltr","revision":"243925950","tid":"10010336-f713-11ee-b1a9-92580edda682","timestamp":"2024-04-10T08:19:34Z","description":"domestizierte Katze","description_source":"central","content_urls":{"desktop":{"page":"https://de.wikipedia.org/wiki/Hauskatze","revisions":"https://de.wikipedia.org/wiki/Hauskatze?action=history","edit":"https://de.wikipedia.org/wiki/Hauskatze?action=edit","talk":"https://de.wikipedia.org/wiki/Diskussion:Hauskatze"},"mobile":{"page":"https://de.m.wikipedia.org/wiki/Hauskatze","revisions":"https://de.m.wikipedia.org/wiki/Special:History/Hauskatze","edit":"https://de.m.wikipedia.org/wiki/Hauskatze?action=edit","talk":"https://de.m.wikipedia.org/wiki/Diskussion:Hauskatze"}},"extract":"Die Hauskatze ist die Haustierform der Falbkatze. Sie zählt zu den beliebtesten Heimtieren. In der Rassekatzenzucht werden oft nur die Tiere als Hauskatze bezeichnet, die keiner anerkannten Katzenrasse angehören.","extract_html":"<p>Die <b>Hauskatze</b> ist die Haustierform der Falbkatze. Sie zählt zu den beliebtesten Heimtieren. In der Rassekatzenzucht werden oft nur die Tiere als Hauskatze bezeichnet, die keiner anerkannten Katzenrasse angehören.</p>"}
rest_command:
  wiki_get_rawdata:
    url: "https://de.wikipedia.org/api/rest_v1/page/summary/{{ url }}"
    method: GET
    content_type:  'application/json; charset=utf-8'

alias: wiki
description: ""
trigger:
  - platform: conversation
    command:
      - suche  {name}
condition: []
action:
  - service: rest_command.wiki_get_rawdata
    data:
      url: "{{ trigger.slots.name }}"
    response_variable: summary
  - alias: Parse data
    variables:
      abstract: "{{ summary['extract'] }}"
  - service: notify.persistent_notification
    data:
      message: "{{ abstract }}"
  - set_conversation_response: "{{ abstract }}"
mode: single
1 Like

Hi, was searching for the same and got this done now :slight_smile: the json response has the path ‘content’ for any reason, so the correct code is:

variables:
  abstract: "{{ summary['content']['extract'] }}"

I still had some issues with the german wikipedia API on some queries like “skoda”, as the title in the API url has a special character. the valid page title for the url can be found with another search query and then it can be used to query the page:

rest_command:
  wiki_search:
    url: "https://de.wikipedia.org/w/rest.php/v1/search/title?q={{ url }}&limit=1&format=json"
    method: GET
    content_type:  'application/json; charset=utf-8'

  wiki_data:
    url: "https://de.wikipedia.org/api/rest_v1/page/summary/{{ url }}"
    method: GET
    content_type:  'application/json; charset=utf-8'

action:
  - service: rest_command.wiki_search
    data:
      url: "{{ trigger.slots.name.replace(' ', '+')  }}"
    response_variable: wiki_search_summary
    enabled: true
  - alias: Parse data
    variables:
      wiki_url: "{{ wiki_search_summary['content']['pages'][0]['key'] }}"
    enabled: true

  - service: rest_command.wiki_data
    data:
      url: "{{ wiki_url }}"
    response_variable: wiki_data_summary
    enabled: true
  - alias: Parse data
    variables:
      wiki_extract: "{{ wiki_data_summary['content']['extract'] }}"
    enabled: true

  - set_conversation_response: "{{ wiki_extract }}"

maybe that helps some people :slight_smile:

Now i have it working even easier in one service trigger.

alias: wiki
description: ""
trigger:
  - platform: conversation
    command:
      - suche [nach] {name}
      - was ist eine {name}
condition: []
action:
  - service: rest_command.wiki_get_rawdata
    data:
      url: "{{ trigger.slots.name }}"
    response_variable: summary
  - alias: Parse data
    variables:
      abstract: "{{ summary['content']['extract'] }}"
  - set_conversation_response: |
      "{{ abstract }}"
mode: single
rest_command:
  wiki_get_rawdata:
    url: "https://de.wikipedia.org/api/rest_v1/page/summary/{{ url }}"
    method: GET
    content_type:  'application/json; charset=utf-8'
    

I managed to filter out the disambiguation pages and select first page title and search for summary

alias: wiki
description: ""
trigger:
  - platform: conversation
    command:
      - suche [nach] {name}
      - was ist eine {name}
condition: []
action:
  - service: rest_command.wiki_search
    data:
      url: "{{ trigger.slots.name  }}"
    response_variable: wiki_search_summary
    enabled: true
  - alias: Parse data
    variables:
      wiki_url: |
        {% set pages = wiki_search_summary.content.pages %}
        {% for page in pages %}
          {% if page.description != "Wikimedia-Begriffsklärungsseite" %}
            {{ page.key }}
            {% break %}
          {% endif %}
        {% endfor %}
  - service: rest_command.wiki_get_rawdata
    data:
      url: "{{ wiki_url }}"
    response_variable: summary
  - alias: Parse data
    variables:
      abstract: "{{ summary['content']['extract'] }}"
  - set_conversation_response: |
      {% if abstract=="" %}
      "Nichts gefunden"
      {% else %}

      "{{ abstract }}"
      {% endif %}
mode: single

1 Like

I’m using multiple german APIs now so i did some changes to the skills…

rest_command:
  api_search:
    url: "{{ url }}"
    method: GET
    content_type:  'application/json; charset=utf-8'

play radio stations:

alias: radio-browser
description: ""
trigger:
  - platform: conversation
    command:
      - spiele radio {name}
condition: []
action:
  - service: rest_command.api_search
    data:
      url: >-
        https://de1.api.radio-browser.info/json/stations/byname/{{
        trigger.slots.name.replace(' ', '_') 
        }}?order=bitrate&limit=1&reverse=true
    response_variable: api_search_summary
    enabled: true
  - alias: Parse data
    variables:
      api_extract_name: "{{ api_search_summary['content'][0]['name'] }}"
      api_extract_url: "{{ api_search_summary['content'][0]['url'] }}"
    enabled: true
  - set_conversation_response: spiele {{ api_extract_name }}
  - service: media_player.play_media
    target:
      entity_id: media_player.<YOUR PLAYER ID>
    data:
      media_content_id: "{{ api_extract_url }}"
      media_content_type: audio/mpeg
mode: single

tell jokes:

alias: witzapi
description: ""
trigger:
  - platform: conversation
    command:
      - erzähle [mir | uns] [noch] einen witz
      - erzähl [mir | uns] [noch] einen witz
condition: []
action:
  - service: rest_command.api_search
    data:
      url: https://witzapi.de/api/joke/?limit=1&language=de
    response_variable: api_search_summary
    enabled: true
  - alias: Parse data
    variables:
      api_extract: "{{ api_search_summary['content'][0]['text'] }}"
    enabled: true
  - set_conversation_response: "{{ api_extract }}"
mode: single

tell useless facts:

alias: uselessfacts
description: ""
trigger:
  - platform: conversation
    command:
      - erzähl [mir | wir | uns] [noch] [einen | etwas] [fakt | interessantes]
      - erzähle [mir | wir | uns] [noch] [einen | etwas] [fakt | interessantes]
      - erzählen [mir | wir | uns] [noch] [einen | etwas] [fakt | interessantes]
condition: []
action:
  - service: rest_command.api_search
    data:
      url: https://uselessfacts.jsph.pl/api/v2/facts/random?language=de
    response_variable: api_search_summary
    enabled: true
  - alias: Parse data
    variables:
      api_extract: "{{ api_search_summary['content']['text'] }}"
    enabled: true
  - set_conversation_response: "{{ api_extract }}"
mode: single