Telegram_bot: Support for downloading attachments

The telegram API supports downloading attachments. It would be really great to be able to send a voice message to a telegram bot and have it play on a media_player component. I’m not a Python guy so I’d probably screw it up.

This json is received:

{  
   'date':1514860312,
   'voice':{  
      'mime_type':'audio/ogg',
      'file_size':6663,
      'duration':2,
      'file_id':'AwADAQADCgADwUlYRoRzTvFKgdjdAg'
   },
   'from':{  
      'id':111111,
      'language_code':'en-US',
      'is_bot':False,
      'first_name':'John',
      'last_name':'Smith'
   },
   'message_id':1553,
   'chat':{  
      'type':'private',
      'id':1111111,
      'first_name':'John',
      'last_name':'Smith'
   }
}

Then via the getFile API you can get the file path like:

https://api.telegram.org/bot<bot_token>/getFile?file_id=the_file_id

Which returns:

{  
   "ok":true,
   "result":{  
      "file_id":"AwADAQADCgADwUlYRoRzTvFKgdjdAg",
      "file_size":6663,
      "file_path":"voice/file_1533"
   }
}

Then you can use download the file via:

https://api.telegram.org/file/bot<token>/<file_path>

Did you ever come up with a way to get the file_id? I would also like to see the ability in home assistant to get the file_id.

Perhaps it could be included in the telegram_sent event or in it’s own event like possibly a telegram_file event.

I use the telegram bot integration in home assistant to send security camera photos to a group, It would be great to be able to get the file_id and use it with inline keyboards and callback queries. My telegram bot sends quite a few images from my security cameras per day to the main telegram group I have set up for my house. With each of the photos sent, I have an inline keyboard that gives me the option to archive the photo to another group for easy access and future reference. I do this with a callback query button and the database record id I get from my blue iris nvr. I also have some nest cameras integrated into home assistant that I have send photos to my main group, but since I don’t have those in a database I have to save them to disk and keep them for a period of time in case I want to archive those with the telegram bot. It would be nice to be able to save the resources and just access the files directly from telegram.

I saw that you can do this with nodered but I prefer keeping my automations within home assistant.

I did it with automation.

-  trigger:
    platform: event
    event_type: telegram_text
  variables:
    token: !secret telegram_token 
    data: "{{ trigger.event.data.text|safe }}"
  action:  
    choose:
    - conditions:
        condition: template
        value_template: '{{ data.audio.file_id != None}}'
      sequence:
      - service: input_text.set_value
        target:
          entity_id: input_text.telegram_get_file_url
        data:
          value: 'https://api.telegram.org/{{token}}/getFile?file_id={{ data.audio.file_id}}'
      - service: homeassistant.update_entity
        target:
          entity_id: sensor.telegram_file_path
      - delay:
          seconds: 2
      - service: telegram_bot.send_message
        data_template:
          target: '{{ trigger.event.data.user_id }}'
          message: "https://api.telegram.org/file/{{token}}/{{states('sensor.telegram_file_path')}}"
          disable_notification: true
          parse_mode: html

you can edit that and do whatever you want with the url (https://api.telegram.org/file/{{token}}/{{states(‘sensor.telegram_file_path’)}}).
Im casting it to my google home…

3 Likes

Hi @yohaybn,

It seems either it was dropped, or I’m missing something, but my json does not contain audio.

Could you perhaps elaborate a bit on how you achieved this?

Yes, Unfortunately they took it down…

Why did they took it down?
Does someone know if we are going to be able to receive files with telegram in HA?

Thank you!

I don’t know why. maybe you can open them issue on github?

A feature would be useful. Is there any alternative?