core icon indicating copy to clipboard operation
core copied to clipboard

Add notify_target parameter for Telegram bot actions

Open hanwg opened this issue 4 months ago • 15 comments

Proposed change

Currently, chat_id is used as targets for telegram bot actions. chat_id is not intuitive since it is a numeric identifier.

With the introduction of notify entities in https://github.com/home-assistant/core/pull/149853, we can instead use these entities as targets. This change introduces a new notify_target parameter which uses the entity selector to allow the user to select telegram bot notify entities for actions.

image

Detailed changes:

  1. Scope: Change is for all Telegram bot actions.
  2. target action parameters:
  • Current: target parameter is used for specifying chat IDs
  • New: target parameter is now used for specifying notify entities. During this migration period, chat IDs found in target are automatically copied to the new chat_id field. A repair issue is also created to inform the user to update their automations/scripts.
  1. Design consideration: This PR doesn't introduce new entity service but rather, it modifies the existing action to handle notify entities. The integration has lots of actions and introducing new entity service will make the number of actions swell. Furthermore, users are already familiar with the names of the existing actions.
  2. Updated action responses (return values): Added new field entity_id to ServiceResponse. This field is only available if entity_id was used in the input. This new output value can be used in complex automations e.g. output entity_id of send_message can be used as input for edit_message.
  3. Existing fields, config_entry_id and chat_id are moved into a a new section named "Custom targets". This helps to avoid confusion for new users by hiding those 2 fields and drawing the user's attention to the target field for specifying notify enties as targets.
  4. New behavior: We first build the list of targets based on the entity_id, config_entry_id and target fields. The service is then invoked once for each of the targets. (See _build_targets() in __init__.py)

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [x] New feature (which adds functionality to an existing integration)
  • [ ] Deprecation (breaking change to happen in the future)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [ ] Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: https://github.com/orgs/home-assistant/discussions/881
  • This PR is related to issue:
  • Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/41823
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • [x] I understand the code I am submitting and can explain how it works.
  • [x] The code change is tested and works locally.
  • [x] Local tests pass. Your PR cannot be merged unless tests pass
  • [x] There is no commented out code in this PR.
  • [x] I have followed the development checklist
  • [x] I have followed the perfect PR recommendations
  • [x] The code has been formatted using Ruff (ruff format homeassistant tests)
  • [x] Tests have been added to verify that the new code works.
  • [x] Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • [ ] The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • [ ] New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

hanwg avatar Oct 20 '25 10:10 hanwg

Look good!

More clean and lower possibilities of mistakes

MiguelAngelLV avatar Oct 20 '25 16:10 MiguelAngelLV

Yep. Looks a very good improvement. I wonder if it would be ideal to allow either target or notify_target to be flagged, but not both. That would simplify also the logic quite a bit, no?

luca-angemi avatar Oct 20 '25 17:10 luca-angemi

Wouldn't a separate entity service be more suitable? There is a select for the config entry, but the notify entities could correspond to different config entries. That is imho a bit counter-intuitive. The ntfy integration for example has an action (ntfy.publish) to target notify entities.

tr4nt0r avatar Oct 20 '25 20:10 tr4nt0r

Wouldn't a separate entity service be more suitable? There is a select for the config entry, but the notify entities could correspond to different config entries. That is imho a bit counter-intuitive. The ntfy integration for example has an action (ntfy.publish) to target notify entities.

If you would want send a notification to multiples users, you should create multiple actions... This allow use one action to multiples targets

Yep. Looks a very good improvement. I wonder if it would be ideal to allow either target or notify_target to be flagged, but not both. That would simplify also the logic quite a bit, no?

I think that the integration should allow it. if you remove numeric chat will be a breaking change, and sometimes you would want send a notification to regular target and especials targets, so both target should be set in the same action

MiguelAngelLV avatar Oct 20 '25 20:10 MiguelAngelLV

Wouldn't a separate entity service be more suitable? There is a select for the config entry, but the notify entities could correspond to different config entries. That is imho a bit counter-intuitive. The ntfy integration for example has an action (ntfy.publish) to target notify entities.

Added v2 screenshot in PR description. In v2, I moved the config_entry_id and target fields into a new collapsed section named "Custom target". The intent is that most users should only use the new notify_target field.

If the user wants to send messages to arbitrary chat_ids then they will use the config_entry_id and target fields. Related PR about sending to arbitrary chat_ids : https://github.com/home-assistant/core/pull/149924

hanwg avatar Oct 21 '25 03:10 hanwg

I'd like to see a motivation to why we want to extend the legacy services instead of adding entity service(s) as done for ntfy in #143560

I initially did thought of creating a new action (for example telegram_bot.notify_message but decided to modify the existing telegram_bot.send_message action because of the following reasons:

  1. avoid breaking changes
  2. support for future feature request (https://github.com/orgs/home-assistant/discussions/467)

hanwg avatar Oct 27 '25 14:10 hanwg

This PR is well and truly needed.

I just went to update my 227 uses of the notify.telegram_xxx action (where xxx is one of five defined chat groups) to the new telegram_bot.send_message action as a repair came up when I installed core-2025.11.0b1.

I found I could not set the newly created telegram bot entities as a targets for this action. I had to list the numeric chat ids.

I will ignore the repair until this is resolved / merged / released.

tomlut avatar Nov 01 '25 05:11 tomlut

@hanwg I don't understand that explanation. Why would implementing a more modern approach be a breaking change or prevent implementing future features?

I think what's not clear to me is:

  • When do users want to not set up notify entities for certain chats, but instead provide them in a legacy style notify service call?
  • If we want to keep extending support for both targeting chats without subentry and targeting chats with a subentry, why is it beneficial to do that in a single service instead of in separate services?

emontnemery avatar Nov 03 '25 09:11 emontnemery

One of the early suggestion was to remove the existing config_entry_id and target parameters and to add the new notify_target parameter for the existing actions. This would have been a breaking change and so this idea was dropped.

It's actually easier to implement separate services/actions and there won't be breaking changes; the only downside is that the integration will have twice as many actions.

I was concerned that having 2 sets of actions (example: the existing telegram_bot.send_message and the new telegram_bot.notify_message) might be confusing for the users. Most users might not be aware of the new notify_message action since it is not a standard Telegram function like send_message.

hanwg avatar Nov 03 '25 10:11 hanwg

@hanwg OK, but when do users want to not set up notify entities for certain chats, but instead provide them in a legacy style notify service call?

emontnemery avatar Nov 04 '25 09:11 emontnemery

I had intended the legacy style (config_entry_id with target) to be used for sending/receiving to arbitrary chat_ids (see: https://github.com/home-assistant/core/pull/149924).

hanwg avatar Nov 04 '25 11:11 hanwg

PR is ready for review, if anyone wants to be an early tester, I would greatly appreciate it.

hanwg avatar Nov 16 '25 01:11 hanwg

Testing right now as a custom component. For the action send_message it works great. I don't see the new fields for the other actions. Is that on purpose?

luca-angemi avatar Nov 16 '25 09:11 luca-angemi

It's only for send_message for now. The some of the other actions have some special handling so it's probably better to do them in a separate PR.

hanwg avatar Nov 16 '25 10:11 hanwg

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Dec 15 '25 11:12 home-assistant[bot]