Telegram Bot: Store a text message into an input_text entity

:face_with_monocle: About this blueprint

This blueprint is useful to 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.

Technical Details
  • Type of blueprint: SCRIPT

: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:

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

Or you can also:

  • Copy-paste the content from this link to a new file inside your configuration’s blueprints/automation folder.

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!