Send Telegram photo using script with variables

Hi,
I want to make a call from an external program to Hass to send a photo using Telegram. The photo file name is different every time, so I’ve created an script and I want to call it passing the photo name as a variable:

I want to do this using a script because I want to add some logit to it, so I cannot call the notify/telegram service directly.

I’ve created the follwing simple script:

test:
  alias: 'Test script'
  sequence:
      - service: notify.telegram
        data_template:
          message: 'text'
          data:
            photo:
              - file: '{{file}}'
                caption: '{{caption}}'

I call it:

curl -X POST -H "Content-Type: application/json" -H "x-ha-access: xxxxxxxxx" -H "Cache-Control: no-cache" -d '{
    "entity_id": "script.test",
    "data": {
	    "file": "/test.jpg",
	    "caption": "Sample caption"
    }
}' "https://xxx.www.zzz/api/services/script/test"

The call arrives to the server but I get the following error:

ERROR (MainThread) [homeassistant.core] Invalid service data for notify.telegram: extra keys not allowed @ data['data_template']. Got OrderedDict([('photo', [OrderedDict([('file', '{{file}}'), ('caption', '{{caption}}')])])])

Is there any way to pass “objects” as script variables?

To me it seems like you have a small error here how stuff gets passed to ha. You would need to replace ‘file’ with ‘data.file’ same with caption or you need to remove the data object in your curl call and add file and caption on the same level as entity_id. This however does not fix your error I believe. Because it states that your config is wrong which I don’t see currently why.

It might be that data_template is not currently allowed in the notify service.

~Cheers

Thank you!

You are right, there is an error in the script. I need to remove the ‘-’ before the file. Once I solved it I follow your suggestion to use {{data.file}} instead {{file}} and it worked!!!

The final code is:

test:                                                                                                                                                                                                              
  alias: 'Test script'                                                                                                                                                                                             
  sequence:                                                                                                                                                                                                        
      - service: notify.telegram                                                                                                                                                                                   
        data_template:                                                                                                                                                                                             
          message: 'text'                                                                                                                                                                                          
          data:                                                                                                                                                                                                    
            photo:
              file: '{{data.file}}'                                                                                                                                                                                
              caption: '{{data.caption}}'

The call remains the same.

Thank you very much!

Ah nice so data_template works for telegram that’s nice to know! Thanks for reporting back!

~Cheers