Fix Telegram Bot send file to multiple targets, snapshots of HA cameras, variable templating, digest auth#7771
Conversation
To make a row of InlineKeyboardButtons you pass:
- a list of tuples like: `[(text_b1, data_callback_b1), (text_b2, data_callback_b2), ...]
- a string like: `/cmd1, /cmd2, /cmd3`
- or a string like: `text_b1:/cmd1, text_b2:/cmd2`
Example:
```yaml
data:
message: 'TV is off'
disable_notification: true
inline_keyboard:
- TV ON:/service_call switch.turn_on switch.tv, Other:/othercmd
- /help, /init
```
- Allow templating for caption, url, file, longitude and latitude fields - Fix send a file to multiple targets - Load data with some retrying for HA cameras, which return 500 one or two times sometimes (generic cams, always!). - Doc in services for new inline keyboards yaml syntax: `Text button:/command`
|
@azogue, thanks for your PR! By analyzing the history of the files in this pull request, we identified @fabaff, @sander76 and @MartinHjelmare to be potential reviewers. |
| kwargs = dict(service.data) | ||
| _render_template_attr(kwargs, ATTR_MESSAGE) | ||
| _render_template_attr(kwargs, ATTR_TITLE) | ||
| _render_template_attr(kwargs, ATTR_URL) |
| _LOGGER.warning("BAD TARGET %s, using default: %s", | ||
| try: | ||
| chat_ids = [int(t) for t in target | ||
| if int(t) in self.allowed_chat_ids] |
There was a problem hiding this comment.
Chat ids in target have already been validated and coerced to integers.
There was a problem hiding this comment.
It's true, this is old, I'm going to simplify it
| try: | ||
| chat_ids = [int(t) for t in target | ||
| if int(t) in self.allowed_chat_ids] | ||
| if len(chat_ids) > 0: |
| if len(chat_ids) > 0: | ||
| return chat_ids | ||
| _LOGGER.warning("Unallowed targets: %s", target) | ||
| except (ValueError, TypeError): |
There was a problem hiding this comment.
When could this happen? Both target and allowed chat ids have been validated as list of integers before this.
| return chat_ids | ||
| _LOGGER.warning("Unallowed targets: %s", target) | ||
| except (ValueError, TypeError): | ||
| _LOGGER.warning("Bad target data: %s, using default: %s", |
There was a problem hiding this comment.
Shouldn't the warning about default be included in the above warning instead? The warning here should never happen.
| params = self._get_msg_kwargs(kwargs) | ||
| caption = kwargs.get(ATTR_CAPTION) | ||
| func_send = self.bot.sendPhoto if is_photo else self.bot.sendDocument | ||
| file = load_data( |
There was a problem hiding this comment.
Pick another variable name than file. It's reserved in python.
There was a problem hiding this comment.
This one is still here. 😉
|
|
||
|
|
||
| def load_data(url=None, file=None, username=None, password=None): | ||
| def load_data(url=None, file=None, username=None, password=None, |
There was a problem hiding this comment.
Pick another parameter name than file. It's reserved in python.
| params["auth"] = HTTPDigestAuth(username, password) | ||
| else: | ||
| params["auth"] = HTTPBasicAuth(username, password) | ||
| req = requests.get(url, **params) |
There was a problem hiding this comment.
Now there's no request without authentication. Is that intended?
There was a problem hiding this comment.
authentication params are included only when are present:
params = {"timeout": 15}
if username is not None and password is not None:
if authentication == HTTP_DIGEST_AUTHENTICATION:
params["auth"] = HTTPDigestAuth(username, password)
else:
params["auth"] = HTTPBasicAuth(username, password)What do you mean?
There was a problem hiding this comment.
You're correct. I missed that point, sorry.
- Don't use `file` as variable name. - For loop - Simplify filter allowed `chat_id`s.
|
@MartinHjelmare, sorry, I found one more when testing the examples in the doc. |
|
Nice! Is there any chance this will go into a 45.2 release? |
|
@azogue Ready to merge? |
|
Yes! |
Description:
Fix some problems introduced with the changes in
telegram_botandnotify.telegramof 0.45 and 0.45.1, and some enhancements:url,file,caption,longitude,latitudefields.Related issues:
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2714
Notification Examples for
configuration.yaml:Checklist:
If user exposed functionality or configuration variables are added/changed: