jekalmin / extended_openai_conversation

Home Assistant custom component of conversation agent. It uses OpenAI to control your devices.
964 stars 136 forks source link

Example of a POST request as a function #134

Open s1061429 opened 9 months ago

s1061429 commented 9 months ago

Thanks for this great project! I am trying to do a POST request as a function, but can you provide an example of how to do this? Thanks!

jekalmin commented 9 months ago

For rest type, schema is same as REST in Home Assistant. The difference is that value_template is added in this integration, so that you can modify API result.

Form data

- spec:
    name: get_your_data
    description: Get your data
    parameters:
      type: object
      properties:
        dummy:
          type: string
          description: Nothing.
  function:
    type: rest
    method: POST
    resource: "https://YOUR_POST_API"
    payload: "foo=foo&bar=bar"
    headers:
      "Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
    value_template: '{{value_json}}'

JSON

- spec:
    name: get_your_data
    description: Get your data
    parameters:
      type: object
      properties:
        dummy:
          type: string
          description: Nothing.
  function:
    type: rest
    method: POST
    resource: "https://YOUR_POST_API"
    payload: >-
      {"foo": "foo", "bar": "bar"}
    headers:
      "Content-Type": "application/json; charset=utf-8"
    value_template: '{{value_json}}'
s1061429 commented 9 months ago

Thanks! So in that case no templating is available for the payload? Like

- spec:
    name: get_your_data
    description: Get your data
    parameters:
      type: object
      properties:
        foo:
          type: string
          description: Some interesting payload variable.
  function:
    type: rest
    method: POST
    resource: "https://YOUR_POST_API"
    payload: >-
      {"foo": "{{foo}}", "bar": "bar"}
    headers:
      "Content-Type": "application/json; charset=utf-8"
    value_template: '{{value_json}}'

This would be nice to have.

jekalmin commented 9 months ago

I just realized that rest and scrape of HA doesn't support payload as a template.

I added payload_template variable in rest and scrape function. Please try 1.0.3-beta1 version and give a feedback.

s1061429 commented 9 months ago

Works like a charm! Thanks a lot!