Telegram Bot: Store a text message into an input_text entity

:face_with_monocle: About this blueprint

Type of blueprint: SCRIPT

What does it do?

Wait for a telegram_text event and store the received text message into an input_text entity. Can be filtred by user_id and chat_id.

:gear: Configuration

Requirements

Blueprint fields

  • Input_text entity :asterisk:: REQUIRED. Select an input_text entity to store the message into.

  • Remove received message?: Activate this toggle to delete the received message from chat. Useful for private or secret messages (alarm codes, passwords…).

Script variables

These variables can be set up when calling the script. See Passing variables to scripts for more information.

  • timeout: Maximum time to wait for receiving a message, in seconds. Default 30 seconds.

  • filter_user_id: Store the message ONLY IF it is received from this user_id. Leave empty to avoid filtering.

  • filter_chat_id: Store the message ONLY IF it is received from this chat_id. Leave empty to avoid filtering.

:pushpin: Related blueprints

:heavy_plus_sign: Other blueprints

:arrow_down: How to get this blueprint?

Click the badge to import this Blueprint (needs Home Assistant Core 2021.3 or higher):

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or you can also:

  • Import this Blueprint by using the forum topic URL
  • Copy-paste the content of the following frame in a new file inside your configuration’s blueprint folder:
Blueprint YAML
blueprint:
  name: 'Telegram Bot: Store a text message into an input_text entity'
  description: >
    Wait for a `telegram_text` event and store the received text message into an `input_text` entity. Can be filtred by `user_id` and `chat_id`.
    
    ## Blueprint fields:

    ### "Input_text entity" field

    Select an input_text entity to store the message into.

    ### "Remove received message?" field

    Activate this toggle to delete the received message from chat.
    
    ## Script variables:

    These variables can be set up when calling the script. See [Passing variables to scripts](https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts) for more information.

      - `timeout`: Maximum time to wait for receiving a message, in seconds. Default 30 seconds.
      
      - `filter_user_id`: Store the message ONLY IF it is received from this user_id. Leave empty to avoid filtering.

      - `filter_chat_id`: Store the message ONLY IF it is received from this chat_id. Leave empty to avoid filtering.

  domain: script

  input:
    input_text_entity:
      name: Input_text entity
      description: "Select an input_text entity to store the message into."
      selector:
        target:
          entity:
            domain: input_text

    remove:
      name: Remove received message?
      description: Activate this toggle to delete the received message from chat.
      default: false
      selector:
        boolean:



mode: parallel

max: 10

max_exceeded: warning



fields:
  timeout:
    name: Timeout
    description: Maximum time to wait for receiving a message, in seconds.
    default: '30'
    required: true
    selector:
      number:
        min: 0
        max: 300
        mode: slider
        unit_of_measurement: "s"     

  filter_user_id:
    name: Filter user_id
    description: Store the message ONLY IF it is received from this user_id. Leave empty to avoid filtering.
    selector:
      text:

  filter_chat_id:
    name: Filter chat_id
    description: Store the message ONLY IF it is received from this chat_id. Leave empty to avoid filtering.
    selector:
      text:



variables:
  filter_user_id: "{% if filter_user_id is defined %}{{ filter_user_id }}{% else %}{% endif %}"
  filter_chat_id: "{% if filter_chat_id is defined %}{{ filter_chat_id }}{% else %}{% endif %}"
  timeout: "{% if timeout is defined %}{{ timeout }}{% else %}30{% endif %}"
  remove: !input remove



sequence:
  - alias: "Choose whether to filter or not"
    choose:
      - alias: "Filter both user_id and chat_id"
        conditions: "{{ filter_user_id != '' and filter_chat_id != '' }}"
        sequence:
          - alias: "Wait for telegram_text event"
            wait_for_trigger:
              - platform: event
                id: "telegram_text"
                event_type: telegram_text
                event_data:
                  user_id: "{{ filter_user_id }}"
                  chat_id: "{{ filter_chat_id }}"
            timeout:
              seconds: "{{ timeout }}"
            continue_on_timeout: true
      - alias: "Filter ONLY chat_id"
        conditions: "{{ filter_user_id == '' and filter_chat_id != '' }}"
        sequence:
          - alias: "Wait for telegram_text event"
            wait_for_trigger:
              - platform: event
                id: "telegram_text"
                event_type: telegram_text
                event_data:
                  chat_id: "{{ filter_chat_id }}"
            timeout:
              seconds: "{{ timeout }}"
            continue_on_timeout: true
      - alias: "Filter ONLY user_id"
        conditions: "{{ filter_user_id != '' and filter_chat_id == '' }}"
        sequence:
          - alias: "Wait for telegram_text event"
            wait_for_trigger:
              - platform: event
                id: "telegram_text"
                event_type: telegram_text
                event_data:
                  user_id: "{{ filter_user_id }}"
            timeout:
              seconds: "{{ timeout }}"
            continue_on_timeout: true
    default:
      - alias: "Wait for telegram_text event"
        wait_for_trigger:
          - platform: event
            id: "telegram_text"
            event_type: telegram_text
        timeout:
          seconds: "{{ timeout }}"
        continue_on_timeout: true

  - alias: "Choose whether message was received or timeout completed"
    choose:
      - alias: "Timeout completed"
        conditions: "{{ wait.trigger == none }}"
        sequence:
        
    default:
      - alias: "Message received: setting variables from trigger"
        variables:
          telegram_text: "{{ wait.trigger.event.data['text'] }}"
          telegram_sender_first_name: "{{ wait.trigger.event.data['from_first'] }}"
          telegram_sender_last_name: "{{ wait.trigger.event.data['from_last'] }}"
          telegram_sender_full_name: "{{ telegram_sender_first_name }} {{ telegram_sender_last_name }}"
          telegram_sender_user_id: "{{ wait.trigger.event.data['user_id'] }}"
          telegram_chat_id: "{{ wait.trigger.event.data['chat_id'] }}"
          telegram_message_id: "{{ wait.trigger.event.data['id'] }}"

      - alias: "Storing message into input_text entity"
        service: input_text.set_value
        target: !input input_text_entity
        data:
          value: "{{ telegram_text }}"

      - alias: "Do I have to remove the received message from chat?"
        choose:
          - alias: "Yes, remove message"
            conditions: "{{ remove }}"
            sequence:
              - alias: "Removing message"
                service: telegram_bot.delete_message
                data:
                  message_id: "{{ telegram_message_id }}"
                  chat_id: "{{ telegram_chat_id }}"

