Telegram bot notifications and communication

Hi syssi,

how do you send the weather-icon via Telegram. Thank you.

This is the automation:

- alias: 'Telegram bot that provides a weather forecast'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/weather'
  action:
    - service: telegram_bot.send_photo
      data_template:
        target: '{{ trigger.event.data.user_id }}'
        disable_notification: true
        file: '/home/homeassistant/.homeassistant/www/climacons/{{ states.sensor.dark_sky_icon.state }}.png'
        caption: '{{ states.sensor.dark_sky_summary.state }}'
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.user_id }}'
        disable_notification: true
        message: '{{ states.sensor.dark_sky_hourly_summary.state }} {{ states.sensor.dark_sky_daily_summary.state }}'

This is the used icon set: http://adamwhitcroft.com/climacons/

1 Like

this is nice thank you very much.

cool, thank you

@petro - save me from trying to work this one out please, Iā€™m sure you know how? :smile:

1 Like

@anon43302295 thats odd, the documentation requires it to be in a string format. @Will711990 can you post the error that the automation posts to the log?

Yeah, I think @Will711990 is saying itā€™s arriving as a string, but it has added single quotes that need stripping off?

Oh, thats ridiculous! I hate when software does that.

Should be able to replace the characters, for safety, we will remove both types of quotes:

  - service: input_datetime.set_datetime
    entity_id: input_datetime.YOUR _ALARM_CLOCK
    data_template:
      time: "{{ trigger.event.data.args | replace('\'', '') | replace('\"','') }}" 

So the slashes are needed to let the software know you want the character that follows the slash. This has to be done because the parser will think itā€™s the end of the string instead of being a character inside the string.

The code is essentially replacing ā€™ with nothing and " with nothing. Should be all instances of each character as well.

2 Likes

Amen :pray:

Hopefully thatā€™ll fix it :slight_smile:

It is possible that the args is returning a datetime object and when it prints, it displays it as a string. In that case, youā€™d need to use strftime. It really depends on what the error is

2 Likes

Thank you to spend time to help me, thatā€™s very cool from youā€¦!

The error is:

Invalid service data for input_datetime.set_datetime: Invalid time specified: ['06:45:00'] for dictionary value @ data['time']. Got "['06:45:00']"

1 Like

thats odd, it shouldnā€™t be a list. Try

  time: "{{ trigger.event.data.args[0] }}"
1 Like

OMG that worksā€¦

Thank you so so much, you reeeeaaaaaalllyyyy rock !!!

2 Likes

How can we setup the config.yaml with a chat ID if we havent yet made contact with the bot? Dont we need the Bot running first so we can chat to it and then get the ID? Or do we put a fake allowed_chat_id in the config until we find out what our real one is? seems a bit chicken before the eggā€¦

I think you deleted your question, but I think it was valid! It stopped me progressing for a while until I figured that part out.

The chat_id is totally seperate from Home Assistant, you need to start a conversation between you and the bot and get the ID of that particular conversation/chat thread. Thatā€™s a Telegram thing. You take that ID and it tells HA which conversation to post messages to.

Yeah, I removed my question after I figured it out. My issue was that the docs make it seem like you need to get the Chat ID from the Bot that we create in HA (which obviously isnā€™t set up yet at that point), but I ended up getting my Chat ID from Telegram by sending a message to @get_id_bot. Iā€™m hoping that is correct and that things workā€¦ Iā€™ll hopefully try it out today.

Hmmā€¦ ok, so rereading your reply, I need the chat ID of a particular chat between me and MY botā€¦ but my bot isnt running until I complete the setup entry in config.yaml which cant be done without allowed_chat_idā€™sā€¦

currently I get no reply if I send a message to MY bot

Thank you for sharing, just when I thought the sleepless nights had ended :joy:

1 Like

The first paragraph on the telegram page gives a link on how to set up your bot.

The simplest way IMAO was:
FYI: Iā€™m using Android.

  1. Install app on smart phone.
  2. Start a chat (click pencil icon buttom right).
  3. Search for BotFather image
  4. Clicks on START (buttom of screen), you will get a reply with options.
  5. Click on /newbot
  6. Follow replies from BotFather
  • choose a name (xxxxx)
  • choose a username (xxxxx_bot)
  1. Copy your token from BotFather reply.
  2. Start a new chat as in step 2 above.
  3. Search for IDBot image
  4. Clicks on START (buttom of screen), you will get a reply with options.
  5. Click on /getid
  6. Copy your ID from IDBot reply.
  7. Setup / Config Telgram in HASS from >
    "To enable Telegram notifications in your installation, add the following to your "
  8. Done.
2 Likes