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
.
Configuration
Requirements
- Telegram Bot integration
Blueprint fields
-
Input_text entity
: 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.
Related blueprints
-
Telegram Bot: Execute an action after receiving a text message
-
Telegram Bot: Respond to Telegram Command with a text message
-
Telegram Bot: Respond to Telegram Command by executing an action
Other blueprints
How to get this blueprint?
Click the badge to import this Blueprint (needs Home Assistant Core 2021.3 or higher):
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