Changelog

  • 2022-01-30: Solved issue when not receiving any message.
  • 2022-01-30: New option to remove received message.
  • 2022-01-30: First version
1 Like

Thanks for the blueprints, they make it easier to deal with the bot. But I have another problem: as soon as I send commands to Home Assistant via the app, everything works and Telegram responds to the chat. But I also want to send commands via Tasker (i.e. the API with POST) and here Home Assistant does not respond, as these messages are apparently sent as a bot in the chat.

My question: Is there a solution for this at all? Or does it have something to do with the authorisation? In configuration.yaml I have:

rest_command:
  assist_request:
    url: "http://192.168.xxx.xxx:8123/api/conversation/process" 
    method: POST
    headers:
      authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... SZE8
      accept: "application/json, text/html"
    payload: '{"text": "{{ text }}","language": "{{ language }}"}'
    content_type: 'application/json; charset=utf-8'
    verify_ssl: true

Hi @rr19-hub,

I don’t use tasker, so I cannot help you so much. If you have problems with the authentication, you could check this tutorial: ⚙️ Using Tasker with Home Assistant (V2)
Or you could also use webhooks to trigger automations in Home Assistant: Automation Trigger - Home Assistant instead of communicating directly.

I hope at least I helped you a bit.

First of all, I just wanted to say thanks for your work with all of your Blueprints so far! Second, is there any way that this can work with groups as well? Or am I missing something? Given that I just woke up and have yet to clear the fog 100% I could just be missing something. Thanks for your time!

Hi @Zashtys ! Thanks for finding this blueprint useful!

This blueprint should work with groups. If you’re trying to filter a group ID, just remember that in Telegram, group IDs begin with - (full example: -123456789). If you’re trying to execute a Home Assistant action from a group chat with a Telegram command, I recommend using this other blueprint: Telegram Bot: Respond to Telegram Command by executing an action

If you explain me what you’re trying to do, I can try to help you with the set-up.

Thanks for your comments! :upside_down_face:

Thank you for your blueprint.
I set it easily and it works but I have a small problem:

Following the event sequence:

  1. I clear the target text field setting null value “” manually or by automation.
  2. I send a telegram message to the bot. Nothing appends.
  3. I send immediatly another message. All works.
  4. I clear the target text field setting null value “” manually or by automation.
  5. I send a telegram message to the bot. Nothing appends.
  6. I send immediatly another message. All works.

Practically, it works alternately.

I tried to change the timeout value but it doesn’t seems related to the problem.

Any suggestion?

Many thanks,
Gery.

Hi @maverik360!

I tried to reproduce your issue, but I don’t get the problem you explain.

Keep in mind that this script finished when a text message is received. If you immediately send a second message, it won’t be stored unless you run the script again first. Can it be part of the issue?

I don’t know if this is what you’re trying to do, but if this is the case, I guess a possible solution would be to call the script on every change in the input_text entity. It could be done with an automation like this one:

trigger:
  - platform: state
    entity_id:
      - input_text.text
action:
  - service: script.turn_on
    target:
      entity_id: script.telegram_bot_script

I hope it helps!

Hello Marc,
thanks a lot for your reply.
I tried but the result doesn’t change.

My scope is to use a card on the dashboard as a memo board that I can update by a telegram message.

Following the automation I’m using to update the field.

First of all I clear the input_text entity, then I turn on the script (as you suggested) and then I activate it.

alias: Telegram_memo_update
description: ""
trigger:
  - platform: event
    event_type: telegram_text
condition: []
action:
  - service: input_text.set_value
    target:
      entity_id: input_text.memo_board
    data:
      value: ""
    enabled: true
  - service: script.turn_on
    target:
      entity_id: script.telegram_bot_store_a_text_message_into_an_input_text_entity
    data: {}
  - service: script.telegram_bot_store_a_text_message_into_an_input_text_entity
    data:
      timeout: 300
mode: single

All continue to works fine, but alternately. Very strange.

Any other parameter or flow that I can change?

Thanks a lot again for your support.

Gery

Hi @maverik360 , sorry for my super late response (I’ve been busy…).

This script is going to store the text received on Telegram AFTER you turn on the script. The timeout is the time the script is waiting for receiving a text message. So, you should set up a new telegram command (such as /memo) that will run the script. You can do it with this other blueprint: Telegram Bot: Respond to Telegram Command by executing an action . Then, when you type /memo on your telegram, the next message you send will be stored to an input_text entity.

Hope it helps!