| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
from inspect import cleandoc |
| 29 |
from json import dumps, loads |
| 30 |
|
| 31 |
# Disable logging for a cleaner testing output |
| 32 |
import logging |
| 33 |
import os |
| 34 |
import re |
| 35 |
from unittest import mock |
| 36 |
|
| 37 |
from helpers import AppriseURLTester |
| 38 |
import pytest |
| 39 |
import requests |
| 40 |
|
| 41 |
from apprise import ( |
| 42 |
Apprise, |
| 43 |
AppriseAsset, |
| 44 |
AppriseAttachment, |
| 45 |
NotifyFormat, |
| 46 |
NotifyType, |
| 47 |
) |
| 48 |
from apprise.exception import AppriseImproperlyConfigured |
| 49 |
from apprise.plugins.telegram import NotifyTelegram |
| 50 |
|
| 51 |
logging.disable(logging.CRITICAL) |
| 52 |
|
| 53 |
# Attachment Directory |
| 54 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 55 |
|
| 56 |
# Our Testing URLs |
| 57 |
apprise_url_tests = ( |
| 58 |
################################## |
| 59 |
# NotifyTelegram |
| 60 |
################################## |
| 61 |
( |
| 62 |
"tgram://", |
| 63 |
{ |
| 64 |
"instance": None, |
| 65 |
}, |
| 66 |
), |
| 67 |
# Simple Message |
| 68 |
( |
| 69 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 70 |
{ |
| 71 |
"instance": NotifyTelegram, |
| 72 |
}, |
| 73 |
), |
| 74 |
# Simple Message (no images) |
| 75 |
( |
| 76 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 77 |
{ |
| 78 |
"instance": NotifyTelegram, |
| 79 |
# don't include an image by default |
| 80 |
"include_image": False, |
| 81 |
}, |
| 82 |
), |
| 83 |
# Simple Message with multiple chat names |
| 84 |
( |
| 85 |
"tgram://123456789:abcdefg_hijklmnop/id1/id2/", |
| 86 |
{ |
| 87 |
"instance": NotifyTelegram, |
| 88 |
}, |
| 89 |
), |
| 90 |
# Simple Message with multiple chat names |
| 91 |
( |
| 92 |
"tgram://123456789:abcdefg_hijklmnop/?to=id1,id2", |
| 93 |
{ |
| 94 |
"instance": NotifyTelegram, |
| 95 |
}, |
| 96 |
), |
| 97 |
# Simple Message with an invalid chat ID |
| 98 |
( |
| 99 |
"tgram://123456789:abcdefg_hijklmnop/%$/", |
| 100 |
{ |
| 101 |
"instance": NotifyTelegram, |
| 102 |
# Notify will fail |
| 103 |
"response": False, |
| 104 |
}, |
| 105 |
), |
| 106 |
# Simple Message with multiple chat ids |
| 107 |
( |
| 108 |
"tgram://123456789:abcdefg_hijklmnop/id1/id2/23423/-30/", |
| 109 |
{ |
| 110 |
"instance": NotifyTelegram, |
| 111 |
}, |
| 112 |
), |
| 113 |
# Simple Message with multiple chat ids (no images) |
| 114 |
( |
| 115 |
"tgram://123456789:abcdefg_hijklmnop/id1/id2/23423/-30/", |
| 116 |
{ |
| 117 |
"instance": NotifyTelegram, |
| 118 |
# don't include an image by default |
| 119 |
"include_image": False, |
| 120 |
}, |
| 121 |
), |
| 122 |
# Support bot keyword prefix |
| 123 |
( |
| 124 |
"tgram://bottest@123456789:abcdefg_hijklmnop/lead2gold/", |
| 125 |
{ |
| 126 |
"instance": NotifyTelegram, |
| 127 |
}, |
| 128 |
), |
| 129 |
# Support Thread Topics |
| 130 |
( |
| 131 |
"tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=12345", |
| 132 |
{ |
| 133 |
"instance": NotifyTelegram, |
| 134 |
}, |
| 135 |
), |
| 136 |
# Thread is just an alias of topic |
| 137 |
( |
| 138 |
"tgram://bottest@123456789:abcdefg_hijklmnop/id1/?thread=12345", |
| 139 |
{ |
| 140 |
"instance": NotifyTelegram, |
| 141 |
}, |
| 142 |
), |
| 143 |
# Threads must be numeric |
| 144 |
( |
| 145 |
"tgram://bottest@123456789:abcdefg_hijklmnop/id1/?topic=invalid", |
| 146 |
{ |
| 147 |
"instance": AppriseImproperlyConfigured, |
| 148 |
}, |
| 149 |
), |
| 150 |
# content must be 'before' or 'after' |
| 151 |
( |
| 152 |
"tgram://bottest@123456789:abcdefg_hijklmnop/id1/?content=invalid", |
| 153 |
{ |
| 154 |
"instance": AppriseImproperlyConfigured, |
| 155 |
}, |
| 156 |
), |
| 157 |
( |
| 158 |
"tgram://bottest@123456789:abcdefg_hijklmnop/id1:invalid/?thread=12345", |
| 159 |
{ |
| 160 |
"instance": NotifyTelegram, |
| 161 |
# Notify will fail (bad target) |
| 162 |
"response": False, |
| 163 |
}, |
| 164 |
), |
| 165 |
# Testing image |
| 166 |
( |
| 167 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes", |
| 168 |
{ |
| 169 |
"instance": NotifyTelegram, |
| 170 |
}, |
| 171 |
), |
| 172 |
# Testing invalid format (fall's back to html) |
| 173 |
( |
| 174 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=invalid", |
| 175 |
{ |
| 176 |
"instance": NotifyTelegram, |
| 177 |
}, |
| 178 |
), |
| 179 |
# Testing empty format (falls back to html) |
| 180 |
( |
| 181 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=", |
| 182 |
{ |
| 183 |
"instance": NotifyTelegram, |
| 184 |
}, |
| 185 |
), |
| 186 |
# Testing valid formats |
| 187 |
( |
| 188 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown", |
| 189 |
{ |
| 190 |
"instance": NotifyTelegram, |
| 191 |
}, |
| 192 |
), |
| 193 |
( |
| 194 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown&mdv=v1", |
| 195 |
{ |
| 196 |
"instance": NotifyTelegram, |
| 197 |
}, |
| 198 |
), |
| 199 |
( |
| 200 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=markdown&mdv=v2", |
| 201 |
{ |
| 202 |
"instance": NotifyTelegram, |
| 203 |
}, |
| 204 |
), |
| 205 |
( |
| 206 |
"tgram://123456789:abcdefg_hijklmnop/l2g/?format=markdown&mdv=bad", |
| 207 |
{ |
| 208 |
# Defaults to v2 |
| 209 |
"instance": NotifyTelegram, |
| 210 |
}, |
| 211 |
), |
| 212 |
( |
| 213 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=html", |
| 214 |
{ |
| 215 |
"instance": NotifyTelegram, |
| 216 |
}, |
| 217 |
), |
| 218 |
( |
| 219 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?format=text", |
| 220 |
{ |
| 221 |
"instance": NotifyTelegram, |
| 222 |
}, |
| 223 |
), |
| 224 |
# Test Silent Settings |
| 225 |
( |
| 226 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?silent=yes", |
| 227 |
{ |
| 228 |
"instance": NotifyTelegram, |
| 229 |
}, |
| 230 |
), |
| 231 |
( |
| 232 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?silent=no", |
| 233 |
{ |
| 234 |
"instance": NotifyTelegram, |
| 235 |
}, |
| 236 |
), |
| 237 |
# Test Web Page Preview Settings |
| 238 |
( |
| 239 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?preview=yes", |
| 240 |
{ |
| 241 |
"instance": NotifyTelegram, |
| 242 |
}, |
| 243 |
), |
| 244 |
( |
| 245 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?preview=no", |
| 246 |
{ |
| 247 |
"instance": NotifyTelegram, |
| 248 |
}, |
| 249 |
), |
| 250 |
# Simple Message without image |
| 251 |
( |
| 252 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 253 |
{ |
| 254 |
"instance": NotifyTelegram, |
| 255 |
# don't include an image by default |
| 256 |
"include_image": False, |
| 257 |
}, |
| 258 |
), |
| 259 |
# Invalid Bot Token |
| 260 |
( |
| 261 |
"tgram://alpha:abcdefg_hijklmnop/lead2gold/", |
| 262 |
{ |
| 263 |
"instance": None, |
| 264 |
}, |
| 265 |
), |
| 266 |
# AuthToken + bad url |
| 267 |
( |
| 268 |
"tgram://:@/", |
| 269 |
{ |
| 270 |
"instance": None, |
| 271 |
}, |
| 272 |
), |
| 273 |
( |
| 274 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 275 |
{ |
| 276 |
"instance": NotifyTelegram, |
| 277 |
# force a failure |
| 278 |
"response": False, |
| 279 |
"requests_response_code": requests.codes.internal_server_error, |
| 280 |
}, |
| 281 |
), |
| 282 |
( |
| 283 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes", |
| 284 |
{ |
| 285 |
"instance": NotifyTelegram, |
| 286 |
# force a failure without an image specified |
| 287 |
"include_image": False, |
| 288 |
"response": False, |
| 289 |
"requests_response_code": requests.codes.internal_server_error, |
| 290 |
}, |
| 291 |
), |
| 292 |
( |
| 293 |
"tgram://123456789:abcdefg_hijklmnop/id1/id2/", |
| 294 |
{ |
| 295 |
"instance": NotifyTelegram, |
| 296 |
# force a failure with multiple chat_ids |
| 297 |
"response": False, |
| 298 |
"requests_response_code": requests.codes.internal_server_error, |
| 299 |
}, |
| 300 |
), |
| 301 |
( |
| 302 |
"tgram://123456789:abcdefg_hijklmnop/id1/id2/", |
| 303 |
{ |
| 304 |
"instance": NotifyTelegram, |
| 305 |
# force a failure without an image specified |
| 306 |
"include_image": False, |
| 307 |
"response": False, |
| 308 |
"requests_response_code": requests.codes.internal_server_error, |
| 309 |
}, |
| 310 |
), |
| 311 |
( |
| 312 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 313 |
{ |
| 314 |
"instance": NotifyTelegram, |
| 315 |
# throw a bizarre code forcing us to fail to look it up |
| 316 |
"response": False, |
| 317 |
"requests_response_code": 999, |
| 318 |
}, |
| 319 |
), |
| 320 |
( |
| 321 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 322 |
{ |
| 323 |
"instance": NotifyTelegram, |
| 324 |
# throw a bizarre code forcing us to fail to look it up without |
| 325 |
# having an image included |
| 326 |
"include_image": False, |
| 327 |
"response": False, |
| 328 |
"requests_response_code": 999, |
| 329 |
}, |
| 330 |
), |
| 331 |
# Test with image set |
| 332 |
( |
| 333 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes", |
| 334 |
{ |
| 335 |
"instance": NotifyTelegram, |
| 336 |
# throw a bizarre code forcing us to fail to look it up without |
| 337 |
# having an image included |
| 338 |
"include_image": True, |
| 339 |
"response": False, |
| 340 |
"requests_response_code": 999, |
| 341 |
}, |
| 342 |
), |
| 343 |
( |
| 344 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/", |
| 345 |
{ |
| 346 |
"instance": NotifyTelegram, |
| 347 |
# Throws a series of i/o exceptions with this flag |
| 348 |
# is set and tests that we gracefully handle them |
| 349 |
"test_requests_exceptions": True, |
| 350 |
}, |
| 351 |
), |
| 352 |
( |
| 353 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?image=Yes", |
| 354 |
{ |
| 355 |
"instance": NotifyTelegram, |
| 356 |
# Throws a series of i/o exceptions with this flag is set and |
| 357 |
# tests that we gracefully handle them without images set |
| 358 |
"include_image": True, |
| 359 |
"test_requests_exceptions": True, |
| 360 |
}, |
| 361 |
), |
| 362 |
) |
| 363 |
|
| 364 |
|
| 365 |
def test_plugin_telegram_urls(): |
| 366 |
"""NotifyTelegram() Apprise URLs.""" |
| 367 |
|
| 368 |
# Run our general tests |
| 369 |
AppriseURLTester(tests=apprise_url_tests).run_all() |
| 370 |
|
| 371 |
|
| 372 |
@mock.patch("requests.post") |
| 373 |
def test_plugin_telegram_general(mock_post): |
| 374 |
"""NotifyTelegram() General Tests.""" |
| 375 |
|
| 376 |
# Bot Token |
| 377 |
bot_token = "123456789:abcdefg_hijklmnop" |
| 378 |
invalid_bot_token = "abcd:123" |
| 379 |
|
| 380 |
# Chat ID |
| 381 |
chat_ids = "l2g:1234, lead2gold" |
| 382 |
|
| 383 |
# Prepare Mock |
| 384 |
mock_post.return_value = requests.Request() |
| 385 |
mock_post.return_value.status_code = requests.codes.ok |
| 386 |
mock_post.return_value.content = "{}" |
| 387 |
|
| 388 |
# Exception should be thrown about the fact no bot token was specified |
| 389 |
with pytest.raises(AppriseImproperlyConfigured): |
| 390 |
NotifyTelegram(bot_token=None, targets=chat_ids) |
| 391 |
|
| 392 |
# Invalid JSON while trying to detect bot owner |
| 393 |
mock_post.return_value.content = "}" |
| 394 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 395 |
obj.notify(title="hello", body="world") |
| 396 |
|
| 397 |
# Invalid JSON while trying to detect bot owner + 400 error |
| 398 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 399 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 400 |
obj.notify(title="hello", body="world") |
| 401 |
|
| 402 |
# Return status back to how they were |
| 403 |
mock_post.return_value.status_code = requests.codes.ok |
| 404 |
|
| 405 |
# Exception should be thrown about the fact an invalid bot token was |
| 406 |
# specifed |
| 407 |
with pytest.raises(AppriseImproperlyConfigured): |
| 408 |
NotifyTelegram(bot_token=invalid_bot_token, targets=chat_ids) |
| 409 |
|
| 410 |
obj = NotifyTelegram( |
| 411 |
bot_token=bot_token, targets=chat_ids, include_image=True |
| 412 |
) |
| 413 |
assert isinstance(obj, NotifyTelegram) is True |
| 414 |
assert len(obj.targets) == 2 |
| 415 |
|
| 416 |
# Test Image Sending Exceptions |
| 417 |
mock_post.side_effect = OSError() |
| 418 |
assert not obj.send_media(obj.targets[0], NotifyType.INFO) |
| 419 |
|
| 420 |
# Test our other objects |
| 421 |
mock_post.side_effect = requests.HTTPError |
| 422 |
assert not obj.send_media(obj.targets[0], NotifyType.INFO) |
| 423 |
|
| 424 |
# Restore their entries |
| 425 |
mock_post.side_effect = None |
| 426 |
mock_post.return_value.content = "{}" |
| 427 |
|
| 428 |
# test url call |
| 429 |
assert isinstance(obj.url(), str) is True |
| 430 |
|
| 431 |
# test privacy version of url |
| 432 |
assert isinstance(obj.url(privacy=True), str) is True |
| 433 |
assert obj.url(privacy=True).startswith("tgram://1...p/") is True |
| 434 |
|
| 435 |
# Test that we can load the string we generate back: |
| 436 |
obj = NotifyTelegram(**NotifyTelegram.parse_url(obj.url())) |
| 437 |
assert isinstance(obj, NotifyTelegram) is True |
| 438 |
|
| 439 |
# Prepare Mock to fail |
| 440 |
response = mock.Mock() |
| 441 |
response.status_code = requests.codes.internal_server_error |
| 442 |
|
| 443 |
# a error response |
| 444 |
response.content = dumps( |
| 445 |
{ |
| 446 |
"description": "test", |
| 447 |
} |
| 448 |
) |
| 449 |
mock_post.return_value = response |
| 450 |
|
| 451 |
# No image asset |
| 452 |
nimg_obj = NotifyTelegram(bot_token=bot_token, targets=chat_ids) |
| 453 |
nimg_obj.asset = AppriseAsset(image_path_mask=False, image_url_mask=False) |
| 454 |
|
| 455 |
# Test that our default settings over-ride base settings since they are |
| 456 |
# not the same as the one specified in the base; this check merely |
| 457 |
# ensures our plugin inheritance is working properly |
| 458 |
assert obj.body_maxlen == NotifyTelegram.body_maxlen |
| 459 |
|
| 460 |
# This tests erroneous messages involving multiple chat ids |
| 461 |
assert ( |
| 462 |
bool( |
| 463 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 464 |
) |
| 465 |
is False |
| 466 |
) |
| 467 |
assert ( |
| 468 |
bool( |
| 469 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 470 |
) |
| 471 |
is False |
| 472 |
) |
| 473 |
assert ( |
| 474 |
bool( |
| 475 |
nimg_obj.notify( |
| 476 |
body="body", title="title", notify_type=NotifyType.INFO |
| 477 |
) |
| 478 |
) |
| 479 |
is False |
| 480 |
) |
| 481 |
|
| 482 |
# This tests erroneous messages involving a single chat id |
| 483 |
obj = NotifyTelegram(bot_token=bot_token, targets="l2g") |
| 484 |
nimg_obj = NotifyTelegram(bot_token=bot_token, targets="l2g") |
| 485 |
nimg_obj.asset = AppriseAsset(image_path_mask=False, image_url_mask=False) |
| 486 |
|
| 487 |
assert ( |
| 488 |
bool( |
| 489 |
obj.notify(body="body", title="title", notify_type=NotifyType.INFO) |
| 490 |
) |
| 491 |
is False |
| 492 |
) |
| 493 |
assert ( |
| 494 |
bool( |
| 495 |
nimg_obj.notify( |
| 496 |
body="body", title="title", notify_type=NotifyType.INFO |
| 497 |
) |
| 498 |
) |
| 499 |
is False |
| 500 |
) |
| 501 |
|
| 502 |
# Bot Token Detection |
| 503 |
# Just to make it clear to people reading this code and trying to learn |
| 504 |
# what is going on. Apprise tries to detect the bot owner if you don't |
| 505 |
# specify a user to message. The idea is to just default to messaging |
| 506 |
# the bot owner himself (it makes it easier for people). So we're testing |
| 507 |
# the creating of a Telegram Notification without providing a chat ID. |
| 508 |
# We're testing the error handling of this bot detection section of the |
| 509 |
# code |
| 510 |
mock_post.return_value.content = dumps( |
| 511 |
{ |
| 512 |
"ok": True, |
| 513 |
"result": [ |
| 514 |
{ |
| 515 |
"update_id": 645421319, |
| 516 |
# Entry without `message` in it |
| 517 |
}, |
| 518 |
{ |
| 519 |
# Entry without `from` in `message` |
| 520 |
"update_id": 645421320, |
| 521 |
"message": { |
| 522 |
"message_id": 2, |
| 523 |
"chat": { |
| 524 |
"id": 532389719, |
| 525 |
"first_name": "Chris", |
| 526 |
"type": "private", |
| 527 |
}, |
| 528 |
"date": 1519694394, |
| 529 |
"text": "/start", |
| 530 |
"entities": [ |
| 531 |
{ |
| 532 |
"offset": 0, |
| 533 |
"length": 6, |
| 534 |
"type": "bot_command", |
| 535 |
} |
| 536 |
], |
| 537 |
}, |
| 538 |
}, |
| 539 |
{ |
| 540 |
"update_id": 645421321, |
| 541 |
"message": { |
| 542 |
"message_id": 2, |
| 543 |
"from": { |
| 544 |
"id": 532389719, |
| 545 |
"is_bot": False, |
| 546 |
"first_name": "Chris", |
| 547 |
"language_code": "en-US", |
| 548 |
}, |
| 549 |
"chat": { |
| 550 |
"id": 532389719, |
| 551 |
"first_name": "Chris", |
| 552 |
"type": "private", |
| 553 |
}, |
| 554 |
"date": 1519694394, |
| 555 |
"text": "/start", |
| 556 |
"entities": [ |
| 557 |
{ |
| 558 |
"offset": 0, |
| 559 |
"length": 6, |
| 560 |
"type": "bot_command", |
| 561 |
} |
| 562 |
], |
| 563 |
}, |
| 564 |
}, |
| 565 |
], |
| 566 |
} |
| 567 |
) |
| 568 |
mock_post.return_value.status_code = requests.codes.ok |
| 569 |
|
| 570 |
obj = NotifyTelegram(bot_token=bot_token, targets="12345") |
| 571 |
assert len(obj.targets) == 1 |
| 572 |
assert obj.targets[0] == (12345, None) |
| 573 |
|
| 574 |
# Test the escaping of characters since Telegram escapes stuff for us to |
| 575 |
# which we need to consider |
| 576 |
mock_post.reset_mock() |
| 577 |
body = "<p>'\"This can't\t\r\nfail us\"'</p>" |
| 578 |
assert ( |
| 579 |
bool( |
| 580 |
obj.notify( |
| 581 |
body=body, |
| 582 |
title="special characters", |
| 583 |
notify_type=NotifyType.INFO, |
| 584 |
) |
| 585 |
) |
| 586 |
is True |
| 587 |
) |
| 588 |
assert mock_post.call_count == 1 |
| 589 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 590 |
|
| 591 |
# Test our payload |
| 592 |
assert ( |
| 593 |
payload["text"] |
| 594 |
== "<b>special characters</b>\r\n'\"This can't\t\r\nfail us\"'\r\n" |
| 595 |
) |
| 596 |
|
| 597 |
for content in ("before", "after"): |
| 598 |
# Test our content settings |
| 599 |
obj = NotifyTelegram( |
| 600 |
bot_token=bot_token, targets="12345", content=content |
| 601 |
) |
| 602 |
# Reset our mock |
| 603 |
mock_post.reset_mock() |
| 604 |
# Test sending attachments |
| 605 |
attach = AppriseAttachment( |
| 606 |
os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 607 |
) |
| 608 |
assert ( |
| 609 |
bool( |
| 610 |
obj.notify( |
| 611 |
body="body", |
| 612 |
title="title", |
| 613 |
notify_type=NotifyType.INFO, |
| 614 |
attach=attach, |
| 615 |
) |
| 616 |
) |
| 617 |
is True |
| 618 |
) |
| 619 |
|
| 620 |
# Test large messages |
| 621 |
assert ( |
| 622 |
bool( |
| 623 |
obj.notify( |
| 624 |
body="a" * (obj.telegram_caption_maxlen + 1), |
| 625 |
title="title", |
| 626 |
notify_type=NotifyType.INFO, |
| 627 |
attach=attach, |
| 628 |
) |
| 629 |
) |
| 630 |
is True |
| 631 |
) |
| 632 |
|
| 633 |
# An invalid attachment will cause a failure |
| 634 |
path = os.path.join( |
| 635 |
TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg" |
| 636 |
) |
| 637 |
attach = AppriseAttachment(path) |
| 638 |
assert ( |
| 639 |
bool( |
| 640 |
obj.notify( |
| 641 |
body="body", |
| 642 |
title="title", |
| 643 |
notify_type=NotifyType.INFO, |
| 644 |
attach=path, |
| 645 |
) |
| 646 |
) |
| 647 |
is False |
| 648 |
) |
| 649 |
|
| 650 |
# Test large messages |
| 651 |
assert ( |
| 652 |
bool( |
| 653 |
obj.notify( |
| 654 |
body="a" * (obj.telegram_caption_maxlen + 1), |
| 655 |
title="title", |
| 656 |
notify_type=NotifyType.INFO, |
| 657 |
attach=path, |
| 658 |
) |
| 659 |
) |
| 660 |
is False |
| 661 |
) |
| 662 |
|
| 663 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 664 |
# No user detected; this happens after our firsst notification |
| 665 |
assert len(obj.targets) == 0 |
| 666 |
|
| 667 |
assert bool(obj.notify(title="hello", body="world")) is True |
| 668 |
assert len(obj.targets) == 1 |
| 669 |
assert obj.targets[0] == ("532389719", None) |
| 670 |
|
| 671 |
# Do the test again, but without the expected (parsed response) |
| 672 |
mock_post.return_value.content = dumps( |
| 673 |
{ |
| 674 |
"ok": True, |
| 675 |
"result": [], |
| 676 |
} |
| 677 |
) |
| 678 |
|
| 679 |
# No user will be detected now |
| 680 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 681 |
# No user detected; this happens after our firsst notification |
| 682 |
assert len(obj.targets) == 0 |
| 683 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 684 |
assert len(obj.targets) == 0 |
| 685 |
|
| 686 |
# Do the test again, but with ok not set to True |
| 687 |
mock_post.return_value.content = dumps( |
| 688 |
{ |
| 689 |
"ok": False, |
| 690 |
"result": [ |
| 691 |
{ |
| 692 |
"update_id": 645421321, |
| 693 |
"message": { |
| 694 |
"message_id": 2, |
| 695 |
"from": { |
| 696 |
"id": 532389719, |
| 697 |
"is_bot": False, |
| 698 |
"first_name": "Chris", |
| 699 |
"language_code": "en-US", |
| 700 |
}, |
| 701 |
"chat": { |
| 702 |
"id": 532389719, |
| 703 |
"first_name": "Chris", |
| 704 |
"type": "private", |
| 705 |
}, |
| 706 |
"date": 1519694394, |
| 707 |
"text": "/start", |
| 708 |
"entities": [ |
| 709 |
{ |
| 710 |
"offset": 0, |
| 711 |
"length": 6, |
| 712 |
"type": "bot_command", |
| 713 |
} |
| 714 |
], |
| 715 |
}, |
| 716 |
}, |
| 717 |
], |
| 718 |
} |
| 719 |
) |
| 720 |
|
| 721 |
# No user will be detected now |
| 722 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 723 |
# No user detected; this happens after our firsst notification |
| 724 |
assert len(obj.targets) == 0 |
| 725 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 726 |
assert len(obj.targets) == 0 |
| 727 |
|
| 728 |
# An edge case where no results were provided; this will probably never |
| 729 |
# happen, but it helps with test coverage completeness |
| 730 |
mock_post.return_value.content = dumps( |
| 731 |
{ |
| 732 |
"ok": True, |
| 733 |
} |
| 734 |
) |
| 735 |
|
| 736 |
# No user will be detected now |
| 737 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 738 |
# No user detected; this happens after our firsst notification |
| 739 |
assert len(obj.targets) == 0 |
| 740 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 741 |
assert len(obj.targets) == 0 |
| 742 |
# Detect the bot with a bad response |
| 743 |
mock_post.return_value.content = dumps({}) |
| 744 |
obj.detect_bot_owner() |
| 745 |
|
| 746 |
# Test our bot detection with a internal server error |
| 747 |
mock_post.return_value.status_code = requests.codes.internal_server_error |
| 748 |
|
| 749 |
# internal server error prevents notification from being sent |
| 750 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 751 |
assert len(obj.targets) == 0 |
| 752 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 753 |
assert len(obj.targets) == 0 |
| 754 |
|
| 755 |
# Test our bot detection with an unmappable html error |
| 756 |
mock_post.return_value.status_code = 999 |
| 757 |
NotifyTelegram(bot_token=bot_token, targets=None) |
| 758 |
assert len(obj.targets) == 0 |
| 759 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 760 |
assert len(obj.targets) == 0 |
| 761 |
|
| 762 |
# Do it again but this time provide a failure message |
| 763 |
mock_post.return_value.content = dumps({"description": "Failure Message"}) |
| 764 |
NotifyTelegram(bot_token=bot_token, targets=None) |
| 765 |
assert len(obj.targets) == 0 |
| 766 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 767 |
assert len(obj.targets) == 0 |
| 768 |
|
| 769 |
# Do it again but this time provide a failure message and perform a |
| 770 |
# notification without a bot detection by providing at least 1 chat id |
| 771 |
obj = NotifyTelegram(bot_token=bot_token, targets=["@abcd"]) |
| 772 |
assert ( |
| 773 |
bool( |
| 774 |
nimg_obj.notify( |
| 775 |
body="body", title="title", notify_type=NotifyType.INFO |
| 776 |
) |
| 777 |
) |
| 778 |
is False |
| 779 |
) |
| 780 |
|
| 781 |
# iterate over our exceptions and test them |
| 782 |
mock_post.side_effect = requests.HTTPError |
| 783 |
|
| 784 |
# No chat_ids specified |
| 785 |
obj = NotifyTelegram(bot_token=bot_token, targets=None) |
| 786 |
assert len(obj.targets) == 0 |
| 787 |
assert bool(obj.notify(title="hello", body="world")) is False |
| 788 |
assert len(obj.targets) == 0 |
| 789 |
|
| 790 |
# Test Telegram Group |
| 791 |
obj = Apprise.instantiate( |
| 792 |
"tgram://123456789:ABCdefghijkl123456789opqyz/-123456789525" |
| 793 |
) |
| 794 |
assert isinstance(obj, NotifyTelegram) |
| 795 |
assert len(obj.targets) == 1 |
| 796 |
assert (-123456789525, None) in obj.targets |
| 797 |
|
| 798 |
|
| 799 |
@mock.patch("requests.post") |
| 800 |
def test_plugin_telegram_formatting(mock_post): |
| 801 |
"""NotifyTelegram() formatting tests.""" |
| 802 |
|
| 803 |
# Prepare Mock |
| 804 |
mock_post.return_value = requests.Request() |
| 805 |
mock_post.return_value.status_code = requests.codes.ok |
| 806 |
mock_post.return_value.content = "{}" |
| 807 |
|
| 808 |
# Simple success response |
| 809 |
mock_post.return_value.content = dumps( |
| 810 |
{ |
| 811 |
"ok": True, |
| 812 |
"result": [ |
| 813 |
{ |
| 814 |
"update_id": 645421321, |
| 815 |
"message": { |
| 816 |
"message_id": 2, |
| 817 |
"from": { |
| 818 |
"id": 532389719, |
| 819 |
"is_bot": False, |
| 820 |
"first_name": "Chris", |
| 821 |
"language_code": "en-US", |
| 822 |
}, |
| 823 |
"chat": { |
| 824 |
"id": 532389719, |
| 825 |
"first_name": "Chris", |
| 826 |
"type": "private", |
| 827 |
}, |
| 828 |
"date": 1519694394, |
| 829 |
"text": "/start", |
| 830 |
"entities": [ |
| 831 |
{ |
| 832 |
"offset": 0, |
| 833 |
"length": 6, |
| 834 |
"type": "bot_command", |
| 835 |
} |
| 836 |
], |
| 837 |
}, |
| 838 |
}, |
| 839 |
], |
| 840 |
} |
| 841 |
) |
| 842 |
mock_post.return_value.status_code = requests.codes.ok |
| 843 |
|
| 844 |
results = NotifyTelegram.parse_url("tgram://123456789:abcdefg_hijklmnop/") |
| 845 |
|
| 846 |
instance = NotifyTelegram(**results) |
| 847 |
assert isinstance(instance, NotifyTelegram) |
| 848 |
|
| 849 |
response = instance.send(title="title", body="body") |
| 850 |
assert response is True |
| 851 |
# 1 call to look up bot owner, and second for notification |
| 852 |
assert mock_post.call_count == 2 |
| 853 |
|
| 854 |
assert ( |
| 855 |
mock_post.call_args_list[0][0][0] |
| 856 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/getUpdates" |
| 857 |
) |
| 858 |
assert ( |
| 859 |
mock_post.call_args_list[1][0][0] |
| 860 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/sendMessage" |
| 861 |
) |
| 862 |
|
| 863 |
# Reset our values |
| 864 |
mock_post.reset_mock() |
| 865 |
|
| 866 |
# Now test our HTML Conversion as TEXT) |
| 867 |
aobj = Apprise() |
| 868 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/") |
| 869 |
assert len(aobj) == 1 |
| 870 |
|
| 871 |
title = "🚨 Change detected for <i>Apprise Test Title</i>" |
| 872 |
body = ( |
| 873 |
'<a href="http://localhost"><i>Apprise Body Title</i></a>' |
| 874 |
' had <a href="http://127.0.0.1">a change</a>' |
| 875 |
) |
| 876 |
|
| 877 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 878 |
|
| 879 |
# Test our calls |
| 880 |
assert mock_post.call_count == 2 |
| 881 |
|
| 882 |
assert ( |
| 883 |
mock_post.call_args_list[0][0][0] |
| 884 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/getUpdates" |
| 885 |
) |
| 886 |
assert ( |
| 887 |
mock_post.call_args_list[1][0][0] |
| 888 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/sendMessage" |
| 889 |
) |
| 890 |
|
| 891 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 892 |
|
| 893 |
# Test that everything is escaped properly in a TEXT mode |
| 894 |
assert ( |
| 895 |
payload["text"] |
| 896 |
== "<b>🚨 Change detected for <i>Apprise Test Title</i>" |
| 897 |
'</b>\r\n<a href="http://localhost"><i>' |
| 898 |
"Apprise Body Title</i></a> had <" |
| 899 |
'a href="http://127.0.0.1">a change</a>' |
| 900 |
) |
| 901 |
|
| 902 |
# Reset our values |
| 903 |
mock_post.reset_mock() |
| 904 |
|
| 905 |
# Now test our HTML Conversion as TEXT) |
| 906 |
aobj = Apprise() |
| 907 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?format=html") |
| 908 |
assert len(aobj) == 1 |
| 909 |
|
| 910 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.HTML) |
| 911 |
|
| 912 |
# Test our calls |
| 913 |
assert mock_post.call_count == 2 |
| 914 |
|
| 915 |
assert ( |
| 916 |
mock_post.call_args_list[0][0][0] |
| 917 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/getUpdates" |
| 918 |
) |
| 919 |
assert ( |
| 920 |
mock_post.call_args_list[1][0][0] |
| 921 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/sendMessage" |
| 922 |
) |
| 923 |
|
| 924 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 925 |
|
| 926 |
# Test that everything is escaped properly in a HTML mode |
| 927 |
assert ( |
| 928 |
payload["text"] |
| 929 |
== "<b>🚨 Change detected for <i>Apprise Test Title</i></b>\r\n" |
| 930 |
'<a href="http://localhost"><i>Apprise Body Title</i></a> had ' |
| 931 |
'<a href="http://127.0.0.1">a change</a>' |
| 932 |
) |
| 933 |
|
| 934 |
# Reset our values |
| 935 |
mock_post.reset_mock() |
| 936 |
|
| 937 |
# Now test our MARKDOWN Handling |
| 938 |
title = "# 🚨 Change detected for _Apprise Test Title_" |
| 939 |
body = ( |
| 940 |
"_[Apprise Body Title](http://localhost)_" |
| 941 |
" had [a change](http://127.0.0.1)" |
| 942 |
) |
| 943 |
|
| 944 |
aobj = Apprise() |
| 945 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?format=markdown&mdv=2") |
| 946 |
assert len(aobj) == 1 |
| 947 |
|
| 948 |
assert aobj.notify( |
| 949 |
title=title, body=body, body_format=NotifyFormat.MARKDOWN |
| 950 |
) |
| 951 |
|
| 952 |
# Test our calls |
| 953 |
assert mock_post.call_count == 2 |
| 954 |
|
| 955 |
assert ( |
| 956 |
mock_post.call_args_list[0][0][0] |
| 957 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/getUpdates" |
| 958 |
) |
| 959 |
assert ( |
| 960 |
mock_post.call_args_list[1][0][0] |
| 961 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/sendMessage" |
| 962 |
) |
| 963 |
|
| 964 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 965 |
|
| 966 |
# MarkdownV2 renders the merged title heading as bold. |
| 967 |
assert ( |
| 968 |
payload["text"] == "*🚨 Change detected for _Apprise Test Title_*\n" |
| 969 |
"_[Apprise Body Title](http://localhost)_ had " |
| 970 |
"[a change](http://127.0.0.1)" |
| 971 |
) |
| 972 |
|
| 973 |
# Reset our values |
| 974 |
mock_post.reset_mock() |
| 975 |
|
| 976 |
# Now test our MARKDOWN Handling |
| 977 |
title = "# 🚨 Change detected for _Apprise Test Title_" |
| 978 |
body = ( |
| 979 |
"_[Apprise Body Title](http://localhost)_" |
| 980 |
" had [a change](http://127.0.0.1)" |
| 981 |
) |
| 982 |
|
| 983 |
aobj = Apprise() |
| 984 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?format=markdown&mdv=1") |
| 985 |
assert len(aobj) == 1 |
| 986 |
|
| 987 |
assert aobj.notify( |
| 988 |
title=title, body=body, body_format=NotifyFormat.MARKDOWN |
| 989 |
) |
| 990 |
|
| 991 |
# Test our calls |
| 992 |
assert mock_post.call_count == 2 |
| 993 |
|
| 994 |
assert ( |
| 995 |
mock_post.call_args_list[0][0][0] |
| 996 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/getUpdates" |
| 997 |
) |
| 998 |
assert ( |
| 999 |
mock_post.call_args_list[1][0][0] |
| 1000 |
== "https://api.telegram.org/bot123456789:abcdefg_hijklmnop/sendMessage" |
| 1001 |
) |
| 1002 |
|
| 1003 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1004 |
|
| 1005 |
# Telegram v1 renders the heading as bold and drops nested italic markers. |
| 1006 |
assert ( |
| 1007 |
payload["text"] == "*🚨 Change detected for Apprise Test Title*\n" |
| 1008 |
"_[Apprise Body Title](http://localhost)_ had " |
| 1009 |
"[a change](http://127.0.0.1)" |
| 1010 |
) |
| 1011 |
|
| 1012 |
# Reset our values |
| 1013 |
mock_post.reset_mock() |
| 1014 |
|
| 1015 |
# Upstream to use HTML but input specified as Markdown |
| 1016 |
aobj = Apprise() |
| 1017 |
aobj.add("tgram://987654321:abcdefg_hijklmnop/?format=html") |
| 1018 |
assert len(aobj) == 1 |
| 1019 |
|
| 1020 |
# Now test our MARKDOWN Handling |
| 1021 |
title = "# 🚨 Another Change detected for _Apprise Test Title_" |
| 1022 |
body = ( |
| 1023 |
"_[Apprise Body Title](http://localhost)_" |
| 1024 |
" had [a change](http://127.0.0.2)" |
| 1025 |
) |
| 1026 |
|
| 1027 |
# HTML forced by the command line, but MARKDOWN specified as |
| 1028 |
# upstream mode |
| 1029 |
assert aobj.notify( |
| 1030 |
title=title, body=body, body_format=NotifyFormat.MARKDOWN |
| 1031 |
) |
| 1032 |
|
| 1033 |
# Test our calls |
| 1034 |
assert mock_post.call_count == 2 |
| 1035 |
|
| 1036 |
assert ( |
| 1037 |
mock_post.call_args_list[0][0][0] |
| 1038 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/getUpdates" |
| 1039 |
) |
| 1040 |
assert ( |
| 1041 |
mock_post.call_args_list[1][0][0] |
| 1042 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1043 |
) |
| 1044 |
|
| 1045 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1046 |
|
| 1047 |
# Test that everything is escaped properly in a HTML mode |
| 1048 |
assert ( |
| 1049 |
payload["text"] == "<b>\r\n<b>🚨 Another Change detected for " |
| 1050 |
"<i>Apprise Test Title</i></b>\r\n</b>\r\n<i>" |
| 1051 |
'<a href="http://localhost">Apprise Body Title</a>' |
| 1052 |
'</i> had <a href="http://127.0.0.2">a change</a>\r\n' |
| 1053 |
) |
| 1054 |
|
| 1055 |
# Now we'll test an edge case where a title was defined, but after |
| 1056 |
# processing it, it was determiend there really wasn't anything there |
| 1057 |
# at all at the end of the day. |
| 1058 |
|
| 1059 |
# Reset our values |
| 1060 |
mock_post.reset_mock() |
| 1061 |
|
| 1062 |
# Upstream to use HTML but input specified as Markdown v1 |
| 1063 |
aobj = Apprise() |
| 1064 |
aobj.add("tgram://987654321:abcdefg_hijklmnop/?format=markdown&mdv=1") |
| 1065 |
assert len(aobj) == 1 |
| 1066 |
|
| 1067 |
# Now test our MARKDOWN Handling (no title defined... not really anyway) |
| 1068 |
title = "# " |
| 1069 |
body = ( |
| 1070 |
"_[Apprise Body Title](http://localhost)_" |
| 1071 |
" had [a change](http://127.0.0.2)" |
| 1072 |
) |
| 1073 |
|
| 1074 |
# MARKDOWN forced by the command line, but TEXT specified as |
| 1075 |
# upstream mode |
| 1076 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1077 |
|
| 1078 |
# Test our calls |
| 1079 |
assert mock_post.call_count == 2 |
| 1080 |
|
| 1081 |
assert ( |
| 1082 |
mock_post.call_args_list[0][0][0] |
| 1083 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/getUpdates" |
| 1084 |
) |
| 1085 |
assert ( |
| 1086 |
mock_post.call_args_list[1][0][0] |
| 1087 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1088 |
) |
| 1089 |
|
| 1090 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1091 |
|
| 1092 |
# The merged ``# #`` heading renders its literal hash as bold. |
| 1093 |
assert payload["text"] == ( |
| 1094 |
"*#*\n" |
| 1095 |
r"\_\[Apprise Body Title\](http://localhost)\_" |
| 1096 |
r" had \[a change\](http://127.0.0.2)" |
| 1097 |
) |
| 1098 |
|
| 1099 |
# Reset our values |
| 1100 |
mock_post.reset_mock() |
| 1101 |
|
| 1102 |
# Upstream to use HTML but input specified as Markdown v2 |
| 1103 |
aobj = Apprise() |
| 1104 |
aobj.add("tgram://987654321:abcdefg_hijklmnop/?format=markdown&mdv=2") |
| 1105 |
assert len(aobj) == 1 |
| 1106 |
|
| 1107 |
# MARKDOWN forced by the command line, but TEXT specified as |
| 1108 |
# upstream mode |
| 1109 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1110 |
|
| 1111 |
# Test our calls |
| 1112 |
assert mock_post.call_count == 2 |
| 1113 |
|
| 1114 |
assert ( |
| 1115 |
mock_post.call_args_list[0][0][0] |
| 1116 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/getUpdates" |
| 1117 |
) |
| 1118 |
assert ( |
| 1119 |
mock_post.call_args_list[1][0][0] |
| 1120 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1121 |
) |
| 1122 |
|
| 1123 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1124 |
|
| 1125 |
# MarkdownV2 also escapes the literal hash inside the bold heading. |
| 1126 |
assert payload["text"] == ( |
| 1127 |
"*\\#*\n" |
| 1128 |
r"\_\[Apprise Body Title\]\(http://localhost\)\_" |
| 1129 |
r" had \[a change\]\(http://127\.0\.0\.2\)" |
| 1130 |
) |
| 1131 |
|
| 1132 |
# Reset our values |
| 1133 |
mock_post.reset_mock() |
| 1134 |
|
| 1135 |
# Upstream to use HTML but input specified as Markdown v1 |
| 1136 |
aobj = Apprise() |
| 1137 |
aobj.add("tgram://987654321:abcdefg_hijklmnop/?format=markdown&mdv=1") |
| 1138 |
assert len(aobj) == 1 |
| 1139 |
|
| 1140 |
# Set an actual title this time |
| 1141 |
title = "# A Great Title" |
| 1142 |
body = ( |
| 1143 |
"_[Apprise Body Title](http://localhost)_" |
| 1144 |
" had [a change](http://127.0.0.2)" |
| 1145 |
) |
| 1146 |
|
| 1147 |
# TEXT forced by the command line, but MARKDOWN specified as |
| 1148 |
# upstream mode |
| 1149 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1150 |
|
| 1151 |
# Test our calls |
| 1152 |
assert mock_post.call_count == 2 |
| 1153 |
|
| 1154 |
assert ( |
| 1155 |
mock_post.call_args_list[0][0][0] |
| 1156 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/getUpdates" |
| 1157 |
) |
| 1158 |
assert ( |
| 1159 |
mock_post.call_args_list[1][0][0] |
| 1160 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1161 |
) |
| 1162 |
|
| 1163 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1164 |
|
| 1165 |
# v1's narrower escape set un-escapes what the framework's baseline |
| 1166 |
# text-to-markdown pass added, since v1 does not require it. The merged |
| 1167 |
# "# # A Great Title" line is itself a heading, so it renders as bold. |
| 1168 |
assert payload["text"] == ( |
| 1169 |
"*# A Great Title*\n" |
| 1170 |
r"\_\[Apprise Body Title\](http://localhost)\_" |
| 1171 |
r" had \[a change\](http://127.0.0.2)" |
| 1172 |
) |
| 1173 |
|
| 1174 |
# Reset our values |
| 1175 |
mock_post.reset_mock() |
| 1176 |
|
| 1177 |
# Upstream to use HTML but input specified as Markdown v2 |
| 1178 |
aobj = Apprise() |
| 1179 |
aobj.add("tgram://987654321:abcdefg_hijklmnop/?format=markdown&mdv=2") |
| 1180 |
assert len(aobj) == 1 |
| 1181 |
|
| 1182 |
# TEXT forced by the command line, but MARKDOWN specified as |
| 1183 |
# upstream mode |
| 1184 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1185 |
|
| 1186 |
# Test our calls |
| 1187 |
assert mock_post.call_count == 2 |
| 1188 |
|
| 1189 |
assert ( |
| 1190 |
mock_post.call_args_list[0][0][0] |
| 1191 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/getUpdates" |
| 1192 |
) |
| 1193 |
assert ( |
| 1194 |
mock_post.call_args_list[1][0][0] |
| 1195 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1196 |
) |
| 1197 |
|
| 1198 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1199 |
|
| 1200 |
# MarkdownV2 runs its completion pass over the already-amalgamated |
| 1201 |
# title+body; the merged heading line renders as bold, and its |
| 1202 |
# literal leading hash still needs its own reserved-character escape. |
| 1203 |
assert payload["text"] == ( |
| 1204 |
"*\\# A Great Title*\n" |
| 1205 |
r"\_\[Apprise Body Title\]\(http://localhost\)\_" |
| 1206 |
r" had \[a change\]\(http://127\.0\.0\.2\)" |
| 1207 |
) |
| 1208 |
|
| 1209 |
# Reset our values |
| 1210 |
mock_post.reset_mock() |
| 1211 |
|
| 1212 |
# Declared Markdown gets MarkdownV2 dialect completion. |
| 1213 |
assert aobj.notify( |
| 1214 |
title=title, body=body, body_format=NotifyFormat.MARKDOWN |
| 1215 |
) |
| 1216 |
|
| 1217 |
# Test our calls |
| 1218 |
assert mock_post.call_count == 1 |
| 1219 |
|
| 1220 |
assert ( |
| 1221 |
mock_post.call_args_list[0][0][0] |
| 1222 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1223 |
) |
| 1224 |
|
| 1225 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1226 |
|
| 1227 |
# The declared heading renders as bold. Link destinations only need |
| 1228 |
# "(" and ")" escaped, not the full reserved-character set. |
| 1229 |
assert payload["text"] == ( |
| 1230 |
"*A Great Title*\n" |
| 1231 |
"_[Apprise Body Title](http://localhost)_ had " |
| 1232 |
"[a change](http://127.0.0.2)" |
| 1233 |
) |
| 1234 |
|
| 1235 |
# Reset our values |
| 1236 |
mock_post.reset_mock() |
| 1237 |
|
| 1238 |
# No body format specified at all... user definitely must know what |
| 1239 |
# they are doing... still no escaping in this circumstance |
| 1240 |
assert aobj.notify(title=title, body=body) |
| 1241 |
|
| 1242 |
# Test our calls |
| 1243 |
assert mock_post.call_count == 1 |
| 1244 |
|
| 1245 |
assert ( |
| 1246 |
mock_post.call_args_list[0][0][0] |
| 1247 |
== "https://api.telegram.org/bot987654321:abcdefg_hijklmnop/sendMessage" |
| 1248 |
) |
| 1249 |
|
| 1250 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1251 |
|
| 1252 |
# No escaping in this circumstance |
| 1253 |
assert ( |
| 1254 |
payload["text"] == "# A Great Title\r\n" |
| 1255 |
"_[Apprise Body Title](http://localhost)_ had " |
| 1256 |
"[a change](http://127.0.0.2)" |
| 1257 |
) |
| 1258 |
|
| 1259 |
# Reset our values |
| 1260 |
mock_post.reset_mock() |
| 1261 |
|
| 1262 |
# |
| 1263 |
# Markdown input aligns directly, then gets title merging and v1 |
| 1264 |
# dialect completion without an HTML round trip. |
| 1265 |
# |
| 1266 |
title = "Test Message Title" |
| 1267 |
body = "Test Message Body <br/> ok</br>" |
| 1268 |
|
| 1269 |
aobj = Apprise() |
| 1270 |
aobj.add("tgram://1234:aaaaaaaaa/-1123456245134") |
| 1271 |
assert len(aobj) == 1 |
| 1272 |
|
| 1273 |
assert aobj.notify( |
| 1274 |
title=title, body=body, body_format=NotifyFormat.MARKDOWN |
| 1275 |
) |
| 1276 |
|
| 1277 |
# Test our calls |
| 1278 |
assert mock_post.call_count == 1 |
| 1279 |
|
| 1280 |
assert ( |
| 1281 |
mock_post.call_args_list[0][0][0] |
| 1282 |
== "https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage" |
| 1283 |
) |
| 1284 |
|
| 1285 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1286 |
|
| 1287 |
# The merged heading renders as bold; v1 keeps HTML-looking body text |
| 1288 |
# unescaped. |
| 1289 |
assert payload["text"] == ( |
| 1290 |
"*Test Message Title*\nTest Message Body <br/> ok</br>" |
| 1291 |
) |
| 1292 |
|
| 1293 |
# Reset our values |
| 1294 |
mock_post.reset_mock() |
| 1295 |
|
| 1296 |
# |
| 1297 |
# Now test that <br/> is correctly escaped as it would have been via the |
| 1298 |
# CLI mode where the body_format is TEXT |
| 1299 |
# |
| 1300 |
|
| 1301 |
aobj = Apprise() |
| 1302 |
aobj.add("tgram://1234:aaaaaaaaa/-1123456245134") |
| 1303 |
assert len(aobj) == 1 |
| 1304 |
|
| 1305 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1306 |
|
| 1307 |
# Test our calls |
| 1308 |
assert mock_post.call_count == 1 |
| 1309 |
|
| 1310 |
assert ( |
| 1311 |
mock_post.call_args_list[0][0][0] |
| 1312 |
== "https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage" |
| 1313 |
) |
| 1314 |
|
| 1315 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1316 |
|
| 1317 |
# Test that everything is escaped properly in a HTML mode |
| 1318 |
assert ( |
| 1319 |
payload["text"] == "<b>Test Message Title</b>\r\n" |
| 1320 |
"Test Message Body <br/> ok</br>" |
| 1321 |
) |
| 1322 |
|
| 1323 |
# Reset our values |
| 1324 |
mock_post.reset_mock() |
| 1325 |
|
| 1326 |
# |
| 1327 |
# Now test that <br/> is correctly escaped if fed as HTML |
| 1328 |
# |
| 1329 |
|
| 1330 |
aobj = Apprise() |
| 1331 |
aobj.add("tgram://1234:aaaaaaaaa/-1123456245134") |
| 1332 |
assert len(aobj) == 1 |
| 1333 |
|
| 1334 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.HTML) |
| 1335 |
|
| 1336 |
# Test our calls |
| 1337 |
assert mock_post.call_count == 1 |
| 1338 |
|
| 1339 |
assert ( |
| 1340 |
mock_post.call_args_list[0][0][0] |
| 1341 |
== "https://api.telegram.org/bot1234:aaaaaaaaa/sendMessage" |
| 1342 |
) |
| 1343 |
|
| 1344 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1345 |
|
| 1346 |
# Test that everything is escaped properly in a HTML mode |
| 1347 |
assert ( |
| 1348 |
payload["text"] |
| 1349 |
== "<b>Test Message Title</b>\r\nTest Message Body\r\nok\r\n" |
| 1350 |
) |
| 1351 |
|
| 1352 |
|
| 1353 |
@mock.patch("requests.post") |
| 1354 |
def test_plugin_telegram_html_formatting(mock_post): |
| 1355 |
"""NotifyTelegram() HTML Formatting.""" |
| 1356 |
# Prepare Mock |
| 1357 |
mock_post.return_value = requests.Request() |
| 1358 |
mock_post.return_value.status_code = requests.codes.ok |
| 1359 |
|
| 1360 |
# Simple success response |
| 1361 |
mock_post.return_value.content = dumps( |
| 1362 |
{ |
| 1363 |
"ok": True, |
| 1364 |
"result": [ |
| 1365 |
{ |
| 1366 |
"update_id": 645421321, |
| 1367 |
"message": { |
| 1368 |
"message_id": 2, |
| 1369 |
"from": { |
| 1370 |
"id": 532389719, |
| 1371 |
"is_bot": False, |
| 1372 |
"first_name": "Chris", |
| 1373 |
"language_code": "en-US", |
| 1374 |
}, |
| 1375 |
"chat": { |
| 1376 |
"id": 532389719, |
| 1377 |
"first_name": "Chris", |
| 1378 |
"type": "private", |
| 1379 |
}, |
| 1380 |
"date": 1519694394, |
| 1381 |
"text": "/start", |
| 1382 |
"entities": [ |
| 1383 |
{ |
| 1384 |
"offset": 0, |
| 1385 |
"length": 6, |
| 1386 |
"type": "bot_command", |
| 1387 |
} |
| 1388 |
], |
| 1389 |
}, |
| 1390 |
}, |
| 1391 |
], |
| 1392 |
} |
| 1393 |
) |
| 1394 |
|
| 1395 |
aobj = Apprise() |
| 1396 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/") |
| 1397 |
|
| 1398 |
assert len(aobj) == 1 |
| 1399 |
|
| 1400 |
assert isinstance(aobj[0], NotifyTelegram) |
| 1401 |
|
| 1402 |
# Test our HTML Conversion |
| 1403 |
title = "<title>'information&apos</title>" |
| 1404 |
body = ( |
| 1405 |
"<em>"This is in Italic"</em><br/>" |
| 1406 |
"<h5> &emspHeadings are dropped and" |
| 1407 |
" converted to bold</h5>" |
| 1408 |
) |
| 1409 |
|
| 1410 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.HTML) |
| 1411 |
|
| 1412 |
# 1 call to look up bot owner, and second for notification |
| 1413 |
assert mock_post.call_count == 2 |
| 1414 |
|
| 1415 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 1416 |
|
| 1417 |
# Test that everything is escaped properly in a HTML mode |
| 1418 |
assert ( |
| 1419 |
payload["text"] |
| 1420 |
== "<b>\r\n<b>'information'</b>\r\n</b>\r\n<i>\"This is in Italic\"" |
| 1421 |
"</i>\r\n<b> Headings are dropped and converted to bold</b>\r\n" |
| 1422 |
) |
| 1423 |
|
| 1424 |
mock_post.reset_mock() |
| 1425 |
|
| 1426 |
assert aobj.notify(title=title, body=body, body_format=NotifyFormat.TEXT) |
| 1427 |
|
| 1428 |
# owner has already been looked up, so only one call is made |
| 1429 |
assert mock_post.call_count == 1 |
| 1430 |
|
| 1431 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1432 |
|
| 1433 |
assert ( |
| 1434 |
payload["text"] |
| 1435 |
== "<b><title>&apos;information&apos</title></b>" |
| 1436 |
"\r\n<em>&quot;This is in Italic&quot</em><" |
| 1437 |
"br/><h5>&emsp;&emspHeadings&nbsp;are " |
| 1438 |
"dropped and&nbspconverted to bold</h5>" |
| 1439 |
) |
| 1440 |
|
| 1441 |
# Lest test more complex HTML examples now |
| 1442 |
mock_post.reset_mock() |
| 1443 |
|
| 1444 |
test_file_01 = os.path.join(TEST_VAR_DIR, "01_test_example.html") |
| 1445 |
with open(test_file_01) as html_file: |
| 1446 |
assert aobj.notify( |
| 1447 |
body=html_file.read(), body_format=NotifyFormat.HTML |
| 1448 |
) |
| 1449 |
|
| 1450 |
# owner has already been looked up, so only one call is made |
| 1451 |
assert mock_post.call_count == 1 |
| 1452 |
|
| 1453 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1454 |
assert ( |
| 1455 |
payload["text"] |
| 1456 |
== "\r\n<b>Bootstrap 101 Template</b>\r\n<b>My Title</b>\r\n" |
| 1457 |
"<b>Heading 1</b>\r\n-Bullet 1\r\n-Bullet 2\r\n-Bullet 3\r\n" |
| 1458 |
"-Bullet 1\r\n-Bullet 2\r\n-Bullet 3\r\n<b>Heading 2</b>\r\n" |
| 1459 |
"A div entry\r\nA div entry\r\n" |
| 1460 |
"<pre><code class=\"language-python\">print('hello')</code></pre>\r\n" |
| 1461 |
"<b>Heading 3</b>\r\n<b>Heading 4</b>\r\n<b>Heading 5</b>\r\n" |
| 1462 |
"<b>Heading 6</b>\r\nA set of text\r\n" |
| 1463 |
"Another line after the set of text\r\nMore text\r\nlabel" |
| 1464 |
) |
| 1465 |
|
| 1466 |
|
| 1467 |
@mock.patch("requests.post") |
| 1468 |
def test_plugin_telegram_html_heading_padding_needs_source( |
| 1469 |
mock_post, |
| 1470 |
): |
| 1471 |
"""HTML heading padding requires a declared source format.""" |
| 1472 |
|
| 1473 |
mock_post.return_value = requests.Request() |
| 1474 |
mock_post.return_value.status_code = requests.codes.ok |
| 1475 |
mock_post.return_value.content = "{}" |
| 1476 |
|
| 1477 |
body = "<h1>Heading</h1>Body text here" |
| 1478 |
|
| 1479 |
aobj = Apprise() |
| 1480 |
assert aobj.add("tgram://123456789:abcdefg_hijklmnop/12345678") |
| 1481 |
|
| 1482 |
# Declared HTML: heading gets padded on both sides. |
| 1483 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) |
| 1484 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 1485 |
assert payload["text"] == "\r\n<b>Heading</b>\r\nBody text here" |
| 1486 |
mock_post.reset_mock() |
| 1487 |
|
| 1488 |
# Undeclared input resolves to HTML but remains unpadded. |
| 1489 |
assert aobj.notify(body=body) |
| 1490 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 1491 |
assert payload["text"] == "<b>Heading</b>Body text here" |
| 1492 |
|
| 1493 |
|
| 1494 |
@mock.patch("requests.post") |
| 1495 |
def test_plugin_telegram_html_to_markdown_format(mock_post): |
| 1496 |
"""Test HTML delivery to Telegram Markdown targets.""" |
| 1497 |
|
| 1498 |
# Prepare Mock |
| 1499 |
mock_post.return_value = requests.Request() |
| 1500 |
mock_post.return_value.status_code = requests.codes.ok |
| 1501 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 1502 |
|
| 1503 |
# Simple HTML that our html_to_markdown converter handles |
| 1504 |
body = "<b>hello</b> <i>world</i>" |
| 1505 |
|
| 1506 |
# Markdown v1 gets the converted body in Telegram's own Markdown dialect |
| 1507 |
# (single-asterisk bold, single-underscore italic). |
| 1508 |
aobj = Apprise() |
| 1509 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=1") |
| 1510 |
assert len(aobj) == 1 |
| 1511 |
|
| 1512 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) |
| 1513 |
|
| 1514 |
assert mock_post.call_count == 1 |
| 1515 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1516 |
|
| 1517 |
assert payload["parse_mode"] == "MARKDOWN" |
| 1518 |
assert payload["text"] == "*hello* _world_" |
| 1519 |
|
| 1520 |
mock_post.reset_mock() |
| 1521 |
|
| 1522 |
# Markdown v2 gets the same dialect-correct delimiters, left unescaped so |
| 1523 |
# Telegram still parses them as real formatting. |
| 1524 |
aobj = Apprise() |
| 1525 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1526 |
assert len(aobj) == 1 |
| 1527 |
|
| 1528 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) |
| 1529 |
|
| 1530 |
assert mock_post.call_count == 1 |
| 1531 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1532 |
|
| 1533 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1534 |
assert payload["text"] == "*hello* _world_" |
| 1535 |
|
| 1536 |
mock_post.reset_mock() |
| 1537 |
|
| 1538 |
# Telegram escapes v2-only punctuation not covered by generic Markdown. |
| 1539 |
aobj = Apprise() |
| 1540 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1541 |
assert len(aobj) == 1 |
| 1542 |
|
| 1543 |
assert aobj.notify( |
| 1544 |
body="<p>3:00 p.m. (sharp)! Don't be late - see you there.</p>", |
| 1545 |
body_format=NotifyFormat.HTML, |
| 1546 |
) |
| 1547 |
|
| 1548 |
assert mock_post.call_count == 1 |
| 1549 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1550 |
|
| 1551 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1552 |
assert payload["text"] == ( |
| 1553 |
r"3:00 p\.m\. \(sharp\)\! Don't be late \- see you there\." |
| 1554 |
) |
| 1555 |
|
| 1556 |
mock_post.reset_mock() |
| 1557 |
|
| 1558 |
# Markdown v2 target, plain TEXT body |
| 1559 |
aobj = Apprise() |
| 1560 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1561 |
assert len(aobj) == 1 |
| 1562 |
|
| 1563 |
assert aobj.notify( |
| 1564 |
body="Tag #1 and *not* bold", body_format=NotifyFormat.TEXT |
| 1565 |
) |
| 1566 |
|
| 1567 |
assert mock_post.call_count == 1 |
| 1568 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1569 |
|
| 1570 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1571 |
assert payload["text"] == r"Tag \#1 and \*not\* bold" |
| 1572 |
|
| 1573 |
mock_post.reset_mock() |
| 1574 |
|
| 1575 |
# Markdown v2 target, no body_format specified |
| 1576 |
aobj = Apprise() |
| 1577 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1578 |
assert len(aobj) == 1 |
| 1579 |
|
| 1580 |
assert aobj.notify(body="**already** markdown #tag") |
| 1581 |
|
| 1582 |
assert mock_post.call_count == 1 |
| 1583 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1584 |
|
| 1585 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1586 |
assert payload["text"] == "**already** markdown #tag" |
| 1587 |
|
| 1588 |
mock_post.reset_mock() |
| 1589 |
|
| 1590 |
# A heading has no MarkdownV2 entity, so it renders as bold instead of |
| 1591 |
# a literal, escaped "#" -- either way Telegram never sees a bare '#' |
| 1592 |
# that would make it reject the whole message. |
| 1593 |
aobj = Apprise() |
| 1594 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1595 |
assert len(aobj) == 1 |
| 1596 |
|
| 1597 |
assert aobj.notify( |
| 1598 |
body="<h1>Title</h1><p>body text</p>", body_format=NotifyFormat.HTML |
| 1599 |
) |
| 1600 |
|
| 1601 |
assert mock_post.call_count == 1 |
| 1602 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1603 |
|
| 1604 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1605 |
assert payload["text"] == "*Title*\n\nbody text" |
| 1606 |
|
| 1607 |
mock_post.reset_mock() |
| 1608 |
|
| 1609 |
# Convert CommonMark links to Telegram's bare-destination syntax. |
| 1610 |
aobj = Apprise() |
| 1611 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1612 |
assert len(aobj) == 1 |
| 1613 |
|
| 1614 |
assert aobj.notify( |
| 1615 |
body='<a href="https://example.com/x">click</a>', |
| 1616 |
body_format=NotifyFormat.HTML, |
| 1617 |
) |
| 1618 |
|
| 1619 |
assert mock_post.call_count == 1 |
| 1620 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1621 |
|
| 1622 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1623 |
assert payload["text"] == "[click](https://example.com/x)" |
| 1624 |
|
| 1625 |
mock_post.reset_mock() |
| 1626 |
|
| 1627 |
# Lists and tables have no MarkdownV2 entity either -- their markers |
| 1628 |
# need the same escaping as a heading's, for the same reason. |
| 1629 |
aobj = Apprise() |
| 1630 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1631 |
assert len(aobj) == 1 |
| 1632 |
|
| 1633 |
assert aobj.notify( |
| 1634 |
body="<ul><li>one</li><li>two</li></ul>", |
| 1635 |
body_format=NotifyFormat.HTML, |
| 1636 |
) |
| 1637 |
|
| 1638 |
assert mock_post.call_count == 1 |
| 1639 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1640 |
|
| 1641 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1642 |
assert payload["text"] == "\\- one\n\\- two" |
| 1643 |
|
| 1644 |
mock_post.reset_mock() |
| 1645 |
|
| 1646 |
aobj = Apprise() |
| 1647 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1648 |
assert len(aobj) == 1 |
| 1649 |
|
| 1650 |
assert aobj.notify( |
| 1651 |
body="<table><tr><td>A</td><td>B</td></tr></table>", |
| 1652 |
body_format=NotifyFormat.HTML, |
| 1653 |
) |
| 1654 |
|
| 1655 |
assert mock_post.call_count == 1 |
| 1656 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1657 |
|
| 1658 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1659 |
assert payload["text"] == ( |
| 1660 |
"\\| A \\| B \\|\n\\| \\-\\-\\- \\| \\-\\-\\- \\|" |
| 1661 |
) |
| 1662 |
|
| 1663 |
mock_post.reset_mock() |
| 1664 |
|
| 1665 |
# A code span's content is just as literal to Telegram as it is to |
| 1666 |
# CommonMark -- the strict escape pass must not touch it. |
| 1667 |
aobj = Apprise() |
| 1668 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=2") |
| 1669 |
assert len(aobj) == 1 |
| 1670 |
|
| 1671 |
assert aobj.notify( |
| 1672 |
body="<code>a.b-c|d</code>", body_format=NotifyFormat.HTML |
| 1673 |
) |
| 1674 |
|
| 1675 |
assert mock_post.call_count == 1 |
| 1676 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 1677 |
|
| 1678 |
assert payload["parse_mode"] == "MarkdownV2" |
| 1679 |
assert payload["text"] == "`a.b-c|d`" |
| 1680 |
|
| 1681 |
|
| 1682 |
@mock.patch("requests.post") |
| 1683 |
def test_plugin_telegram_html_to_markdown_hardening(mock_post): |
| 1684 |
"""Test edge cases in the CommonMark-to-Telegram dialect adaptation.""" |
| 1685 |
|
| 1686 |
# Prepare Mock |
| 1687 |
mock_post.return_value = requests.Request() |
| 1688 |
mock_post.return_value.status_code = requests.codes.ok |
| 1689 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 1690 |
|
| 1691 |
def notify(body, mdv="2"): |
| 1692 |
aobj = Apprise() |
| 1693 |
aobj.add( |
| 1694 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 1695 |
f"?format=markdown&mdv={mdv}" |
| 1696 |
) |
| 1697 |
assert len(aobj) == 1 |
| 1698 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) |
| 1699 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 1700 |
mock_post.reset_mock() |
| 1701 |
return payload["text"] |
| 1702 |
|
| 1703 |
# A link destination containing a literal ')' or '\' must have those |
| 1704 |
# escaped. |
| 1705 |
assert notify('<a href="https://example.com/a(b)c">x</a>') == ( |
| 1706 |
"[x](https://example.com/a(b\\)c)" |
| 1707 |
) |
| 1708 |
|
| 1709 |
# A code span's content needs every '`'/'\' inside it escaped. |
| 1710 |
assert notify("<code>a\\b</code>") == "`a\\\\b`" |
| 1711 |
assert notify("<code>a`b</code>") == "`a\\`b`" |
| 1712 |
|
| 1713 |
# Either adjacent nesting order flattens to italic around bold. |
| 1714 |
assert notify("<b><i>x</i></b>") == "_*x*_" |
| 1715 |
assert notify("<i><b>x</b></i>") == "_*x*_" |
| 1716 |
|
| 1717 |
# Legacy Markdown (v1) doesn't support nested entities at all. |
| 1718 |
assert notify("<b>a <i>b</i> c</b>", mdv="1") == "*a b c*" |
| 1719 |
|
| 1720 |
# Telegram v1 keeps only the outer span from nested CommonMark. |
| 1721 |
assert notify("<b><i>x</i></b>", mdv="1") == "_x_" |
| 1722 |
assert notify("<i><b>x</b></i>", mdv="1") == "_x_" |
| 1723 |
|
| 1724 |
# Legacy Markdown only recognizes a backslash escape in front of |
| 1725 |
# '`'/'*'/'_'/'['. |
| 1726 |
assert ( |
| 1727 |
notify("<p>#tag (test)! <x> ~wave~</p>", mdv="1") |
| 1728 |
== "#tag (test)! <x> ~wave~" |
| 1729 |
) |
| 1730 |
assert ( |
| 1731 |
notify("<p>a[b]c *lit* _lit_ `lit`</p>", mdv="1") |
| 1732 |
== "a\\[b\\]c \\*lit\\* \\_lit\\_ \\`lit\\`" |
| 1733 |
) |
| 1734 |
|
| 1735 |
# Non-adjacent nesting and sibling spans are unaffected. |
| 1736 |
assert notify("<b>bold <i>italic</i> still bold</b>") == ( |
| 1737 |
"*bold _italic_ still bold*" |
| 1738 |
) |
| 1739 |
# Adjacent bold tags retain and escape ambiguous middle markers. |
| 1740 |
assert notify("<b>A</b><b>B</b>") == "*A\\*\\*\\*\\*B*" |
| 1741 |
|
| 1742 |
# A nested bold opening *while italic is already open, with real text in |
| 1743 |
# between* (so the two opening delimiters aren't touching) is a completely. |
| 1744 |
assert notify("<i>a <b>b</b> c</i>") == "_a *b* c_" |
| 1745 |
assert notify("<i>a <b>b</b> c</i>", mdv="1") == "_a b c_" |
| 1746 |
|
| 1747 |
# The reverse nesting (bold containing italic, separated by text) was |
| 1748 |
# already correct, and must stay that way. |
| 1749 |
assert notify("<b>a <i>b</i> c</b>") == "*a _b_ c*" |
| 1750 |
|
| 1751 |
# A literal "\x01<digits>\x01"-shaped sequence in ordinary text must pass |
| 1752 |
# through completely unaltered. |
| 1753 |
assert notify("literal \x010\x01 text, no code or links at all") == ( |
| 1754 |
"literal \x010\x01 text, no code or links at all" |
| 1755 |
) |
| 1756 |
|
| 1757 |
# overflow=split can hand this method just one chunk of a longer body, with |
| 1758 |
# a span that doesn't open or close until a different chunk entirely. |
| 1759 |
aobj = Apprise() |
| 1760 |
aobj.add( |
| 1761 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 1762 |
"?format=markdown&mdv=2&overflow=split" |
| 1763 |
) |
| 1764 |
assert len(aobj) == 1 |
| 1765 |
assert aobj.notify( |
| 1766 |
body="<b>" + ("x" * 4990) + "</b>", body_format=NotifyFormat.HTML |
| 1767 |
) |
| 1768 |
assert mock_post.call_count == 2 |
| 1769 |
texts = [loads(c[1]["data"])["text"] for c in mock_post.call_args_list] |
| 1770 |
|
| 1771 |
# Each half is independently balanced -- an odd number of un-escaped |
| 1772 |
# '*'/'_' in either one would mean Telegram still rejects it. |
| 1773 |
for text in texts: |
| 1774 |
assert text.count("*") % 2 == 0 |
| 1775 |
assert text.count("_") % 2 == 0 |
| 1776 |
|
| 1777 |
# A split at a bold close must not leave an empty entity. |
| 1778 |
assert texts[1] == "x" * (len(texts[1])) |
| 1779 |
|
| 1780 |
# The same overflow split can also land mid-code-span or mid-link. |
| 1781 |
assert ( |
| 1782 |
NotifyTelegram._commonmark_to_telegram( |
| 1783 |
"text ``unterminated", strict=True |
| 1784 |
) |
| 1785 |
== "text \\`\\`unterminated" |
| 1786 |
) |
| 1787 |
# "](<" with no preceding "[" (so not a real link) and no closing ">)" |
| 1788 |
# either: every reserved bracket/paren is escaped as stray punctuation. |
| 1789 |
assert ( |
| 1790 |
NotifyTelegram._commonmark_to_telegram( |
| 1791 |
"a](<https://incomplete no close", strict=True |
| 1792 |
) |
| 1793 |
== "a\\]\\(\\<https://incomplete no close" |
| 1794 |
) |
| 1795 |
|
| 1796 |
# Always escape stray MarkdownV2 brackets and parentheses. |
| 1797 |
assert ( |
| 1798 |
NotifyTelegram._commonmark_to_telegram( |
| 1799 |
"literal (value) and [bracket] text", strict=True |
| 1800 |
) |
| 1801 |
== "literal \\(value\\) and \\[bracket\\] text" |
| 1802 |
) |
| 1803 |
|
| 1804 |
# Prevent an orphaned "[" from matching a later unrelated link. |
| 1805 |
assert ( |
| 1806 |
NotifyTelegram._commonmark_to_telegram("[a] b (c) ](d)", strict=True) |
| 1807 |
== "\\[a\\] b \\(c\\) \\]\\(d\\)" |
| 1808 |
) |
| 1809 |
|
| 1810 |
# An invalid inner angle link must not reuse or consume its outer label. |
| 1811 |
# Telegram returns the unresolved structure as escaped literal text. |
| 1812 |
body = "[OUTER [INNER](<badstuff)](https://example.com)" |
| 1813 |
assert NotifyTelegram._commonmark_to_telegram(body, strict=True) == ( |
| 1814 |
"\\[OUTER \\[INNER\\]\\(\\<badstuff\\)\\]\\(https://example\\.com\\)" |
| 1815 |
) |
| 1816 |
|
| 1817 |
# A "]" with no "(" must retire its "[" in both Markdown modes, |
| 1818 |
# instead of leaving it pending for a later, unrelated link to reuse. |
| 1819 |
stray_body = "[label] and ](http://x.com/a(b)c) end" |
| 1820 |
assert ( |
| 1821 |
NotifyTelegram._commonmark_to_telegram(stray_body, strict=False) |
| 1822 |
== "[label] and ](http://x.com/a(b)c) end" |
| 1823 |
) |
| 1824 |
assert NotifyTelegram._commonmark_to_telegram(stray_body, strict=True) == ( |
| 1825 |
"\\[label\\] and \\]\\(http://x\\.com/a\\(b\\)c\\) end" |
| 1826 |
) |
| 1827 |
|
| 1828 |
# An invalid destination becomes fully escaped literal text in v2. |
| 1829 |
assert ( |
| 1830 |
NotifyTelegram._commonmark_to_telegram( |
| 1831 |
"[label](has space)", strict=True |
| 1832 |
) |
| 1833 |
== "\\[label\\]\\(has space\\)" |
| 1834 |
) |
| 1835 |
|
| 1836 |
# An unfinished destination follows the same literal-text path. |
| 1837 |
assert ( |
| 1838 |
NotifyTelegram._commonmark_to_telegram( |
| 1839 |
"[label](unterminated", strict=True |
| 1840 |
) |
| 1841 |
== "\\[label\\]\\(unterminated" |
| 1842 |
) |
| 1843 |
|
| 1844 |
# Telegram v1 leaves the rejected destination literal. |
| 1845 |
assert ( |
| 1846 |
NotifyTelegram._commonmark_to_telegram( |
| 1847 |
"[label](has space)", strict=False |
| 1848 |
) |
| 1849 |
== "[label](has space)" |
| 1850 |
) |
| 1851 |
|
| 1852 |
# An unfinished angle destination follows the same v2 fallback. |
| 1853 |
assert ( |
| 1854 |
NotifyTelegram._commonmark_to_telegram( |
| 1855 |
"[label](<https://unterminated", strict=True |
| 1856 |
) |
| 1857 |
== "\\[label\\]\\(\\<https://unterminated" |
| 1858 |
) |
| 1859 |
|
| 1860 |
# Telegram v1 also leaves the unfinished angle destination literal. |
| 1861 |
assert ( |
| 1862 |
NotifyTelegram._commonmark_to_telegram( |
| 1863 |
"[label](<https://unterminated", strict=False |
| 1864 |
) |
| 1865 |
== "[label](<https://unterminated" |
| 1866 |
) |
| 1867 |
|
| 1868 |
# Escape a dangling "[" during end-of-scan cleanup. |
| 1869 |
assert ( |
| 1870 |
NotifyTelegram._commonmark_to_telegram( |
| 1871 |
"text [dangling forever", strict=True |
| 1872 |
) |
| 1873 |
== "text \\[dangling forever" |
| 1874 |
) |
| 1875 |
|
| 1876 |
# Preserve plain Markdown links; only "(" and ")" need escaping inside |
| 1877 |
# a destination, not the full MarkdownV2 reserved-character set. |
| 1878 |
assert ( |
| 1879 |
NotifyTelegram._commonmark_to_telegram( |
| 1880 |
"[a link](https://example.com/x.y)", strict=True |
| 1881 |
) |
| 1882 |
== "[a link](https://example.com/x.y)" |
| 1883 |
) |
| 1884 |
|
| 1885 |
# Preserve an escaped parenthesis inside a plain link destination. |
| 1886 |
assert ( |
| 1887 |
NotifyTelegram._commonmark_to_telegram( |
| 1888 |
"[label](http://example.com/a\\)b)", strict=True |
| 1889 |
) |
| 1890 |
== "[label](http://example.com/a\\)b)" |
| 1891 |
) |
| 1892 |
|
| 1893 |
# Preserve balanced parentheses inside a plain link destination. |
| 1894 |
assert ( |
| 1895 |
NotifyTelegram._commonmark_to_telegram( |
| 1896 |
"[label](https://example.com/a_(b))", strict=True |
| 1897 |
) |
| 1898 |
== "[label](https://example.com/a_\\(b\\))" |
| 1899 |
) |
| 1900 |
|
| 1901 |
# Apply the same parenthesis balancing in Telegram V1. |
| 1902 |
assert ( |
| 1903 |
NotifyTelegram._commonmark_to_telegram( |
| 1904 |
"[label](https://example.com/a_(b))", strict=False |
| 1905 |
) |
| 1906 |
== "[label](https://example.com/a_\\(b\\))" |
| 1907 |
) |
| 1908 |
|
| 1909 |
# Preserve an opener with no closer. |
| 1910 |
assert NotifyTelegram._commonmark_to_telegram("****x") == "****x" |
| 1911 |
|
| 1912 |
# Preserve a run that is neither left- nor right-flanking. |
| 1913 |
assert NotifyTelegram._commonmark_to_telegram("******") == "******" |
| 1914 |
|
| 1915 |
# Preserve unmatched markers in a complete body. |
| 1916 |
f1 = NotifyTelegram._commonmark_to_telegram |
| 1917 |
assert f1("***italic text") == "***italic text" |
| 1918 |
assert f1("**text") == "**text" |
| 1919 |
assert f1("**") == "**" |
| 1920 |
|
| 1921 |
# User-provided Private Use text must not collide in either Markdown mode. |
| 1922 |
marker = chr(0xE000) |
| 1923 |
attack = f"before {marker}0{marker} after" |
| 1924 |
assert f1(attack, strict=False) == attack |
| 1925 |
assert f1(attack, strict=True) == attack |
| 1926 |
assert f1(f"*italic* {attack}", strict=False) == f"_italic_ {attack}" |
| 1927 |
|
| 1928 |
# Strict MarkdownV2 escapes literal emphasis markers. |
| 1929 |
assert ( |
| 1930 |
NotifyTelegram._commonmark_to_telegram("a" * 9 + "_", strict=True) |
| 1931 |
== "a" * 9 + "\\_" |
| 1932 |
) |
| 1933 |
assert ( |
| 1934 |
NotifyTelegram._commonmark_to_telegram("a" * 9 + "*", strict=True) |
| 1935 |
== "a" * 9 + "\\*" |
| 1936 |
) |
| 1937 |
# Legacy v1 does not enforce universal escaping. |
| 1938 |
assert ( |
| 1939 |
NotifyTelegram._commonmark_to_telegram("a" * 9 + "_", strict=False) |
| 1940 |
== "a" * 9 + "_" |
| 1941 |
) |
| 1942 |
|
| 1943 |
# Preserve genuine underscore-based italics. |
| 1944 |
assert ( |
| 1945 |
NotifyTelegram._commonmark_to_telegram("_italic_", strict=True) |
| 1946 |
== "_italic_" |
| 1947 |
) |
| 1948 |
assert ( |
| 1949 |
NotifyTelegram._commonmark_to_telegram( |
| 1950 |
"**bold** _italic_", strict=True |
| 1951 |
) |
| 1952 |
== "*bold* _italic_" |
| 1953 |
) |
| 1954 |
# Escape intraword underscores instead of treating them as emphasis. |
| 1955 |
assert ( |
| 1956 |
NotifyTelegram._commonmark_to_telegram("foo_bar_baz", strict=True) |
| 1957 |
== "foo\\_bar\\_baz" |
| 1958 |
) |
| 1959 |
# Keep intraword double underscores literal, not underlined. |
| 1960 |
assert ( |
| 1961 |
NotifyTelegram._commonmark_to_telegram("a__b__c", strict=True) |
| 1962 |
== "a\\_\\_b\\_\\_c" |
| 1963 |
) |
| 1964 |
# Render valid double-underscore CommonMark as Telegram bold. |
| 1965 |
assert ( |
| 1966 |
NotifyTelegram._commonmark_to_telegram("__bold__", strict=True) |
| 1967 |
== "*bold*" |
| 1968 |
) |
| 1969 |
assert ( |
| 1970 |
NotifyTelegram._commonmark_to_telegram( |
| 1971 |
"before __bold__ after", strict=True |
| 1972 |
) |
| 1973 |
== "before *bold* after" |
| 1974 |
) |
| 1975 |
|
| 1976 |
# Keep adjacent asterisk and underscore families independent. |
| 1977 |
assert NotifyTelegram._commonmark_to_telegram("*_", strict=True) == ( |
| 1978 |
"\\*\\_" |
| 1979 |
) |
| 1980 |
|
| 1981 |
# Preserve unrelated unmatched delimiter families. |
| 1982 |
assert ( |
| 1983 |
NotifyTelegram._commonmark_to_telegram("**_a", strict=False) == "**_a" |
| 1984 |
) |
| 1985 |
|
| 1986 |
# Do not close asterisk emphasis with an underscore. |
| 1987 |
assert ( |
| 1988 |
NotifyTelegram._commonmark_to_telegram("***_", strict=True) |
| 1989 |
== "\\*\\*\\*\\_" |
| 1990 |
) |
| 1991 |
|
| 1992 |
# Match underscores across an unrelated literal asterisk run. |
| 1993 |
assert ( |
| 1994 |
NotifyTelegram._commonmark_to_telegram("_***_", strict=True) |
| 1995 |
== "_\\*\\*\\*_" |
| 1996 |
) |
| 1997 |
|
| 1998 |
# Leave unmatched width literal beside a valid italic span. |
| 1999 |
assert ( |
| 2000 |
NotifyTelegram._commonmark_to_telegram("__a_", strict=True) == "\\__a_" |
| 2001 |
) |
| 2002 |
|
| 2003 |
# Preserve an unmatched underscore opener. |
| 2004 |
assert ( |
| 2005 |
NotifyTelegram._commonmark_to_telegram("____a", strict=True) |
| 2006 |
== "\\_\\_\\_\\_a" |
| 2007 |
) |
| 2008 |
|
| 2009 |
# Preserve leftover width after closing bold. |
| 2010 |
assert ( |
| 2011 |
NotifyTelegram._commonmark_to_telegram("__a___", strict=True) |
| 2012 |
== "*a*\\_" |
| 2013 |
) |
| 2014 |
|
| 2015 |
# Preserve unmatched mixed delimiter families. |
| 2016 |
assert ( |
| 2017 |
NotifyTelegram._commonmark_to_telegram("*__a", strict=False) == "*__a" |
| 2018 |
) |
| 2019 |
|
| 2020 |
# A run wide enough to supply both kinds of emphasis nests regular |
| 2021 |
# emphasis outermost and bold innermost, the same as for asterisks. |
| 2022 |
assert ( |
| 2023 |
NotifyTelegram._commonmark_to_telegram("___a___", strict=True) |
| 2024 |
== "_*a*_" |
| 2025 |
) |
| 2026 |
|
| 2027 |
# Opening requires content, so empty trailing bold is unreachable. |
| 2028 |
|
| 2029 |
# A link destination containing a backslash-escaped '>' in V1 mode: |
| 2030 |
# the scan skips escaped characters and still finds the '>)' terminator. |
| 2031 |
assert ( |
| 2032 |
notify('<a href="https://example.com/x>y">click</a>', mdv="1") |
| 2033 |
== r"[click](https://example.com/x\\>y)" |
| 2034 |
) |
| 2035 |
|
| 2036 |
# Telegram v1 renders the merged title heading as bold. |
| 2037 |
aobj_v1 = Apprise() |
| 2038 |
aobj_v1.add( |
| 2039 |
"tgram://123456789:abcdefg_hijklmnop/12345?format=markdown&mdv=1" |
| 2040 |
) |
| 2041 |
assert aobj_v1.notify( |
| 2042 |
body="<b>hello</b>", title="My Title", body_format=NotifyFormat.HTML |
| 2043 |
) |
| 2044 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 2045 |
assert payload["text"] == "*My Title*\n*hello*" |
| 2046 |
mock_post.reset_mock() |
| 2047 |
|
| 2048 |
# Title that reduces to an empty string after stripping leading heading and |
| 2049 |
# list characters (html_to_markdown converts " - " to "-"). |
| 2050 |
assert aobj_v1.notify( |
| 2051 |
body="<b>hello</b>", title=" - ", body_format=NotifyFormat.HTML |
| 2052 |
) |
| 2053 |
payload = loads(mock_post.call_args_list[-1][1]["data"]) |
| 2054 |
assert payload["text"] == "*hello*" |
| 2055 |
mock_post.reset_mock() |
| 2056 |
|
| 2057 |
|
| 2058 |
@mock.patch("requests.post") |
| 2059 |
def test_plugin_telegram_overflow_split_repair(mock_post): |
| 2060 |
"""Test generic split repair before Telegram dialect conversion.""" |
| 2061 |
|
| 2062 |
# Prepare Mock |
| 2063 |
mock_post.return_value = requests.Request() |
| 2064 |
mock_post.return_value.status_code = requests.codes.ok |
| 2065 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 2066 |
|
| 2067 |
def notify_split(body): |
| 2068 |
aobj = Apprise() |
| 2069 |
aobj.add( |
| 2070 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 2071 |
"?format=markdown&mdv=2&overflow=split" |
| 2072 |
) |
| 2073 |
assert len(aobj) == 1 |
| 2074 |
assert aobj.notify(body=body, body_format=NotifyFormat.HTML) |
| 2075 |
texts = [loads(c[1]["data"])["text"] for c in mock_post.call_args_list] |
| 2076 |
mock_post.reset_mock() |
| 2077 |
return texts |
| 2078 |
|
| 2079 |
# A bold span long enough to force a split, immediately followed by plain |
| 2080 |
# text that was never part of it. |
| 2081 |
texts = notify_split( |
| 2082 |
"<b>" + ("x" * 4990) + "</b>" + "TAIL_SHOULD_NOT_BE_BOLD" |
| 2083 |
) |
| 2084 |
assert len(texts) == 2 |
| 2085 |
# The part that fit keeps its formatting... |
| 2086 |
assert texts[0].startswith("*x") |
| 2087 |
assert texts[0].endswith("x*") |
| 2088 |
# ...but the unrelated trailing text does not become bold. |
| 2089 |
assert "TAIL" in texts[1] |
| 2090 |
assert not texts[1].startswith("*") |
| 2091 |
for text in texts: |
| 2092 |
assert text.count("*") % 2 == 0 |
| 2093 |
assert text.count("_") % 2 == 0 |
| 2094 |
|
| 2095 |
# A link long enough that its URL alone forces a split. |
| 2096 |
url = "https://example.com/" + ("a" * 4990) |
| 2097 |
texts = notify_split(f'<a href="{url}">click here</a>') |
| 2098 |
assert len(texts) >= 2 |
| 2099 |
for text in texts: |
| 2100 |
# Every chunk must be valid MarkdownV2: no unescaped reserved chars. |
| 2101 |
assert not re.search(r"(?<!\\)[_*\[\]()~`>#+=|{}.!<-]", text) |
| 2102 |
|
| 2103 |
# A <pre> block long enough to force a split. |
| 2104 |
content = "line.with.dots-and-dashes_under " * 200 |
| 2105 |
texts = notify_split(f"<pre>{content}</pre>") |
| 2106 |
assert len(texts) >= 2 |
| 2107 |
for text in texts: |
| 2108 |
assert not re.search(r"(?<!\\)[_*\[\]()~`>#+=|{}.!<-]", text) |
| 2109 |
|
| 2110 |
# A short message that never triggers a split at all is unaffected. |
| 2111 |
texts = notify_split("<b>short</b> <i>text</i>") |
| 2112 |
assert texts == ["*short* _text_"] |
| 2113 |
|
| 2114 |
# Conversion tests cover the repair primitive used indirectly here. |
| 2115 |
|
| 2116 |
|
| 2117 |
@mock.patch("requests.post") |
| 2118 |
def test_plugin_telegram_declared_markdown_split_repair(mock_post): |
| 2119 |
"""Declared Markdown uses the same split repair as converted HTML.""" |
| 2120 |
|
| 2121 |
mock_post.return_value = requests.Request() |
| 2122 |
mock_post.return_value.status_code = requests.codes.ok |
| 2123 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 2124 |
|
| 2125 |
def notify_split(body, body_format): |
| 2126 |
aobj = Apprise() |
| 2127 |
aobj.add( |
| 2128 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 2129 |
"?format=markdown&mdv=2&overflow=split" |
| 2130 |
) |
| 2131 |
assert len(aobj) == 1 |
| 2132 |
assert aobj.notify(body=body, body_format=body_format) |
| 2133 |
texts = [loads(c[1]["data"])["text"] for c in mock_post.call_args_list] |
| 2134 |
mock_post.reset_mock() |
| 2135 |
return texts |
| 2136 |
|
| 2137 |
# Force a split after a long bold span. |
| 2138 |
body = "**" + ("x" * 4990) + "**" + "TAIL SHOULD NOT BE BOLD" |
| 2139 |
texts_html = notify_split( |
| 2140 |
"<b>" + ("x" * 4990) + "</b>" + "TAIL SHOULD NOT BE BOLD", |
| 2141 |
NotifyFormat.HTML, |
| 2142 |
) |
| 2143 |
texts_md = notify_split(body, NotifyFormat.MARKDOWN) |
| 2144 |
|
| 2145 |
# Declared Markdown matches HTML-derived Markdown chunk repair. |
| 2146 |
assert texts_md == texts_html |
| 2147 |
|
| 2148 |
# Short declared Markdown still gets dialect completion. |
| 2149 |
texts = notify_split("**short** _text_", NotifyFormat.MARKDOWN) |
| 2150 |
assert texts == ["*short* _text_"] |
| 2151 |
|
| 2152 |
# Undeclared input skips dialect completion and repair. |
| 2153 |
texts = notify_split("**short** _text_", None) |
| 2154 |
assert texts == ["**short** _text_"] |
| 2155 |
|
| 2156 |
|
| 2157 |
@mock.patch("requests.post") |
| 2158 |
def test_plugin_telegram_dialect_overflow(mock_post): |
| 2159 |
"""Apply the selected overflow mode after MarkdownV2 escaping |
| 2160 |
grows text.""" |
| 2161 |
|
| 2162 |
mock_post.return_value = requests.Request() |
| 2163 |
mock_post.return_value.status_code = requests.codes.ok |
| 2164 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 2165 |
|
| 2166 |
def notify(overflow): |
| 2167 |
aobj = Apprise() |
| 2168 |
aobj.add( |
| 2169 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 2170 |
f"?format=markdown&mdv=2&overflow={overflow}" |
| 2171 |
) |
| 2172 |
assert len(aobj) == 1 |
| 2173 |
# Escaping periods makes this body exceed the converted limit. |
| 2174 |
assert aobj.notify(body="." * 5000, body_format=NotifyFormat.MARKDOWN) |
| 2175 |
texts = [loads(c[1]["data"])["text"] for c in mock_post.call_args_list] |
| 2176 |
mock_post.reset_mock() |
| 2177 |
return texts |
| 2178 |
|
| 2179 |
# UPSTREAM: exactly one message, sent oversized rather than split. |
| 2180 |
texts = notify("upstream") |
| 2181 |
assert len(texts) == 1 |
| 2182 |
assert len(texts[0]) > NotifyTelegram.body_maxlen |
| 2183 |
|
| 2184 |
# TRUNCATE: exactly one message, clipped to fit. |
| 2185 |
texts = notify("truncate") |
| 2186 |
assert len(texts) == 1 |
| 2187 |
assert len(texts[0]) <= NotifyTelegram.body_maxlen |
| 2188 |
|
| 2189 |
# SPLIT: as many messages as needed, each one within the limit. |
| 2190 |
texts = notify("split") |
| 2191 |
assert len(texts) > 1 |
| 2192 |
for text in texts: |
| 2193 |
assert len(text) <= NotifyTelegram.body_maxlen |
| 2194 |
|
| 2195 |
|
| 2196 |
@mock.patch("requests.post") |
| 2197 |
def test_plugin_telegram_dialect_attachment_order(mock_post): |
| 2198 |
"""Place one attachment before or after dialect-split text pieces.""" |
| 2199 |
from apprise.attachment.memory import AttachMemory |
| 2200 |
|
| 2201 |
response = mock.Mock() |
| 2202 |
response.status_code = requests.codes.ok |
| 2203 |
response.content = dumps({"ok": True, "result": True}) |
| 2204 |
mock_post.return_value = response |
| 2205 |
|
| 2206 |
mem = AttachMemory( |
| 2207 |
content=b"hello world", name="test.txt", mimetype="text/plain" |
| 2208 |
) |
| 2209 |
|
| 2210 |
def notify(content_mode): |
| 2211 |
aobj = Apprise() |
| 2212 |
aobj.add( |
| 2213 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 2214 |
f"?format=markdown&mdv=2&overflow=split&content={content_mode}" |
| 2215 |
) |
| 2216 |
assert len(aobj) == 1 |
| 2217 |
# Escaping doubles this initially valid body and forces dialect |
| 2218 |
# splitting. |
| 2219 |
assert aobj.notify( |
| 2220 |
body="." * 3000, attach=mem, body_format=NotifyFormat.MARKDOWN |
| 2221 |
) |
| 2222 |
# Classify each call as a text message or an attachment upload. |
| 2223 |
kinds = [ |
| 2224 |
"text" if "sendMessage" in c[0][0] else "attach" |
| 2225 |
for c in mock_post.call_args_list |
| 2226 |
] |
| 2227 |
mock_post.reset_mock() |
| 2228 |
return kinds |
| 2229 |
|
| 2230 |
# content=before: every text piece goes out, then the attachment. |
| 2231 |
kinds = notify("before") |
| 2232 |
assert kinds.count("text") > 1 |
| 2233 |
assert kinds.count("attach") == 1 |
| 2234 |
assert kinds == ["text"] * kinds.count("text") + ["attach"] |
| 2235 |
|
| 2236 |
# content=after: the attachment goes out first, then every piece. |
| 2237 |
kinds = notify("after") |
| 2238 |
assert kinds.count("text") > 1 |
| 2239 |
assert kinds.count("attach") == 1 |
| 2240 |
assert kinds == ["attach"] + ["text"] * (len(kinds) - 1) |
| 2241 |
|
| 2242 |
|
| 2243 |
@mock.patch("requests.post") |
| 2244 |
def test_plugin_telegram_overflow_no_invented_emphasis(mock_post): |
| 2245 |
"""Do not invent emphasis for an unmatched literal delimiter.""" |
| 2246 |
|
| 2247 |
mock_post.return_value = requests.Request() |
| 2248 |
mock_post.return_value.status_code = requests.codes.ok |
| 2249 |
mock_post.return_value.content = dumps({"ok": True, "result": True}) |
| 2250 |
|
| 2251 |
def notify(overflow): |
| 2252 |
aobj = Apprise() |
| 2253 |
aobj.add( |
| 2254 |
"tgram://123456789:abcdefg_hijklmnop/12345" |
| 2255 |
f"?format=markdown&mdv=2&overflow={overflow}" |
| 2256 |
) |
| 2257 |
assert len(aobj) == 1 |
| 2258 |
# Keep spacing after valid emphasis so its closer is not intraword. |
| 2259 |
body = "*_a_ test " + ("x" * 5000) |
| 2260 |
assert aobj.notify(body=body, body_format=NotifyFormat.MARKDOWN) |
| 2261 |
texts = [loads(c[1]["data"])["text"] for c in mock_post.call_args_list] |
| 2262 |
mock_post.reset_mock() |
| 2263 |
return texts |
| 2264 |
|
| 2265 |
# SPLIT preserves both the escaped literal and valid emphasis. |
| 2266 |
texts = notify("split") |
| 2267 |
assert texts[0].startswith("\\*_a_") |
| 2268 |
|
| 2269 |
# TRUNCATE preserves the same prefix in one message. |
| 2270 |
texts = notify("truncate") |
| 2271 |
assert len(texts) == 1 |
| 2272 |
assert texts[0].startswith("\\*_a_") |
| 2273 |
|
| 2274 |
|
| 2275 |
@mock.patch("requests.post") |
| 2276 |
def test_plugin_telegram_threads(mock_post): |
| 2277 |
"""NotifyTelegram() Threads/Topics.""" |
| 2278 |
# Prepare Mock |
| 2279 |
mock_post.return_value = requests.Request() |
| 2280 |
mock_post.return_value.status_code = requests.codes.ok |
| 2281 |
|
| 2282 |
# Simple success response |
| 2283 |
mock_post.return_value.content = dumps( |
| 2284 |
{ |
| 2285 |
"ok": True, |
| 2286 |
"result": [ |
| 2287 |
{ |
| 2288 |
"update_id": 645421321, |
| 2289 |
"message": { |
| 2290 |
"message_id": 2, |
| 2291 |
"from": { |
| 2292 |
"id": 532389719, |
| 2293 |
"is_bot": False, |
| 2294 |
"first_name": "Chris", |
| 2295 |
"language_code": "en-US", |
| 2296 |
}, |
| 2297 |
"chat": { |
| 2298 |
"id": 532389719, |
| 2299 |
"first_name": "Chris", |
| 2300 |
"type": "private", |
| 2301 |
}, |
| 2302 |
"date": 1519694394, |
| 2303 |
"text": "/start", |
| 2304 |
"entities": [ |
| 2305 |
{ |
| 2306 |
"offset": 0, |
| 2307 |
"length": 6, |
| 2308 |
"type": "bot_command", |
| 2309 |
} |
| 2310 |
], |
| 2311 |
}, |
| 2312 |
}, |
| 2313 |
], |
| 2314 |
} |
| 2315 |
) |
| 2316 |
|
| 2317 |
aobj = Apprise() |
| 2318 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?thread=1234") |
| 2319 |
|
| 2320 |
assert len(aobj) == 1 |
| 2321 |
|
| 2322 |
assert isinstance(aobj[0], NotifyTelegram) |
| 2323 |
|
| 2324 |
body = "my threaded message" |
| 2325 |
|
| 2326 |
assert aobj.notify(body=body) |
| 2327 |
|
| 2328 |
# 1 call to look up bot owner, and second for notification |
| 2329 |
assert mock_post.call_count == 2 |
| 2330 |
|
| 2331 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 2332 |
|
| 2333 |
assert "message_thread_id" in payload |
| 2334 |
assert payload["message_thread_id"] == 1234 |
| 2335 |
|
| 2336 |
mock_post.reset_mock() |
| 2337 |
|
| 2338 |
aobj = Apprise() |
| 2339 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?topic=1234") |
| 2340 |
|
| 2341 |
assert len(aobj) == 1 |
| 2342 |
|
| 2343 |
assert isinstance(aobj[0], NotifyTelegram) |
| 2344 |
|
| 2345 |
body = "my message" |
| 2346 |
|
| 2347 |
assert aobj.notify(body=body) |
| 2348 |
|
| 2349 |
# 1 call to look up bot owner, and second for notification |
| 2350 |
assert mock_post.call_count == 2 |
| 2351 |
|
| 2352 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 2353 |
|
| 2354 |
assert "message_thread_id" in payload |
| 2355 |
assert payload["message_thread_id"] == 1234 |
| 2356 |
|
| 2357 |
mock_post.reset_mock() |
| 2358 |
|
| 2359 |
aobj = Apprise() |
| 2360 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/9876:1234/9876:1111") |
| 2361 |
|
| 2362 |
assert len(aobj) == 1 |
| 2363 |
|
| 2364 |
assert isinstance(aobj[0], NotifyTelegram) |
| 2365 |
|
| 2366 |
body = "my message" |
| 2367 |
|
| 2368 |
assert aobj.notify(body=body) |
| 2369 |
|
| 2370 |
# 1 call to look up bot owner, and second for notification |
| 2371 |
assert mock_post.call_count == 2 |
| 2372 |
|
| 2373 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 2374 |
|
| 2375 |
assert "message_thread_id" in payload |
| 2376 |
assert payload["message_thread_id"] == 1111 |
| 2377 |
|
| 2378 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 2379 |
|
| 2380 |
assert "message_thread_id" in payload |
| 2381 |
assert payload["message_thread_id"] == 1234 |
| 2382 |
|
| 2383 |
mock_post.reset_mock() |
| 2384 |
|
| 2385 |
|
| 2386 |
@mock.patch("requests.post") |
| 2387 |
def test_plugin_telegram_markdown_v2(mock_post): |
| 2388 |
"""NotifyTelegram() MarkdownV2.""" |
| 2389 |
# Prepare Mock |
| 2390 |
mock_post.return_value = requests.Request() |
| 2391 |
mock_post.return_value.status_code = requests.codes.ok |
| 2392 |
|
| 2393 |
# Simple success response |
| 2394 |
mock_post.return_value.content = dumps( |
| 2395 |
{ |
| 2396 |
"ok": True, |
| 2397 |
"result": [ |
| 2398 |
{ |
| 2399 |
"update_id": 645421321, |
| 2400 |
"message": { |
| 2401 |
"message_id": 2, |
| 2402 |
"from": { |
| 2403 |
"id": 532389719, |
| 2404 |
"is_bot": False, |
| 2405 |
"first_name": "Chris", |
| 2406 |
"language_code": "en-US", |
| 2407 |
}, |
| 2408 |
"chat": { |
| 2409 |
"id": 532389719, |
| 2410 |
"first_name": "Chris", |
| 2411 |
"type": "private", |
| 2412 |
}, |
| 2413 |
"date": 1519694394, |
| 2414 |
"text": "/start", |
| 2415 |
"entities": [ |
| 2416 |
{ |
| 2417 |
"offset": 0, |
| 2418 |
"length": 6, |
| 2419 |
"type": "bot_command", |
| 2420 |
} |
| 2421 |
], |
| 2422 |
}, |
| 2423 |
}, |
| 2424 |
], |
| 2425 |
} |
| 2426 |
) |
| 2427 |
|
| 2428 |
aobj = Apprise() |
| 2429 |
aobj.add("tgram://123456789:abcdefg_hijklmnop/?mdv=2&format=markdown") |
| 2430 |
assert len(aobj) == 1 |
| 2431 |
assert isinstance(aobj[0], NotifyTelegram) |
| 2432 |
|
| 2433 |
body = "# my message\r\n## more content\r\n\\# already escaped hashtag" |
| 2434 |
|
| 2435 |
# Test with body format set to markdown |
| 2436 |
assert aobj.notify(body=body, body_format=NotifyFormat.TEXT) |
| 2437 |
|
| 2438 |
# 1 call to look up bot owner, and second for notification |
| 2439 |
assert mock_post.call_count == 2 |
| 2440 |
payload = loads(mock_post.call_args_list[1][1]["data"]) |
| 2441 |
|
| 2442 |
# Literal backslashes are escaped along with MarkdownV2 syntax. |
| 2443 |
assert ( |
| 2444 |
payload["text"] == "\\# my message\r\n" |
| 2445 |
"\\#\\# more content\r\n\\\\\\# already escaped hashtag" |
| 2446 |
) |
| 2447 |
|
| 2448 |
mock_post.reset_mock() |
| 2449 |
|
| 2450 |
# We'll iterate over all of the bad unsupported characters |
| 2451 |
mdv2_unsupported = ( |
| 2452 |
"_", |
| 2453 |
"*", |
| 2454 |
"[", |
| 2455 |
"]", |
| 2456 |
"(", |
| 2457 |
")", |
| 2458 |
"~", |
| 2459 |
"`", |
| 2460 |
">", |
| 2461 |
"#", |
| 2462 |
"+", |
| 2463 |
"=", |
| 2464 |
"|", |
| 2465 |
"{", |
| 2466 |
"}", |
| 2467 |
".", |
| 2468 |
"!", |
| 2469 |
"-", |
| 2470 |
) |
| 2471 |
|
| 2472 |
for c in mdv2_unsupported: |
| 2473 |
body = f"bad character: {c}, and already escapped \\{c}" |
| 2474 |
|
| 2475 |
# Test with body format set to markdown |
| 2476 |
assert aobj.notify(body=body, body_format=NotifyFormat.TEXT) |
| 2477 |
assert mock_post.call_count == 1 |
| 2478 |
payload = loads(mock_post.call_args_list[0][1]["data"]) |
| 2479 |
|
| 2480 |
# The literal backslash before the second occurrence is escaped too. |
| 2481 |
assert ( |
| 2482 |
payload["text"] |
| 2483 |
== f"bad character: \\{c}, and already escapped \\\\\\{c}" |
| 2484 |
) |
| 2485 |
|
| 2486 |
mock_post.reset_mock() |
| 2487 |
|
| 2488 |
|
| 2489 |
def test_plugin_telegram_standalone_autolink_dialect(): |
| 2490 |
"""A complete standalone autolink converts safely in both modes.""" |
| 2491 |
|
| 2492 |
# Telegram auto-links the URL after its brackets are removed. |
| 2493 |
assert ( |
| 2494 |
NotifyTelegram._commonmark_to_telegram( |
| 2495 |
"see <https://a*b> now", strict=False |
| 2496 |
) |
| 2497 |
== "see https://a*b now" |
| 2498 |
) |
| 2499 |
|
| 2500 |
# Escape reserved markup characters inside a MarkdownV2 autolink. |
| 2501 |
assert ( |
| 2502 |
NotifyTelegram._commonmark_to_telegram( |
| 2503 |
"see <https://a*b-c> now", strict=True |
| 2504 |
) |
| 2505 |
== "see https://a\\*b\\-c now" |
| 2506 |
) |
| 2507 |
|
| 2508 |
# Strict mode also escapes every other reserved autolink character. |
| 2509 |
assert ( |
| 2510 |
NotifyTelegram._commonmark_to_telegram( |
| 2511 |
"see <https://x.example/*a*_b_[c](d)`e`> now", strict=True |
| 2512 |
) |
| 2513 |
== "see https://x\\.example/\\*a\\*\\_b\\_\\[c\\]\\(d\\)\\`e\\` now" |
| 2514 |
) |
| 2515 |
|
| 2516 |
|
| 2517 |
@mock.patch("requests.post") |
| 2518 |
def test_plugin_telegram_attach_memory(mock_post): |
| 2519 |
"""Regression: AttachMemory must be sendable without OSError.""" |
| 2520 |
from apprise.attachment.memory import AttachMemory |
| 2521 |
|
| 2522 |
response = mock.Mock() |
| 2523 |
response.status_code = requests.codes.ok |
| 2524 |
response.content = dumps({"ok": True, "result": True}) |
| 2525 |
mock_post.return_value = response |
| 2526 |
|
| 2527 |
obj = NotifyTelegram( |
| 2528 |
bot_token="123456789:abcdefg_hijklmnop", targets="12345" |
| 2529 |
) |
| 2530 |
|
| 2531 |
mem = AttachMemory( |
| 2532 |
content=b"<html><body><h1>Test</h1></body></html>", |
| 2533 |
name="test.html", |
| 2534 |
mimetype="text/html", |
| 2535 |
) |
| 2536 |
|
| 2537 |
assert bool(obj.notify(body="Test", attach=mem)) is True |
| 2538 |
assert mock_post.call_count >= 1 |
| 2539 |
|
| 2540 |
|
| 2541 |
@mock.patch("requests.post") |
| 2542 |
def test_plugin_telegram_template_blocks(mock_post, tmpdir): |
| 2543 |
"""NotifyTelegram() - Rich Message template mode.""" |
| 2544 |
response = mock.Mock() |
| 2545 |
response.status_code = requests.codes.ok |
| 2546 |
response.content = dumps({"ok": True}) |
| 2547 |
mock_post.return_value = response |
| 2548 |
|
| 2549 |
# Write a minimal Rich Message JSON template to disk |
| 2550 |
template = tmpdir.join("blocks.json") |
| 2551 |
template.write( |
| 2552 |
cleandoc(""" |
| 2553 |
{ |
| 2554 |
"blocks": [ |
| 2555 |
{ |
| 2556 |
"type": "section_heading", |
| 2557 |
"text": "{{app_title}}" |
| 2558 |
}, |
| 2559 |
{ |
| 2560 |
"type": "paragraph", |
| 2561 |
"text": "{{app_body}}" |
| 2562 |
} |
| 2563 |
] |
| 2564 |
} |
| 2565 |
""") |
| 2566 |
) |
| 2567 |
|
| 2568 |
obj = Apprise.instantiate( |
| 2569 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/" |
| 2570 |
"?template={}&:mykey=myval".format(str(template)) |
| 2571 |
) |
| 2572 |
assert isinstance(obj, NotifyTelegram) |
| 2573 |
|
| 2574 |
# Verify tokens and template were parsed correctly |
| 2575 |
assert "mykey" in obj.tokens |
| 2576 |
assert obj.tokens["mykey"] == "myval" |
| 2577 |
assert obj.template |
| 2578 |
|
| 2579 |
assert ( |
| 2580 |
obj.notify(body="hello", title="world", notify_type=NotifyType.INFO) |
| 2581 |
is True |
| 2582 |
) |
| 2583 |
assert mock_post.called is True |
| 2584 |
|
| 2585 |
# Inspect the posted URL and payload |
| 2586 |
posted_url = mock_post.call_args_list[0][0][0] |
| 2587 |
assert posted_url.endswith("/sendRichMessage") |
| 2588 |
|
| 2589 |
posted = loads(mock_post.call_args_list[0][1]["data"]) |
| 2590 |
assert posted["chat_id"] == "@lead2gold" |
| 2591 |
assert "rich_message" in posted |
| 2592 |
blocks = posted["rich_message"]["blocks"] |
| 2593 |
assert any(b.get("type") == "section_heading" for b in blocks) |
| 2594 |
assert any(b.get("type") == "paragraph" for b in blocks) |
| 2595 |
|
| 2596 |
heading = next(b for b in blocks if b.get("type") == "section_heading") |
| 2597 |
assert heading["text"] == "world" |
| 2598 |
paragraph = next(b for b in blocks if b.get("type") == "paragraph") |
| 2599 |
assert paragraph["text"] == "hello" |
| 2600 |
|
| 2601 |
|
| 2602 |
@mock.patch("requests.post") |
| 2603 |
def test_plugin_telegram_template_invalid_json(mock_post, tmpdir): |
| 2604 |
"""NotifyTelegram() - Rich Message template with invalid JSON fails.""" |
| 2605 |
response = mock.Mock() |
| 2606 |
response.status_code = requests.codes.ok |
| 2607 |
response.content = dumps({"ok": True}) |
| 2608 |
mock_post.return_value = response |
| 2609 |
|
| 2610 |
template = tmpdir.join("bad.json") |
| 2611 |
template.write("{ not valid json }") |
| 2612 |
|
| 2613 |
obj = Apprise.instantiate( |
| 2614 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2615 |
str(template) |
| 2616 |
) |
| 2617 |
) |
| 2618 |
assert isinstance(obj, NotifyTelegram) |
| 2619 |
|
| 2620 |
assert ( |
| 2621 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2622 |
) |
| 2623 |
assert mock_post.called is False |
| 2624 |
|
| 2625 |
|
| 2626 |
@mock.patch("requests.post") |
| 2627 |
def test_plugin_telegram_template_blocks_not_list(mock_post, tmpdir): |
| 2628 |
"""NotifyTelegram() - 'blocks' missing/not-a-list/empty is rejected.""" |
| 2629 |
response = mock.Mock() |
| 2630 |
response.status_code = requests.codes.ok |
| 2631 |
response.content = dumps({"ok": True}) |
| 2632 |
mock_post.return_value = response |
| 2633 |
|
| 2634 |
# 'blocks' key entirely missing |
| 2635 |
template = tmpdir.join("missing_blocks.json") |
| 2636 |
template.write('{"text": "no blocks here"}') |
| 2637 |
|
| 2638 |
obj = Apprise.instantiate( |
| 2639 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2640 |
str(template) |
| 2641 |
) |
| 2642 |
) |
| 2643 |
assert isinstance(obj, NotifyTelegram) |
| 2644 |
assert ( |
| 2645 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2646 |
) |
| 2647 |
assert mock_post.called is False |
| 2648 |
|
| 2649 |
# 'blocks' present but not a list |
| 2650 |
template.write('{"blocks": "not-a-list"}') |
| 2651 |
obj2 = Apprise.instantiate( |
| 2652 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2653 |
str(template) |
| 2654 |
) |
| 2655 |
) |
| 2656 |
assert isinstance(obj2, NotifyTelegram) |
| 2657 |
assert ( |
| 2658 |
obj2.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2659 |
) |
| 2660 |
assert mock_post.called is False |
| 2661 |
|
| 2662 |
# Empty list is also rejected |
| 2663 |
template.write('{"blocks": []}') |
| 2664 |
obj3 = Apprise.instantiate( |
| 2665 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2666 |
str(template) |
| 2667 |
) |
| 2668 |
) |
| 2669 |
assert isinstance(obj3, NotifyTelegram) |
| 2670 |
assert ( |
| 2671 |
obj3.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2672 |
) |
| 2673 |
assert mock_post.called is False |
| 2674 |
|
| 2675 |
|
| 2676 |
@mock.patch("requests.post") |
| 2677 |
def test_plugin_telegram_template_block_missing_type(mock_post, tmpdir): |
| 2678 |
"""NotifyTelegram() - a block dict without 'type' is rejected.""" |
| 2679 |
response = mock.Mock() |
| 2680 |
response.status_code = requests.codes.ok |
| 2681 |
response.content = dumps({"ok": True}) |
| 2682 |
mock_post.return_value = response |
| 2683 |
|
| 2684 |
template = tmpdir.join("no_type.json") |
| 2685 |
template.write('{"blocks": [{"text": "hi"}]}') |
| 2686 |
|
| 2687 |
obj = Apprise.instantiate( |
| 2688 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2689 |
str(template) |
| 2690 |
) |
| 2691 |
) |
| 2692 |
assert isinstance(obj, NotifyTelegram) |
| 2693 |
assert ( |
| 2694 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2695 |
) |
| 2696 |
assert mock_post.called is False |
| 2697 |
|
| 2698 |
|
| 2699 |
@mock.patch("requests.post") |
| 2700 |
def test_plugin_telegram_template_content_not_dict(mock_post, tmpdir): |
| 2701 |
"""NotifyTelegram() - template that parses to a JSON array is |
| 2702 |
rejected.""" |
| 2703 |
response = mock.Mock() |
| 2704 |
response.status_code = requests.codes.ok |
| 2705 |
response.content = dumps({"ok": True}) |
| 2706 |
mock_post.return_value = response |
| 2707 |
|
| 2708 |
template = tmpdir.join("array.json") |
| 2709 |
template.write('[{"type": "paragraph"}]') |
| 2710 |
|
| 2711 |
obj = Apprise.instantiate( |
| 2712 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2713 |
str(template) |
| 2714 |
) |
| 2715 |
) |
| 2716 |
assert isinstance(obj, NotifyTelegram) |
| 2717 |
assert ( |
| 2718 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2719 |
) |
| 2720 |
assert mock_post.called is False |
| 2721 |
|
| 2722 |
|
| 2723 |
@mock.patch("requests.post") |
| 2724 |
def test_plugin_telegram_template_block_not_dict(mock_post, tmpdir): |
| 2725 |
"""NotifyTelegram() - non-dict entry in blocks list is rejected.""" |
| 2726 |
response = mock.Mock() |
| 2727 |
response.status_code = requests.codes.ok |
| 2728 |
response.content = dumps({"ok": True}) |
| 2729 |
mock_post.return_value = response |
| 2730 |
|
| 2731 |
template = tmpdir.join("bad_block.json") |
| 2732 |
template.write('{"blocks": ["not-a-dict"]}') |
| 2733 |
|
| 2734 |
obj = Apprise.instantiate( |
| 2735 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2736 |
str(template) |
| 2737 |
) |
| 2738 |
) |
| 2739 |
assert isinstance(obj, NotifyTelegram) |
| 2740 |
assert ( |
| 2741 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2742 |
) |
| 2743 |
assert mock_post.called is False |
| 2744 |
|
| 2745 |
|
| 2746 |
@mock.patch("requests.post") |
| 2747 |
def test_plugin_telegram_template_load_error(mock_post, tmpdir): |
| 2748 |
"""NotifyTelegram() - template OSError during read fails gracefully.""" |
| 2749 |
response = mock.Mock() |
| 2750 |
response.status_code = requests.codes.ok |
| 2751 |
response.content = dumps({"ok": True}) |
| 2752 |
mock_post.return_value = response |
| 2753 |
|
| 2754 |
# Write an empty file so the attachment resolves but open() can be |
| 2755 |
# mocked to fail |
| 2756 |
template = tmpdir.join("empty.json") |
| 2757 |
template.write("") |
| 2758 |
|
| 2759 |
obj = Apprise.instantiate( |
| 2760 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2761 |
str(template) |
| 2762 |
) |
| 2763 |
) |
| 2764 |
assert isinstance(obj, NotifyTelegram) |
| 2765 |
|
| 2766 |
with mock.patch("builtins.open", side_effect=OSError): |
| 2767 |
assert ( |
| 2768 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) |
| 2769 |
is False |
| 2770 |
) |
| 2771 |
assert mock_post.called is False |
| 2772 |
|
| 2773 |
|
| 2774 |
def test_plugin_telegram_template_bad_tokens(): |
| 2775 |
"""NotifyTelegram() rejects an invalid template token type.""" |
| 2776 |
with pytest.raises(AppriseImproperlyConfigured): |
| 2777 |
NotifyTelegram( |
| 2778 |
bot_token="123456789:abcdefg_hijklmnop", |
| 2779 |
targets="lead2gold", |
| 2780 |
tokens="not-a-dict", |
| 2781 |
) |
| 2782 |
|
| 2783 |
|
| 2784 |
def test_plugin_telegram_template_add_failure(): |
| 2785 |
"""NotifyTelegram() rejects a template attachment it cannot add.""" |
| 2786 |
with mock.patch("apprise.plugins.telegram.AppriseAttachment") as mock_cls: |
| 2787 |
inst = mock.MagicMock() |
| 2788 |
inst.__len__ = mock.Mock(return_value=0) |
| 2789 |
mock_cls.return_value = inst |
| 2790 |
|
| 2791 |
with pytest.raises(AppriseImproperlyConfigured): |
| 2792 |
NotifyTelegram( |
| 2793 |
bot_token="123456789:abcdefg_hijklmnop", |
| 2794 |
targets="lead2gold", |
| 2795 |
template="file:///some/template.json", |
| 2796 |
) |
| 2797 |
|
| 2798 |
|
| 2799 |
@mock.patch("requests.post") |
| 2800 |
def test_plugin_telegram_template_inaccessible(mock_post, tmpdir): |
| 2801 |
"""NotifyTelegram() - template attachment that cannot be accessed |
| 2802 |
fails.""" |
| 2803 |
response = mock.Mock() |
| 2804 |
response.status_code = requests.codes.ok |
| 2805 |
response.content = dumps({"ok": True}) |
| 2806 |
mock_post.return_value = response |
| 2807 |
|
| 2808 |
# Point to a template file that does not exist |
| 2809 |
missing = str(tmpdir.join("missing.json")) |
| 2810 |
|
| 2811 |
obj = Apprise.instantiate( |
| 2812 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2813 |
missing |
| 2814 |
) |
| 2815 |
) |
| 2816 |
assert isinstance(obj, NotifyTelegram) |
| 2817 |
assert ( |
| 2818 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 2819 |
) |
| 2820 |
assert mock_post.called is False |
| 2821 |
|
| 2822 |
|
| 2823 |
@mock.patch("requests.post") |
| 2824 |
def test_plugin_telegram_template_none_token_value(mock_post, tmpdir): |
| 2825 |
"""NotifyTelegram() - a None token value (e.g. app_image_url) is |
| 2826 |
coerced to an empty string before JSON-escaping.""" |
| 2827 |
response = mock.Mock() |
| 2828 |
response.status_code = requests.codes.ok |
| 2829 |
response.content = dumps({"ok": True}) |
| 2830 |
mock_post.return_value = response |
| 2831 |
|
| 2832 |
# Template references app_image_url which will be None when |
| 2833 |
# include_image=False |
| 2834 |
template = tmpdir.join("img.json") |
| 2835 |
template.write( |
| 2836 |
'{"blocks": [{"type": "paragraph",' |
| 2837 |
' "text": "{{app_body}} img={{app_image_url}}"}]}' |
| 2838 |
) |
| 2839 |
|
| 2840 |
obj = Apprise.instantiate( |
| 2841 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/" |
| 2842 |
"?image=no&template={}".format(str(template)) |
| 2843 |
) |
| 2844 |
assert isinstance(obj, NotifyTelegram) |
| 2845 |
assert ( |
| 2846 |
obj.notify(body="hi", title="y", notify_type=NotifyType.INFO) is True |
| 2847 |
) |
| 2848 |
posted = loads(mock_post.call_args_list[0][1]["data"]) |
| 2849 |
paragraph = posted["rich_message"]["blocks"][0] |
| 2850 |
assert paragraph["text"] == "hi img=" |
| 2851 |
|
| 2852 |
|
| 2853 |
@mock.patch("requests.post") |
| 2854 |
def test_plugin_telegram_template_url_roundtrip(mock_post, tmpdir): |
| 2855 |
"""NotifyTelegram() - template + tokens survive url()/parse_url() |
| 2856 |
round-trip.""" |
| 2857 |
response = mock.Mock() |
| 2858 |
response.status_code = requests.codes.ok |
| 2859 |
response.content = dumps({"ok": True}) |
| 2860 |
mock_post.return_value = response |
| 2861 |
|
| 2862 |
template = tmpdir.join("rt.json") |
| 2863 |
template.write( |
| 2864 |
cleandoc(""" |
| 2865 |
{ |
| 2866 |
"blocks": [ |
| 2867 |
{"type": "paragraph", "text": "{{app_body}}"} |
| 2868 |
] |
| 2869 |
} |
| 2870 |
""") |
| 2871 |
) |
| 2872 |
|
| 2873 |
obj1 = NotifyTelegram( |
| 2874 |
bot_token="123456789:abcdefg_hijklmnop", |
| 2875 |
targets="lead2gold", |
| 2876 |
template=str(template), |
| 2877 |
tokens={"key1": "val1", "key2": "val2"}, |
| 2878 |
) |
| 2879 |
|
| 2880 |
url = obj1.url() |
| 2881 |
result = NotifyTelegram.parse_url(url) |
| 2882 |
assert result is not None |
| 2883 |
|
| 2884 |
obj2 = NotifyTelegram(**result) |
| 2885 |
assert isinstance(obj2, NotifyTelegram) |
| 2886 |
|
| 2887 |
# Connection identity must be preserved |
| 2888 |
assert obj1.url_identifier == obj2.url_identifier |
| 2889 |
|
| 2890 |
# Tokens must survive the round-trip |
| 2891 |
assert obj2.tokens.get("key1") == "val1" |
| 2892 |
assert obj2.tokens.get("key2") == "val2" |
| 2893 |
|
| 2894 |
# Template must be present after round-trip |
| 2895 |
assert obj2.template |
| 2896 |
|
| 2897 |
|
| 2898 |
@mock.patch("requests.post") |
| 2899 |
def test_plugin_telegram_template_multi_target(mock_post, tmpdir): |
| 2900 |
"""NotifyTelegram() - Rich Message is POSTed once per target.""" |
| 2901 |
response = mock.Mock() |
| 2902 |
response.status_code = requests.codes.ok |
| 2903 |
response.content = dumps({"ok": True}) |
| 2904 |
mock_post.return_value = response |
| 2905 |
|
| 2906 |
template = tmpdir.join("multi.json") |
| 2907 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 2908 |
|
| 2909 |
obj = Apprise.instantiate( |
| 2910 |
"tgram://123456789:abcdefg_hijklmnop/12345/67890:55/" |
| 2911 |
"?template={}".format(str(template)) |
| 2912 |
) |
| 2913 |
assert isinstance(obj, NotifyTelegram) |
| 2914 |
assert len(obj.targets) == 2 |
| 2915 |
|
| 2916 |
assert obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is True |
| 2917 |
assert mock_post.call_count == 2 |
| 2918 |
|
| 2919 |
posted_1 = loads(mock_post.call_args_list[0][1]["data"]) |
| 2920 |
posted_2 = loads(mock_post.call_args_list[1][1]["data"]) |
| 2921 |
assert posted_1["chat_id"] == 12345 |
| 2922 |
assert "message_thread_id" not in posted_1 |
| 2923 |
assert posted_2["chat_id"] == 67890 |
| 2924 |
assert posted_2["message_thread_id"] == 55 |
| 2925 |
|
| 2926 |
|
| 2927 |
@mock.patch("requests.post") |
| 2928 |
def test_plugin_telegram_template_with_attachment(mock_post, tmpdir): |
| 2929 |
"""NotifyTelegram() - attachments remain untouched in template mode, |
| 2930 |
sent separately after the Rich Message.""" |
| 2931 |
response = mock.Mock() |
| 2932 |
response.status_code = requests.codes.ok |
| 2933 |
response.content = dumps({"ok": True}) |
| 2934 |
mock_post.return_value = response |
| 2935 |
|
| 2936 |
template = tmpdir.join("attach.json") |
| 2937 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 2938 |
|
| 2939 |
obj = Apprise.instantiate( |
| 2940 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2941 |
str(template) |
| 2942 |
) |
| 2943 |
) |
| 2944 |
assert isinstance(obj, NotifyTelegram) |
| 2945 |
|
| 2946 |
# Pass a raw (not pre-wrapped) attachment path; template mode must |
| 2947 |
# still normalize it into an AppriseAttachment internally. |
| 2948 |
path = os.path.join(TEST_VAR_DIR, "apprise-test.gif") |
| 2949 |
|
| 2950 |
assert obj.notify(body="hi", title="y", attach=path) is True |
| 2951 |
|
| 2952 |
# First call is the Rich Message itself |
| 2953 |
first_url = mock_post.call_args_list[0][0][0] |
| 2954 |
assert first_url.endswith("/sendRichMessage") |
| 2955 |
|
| 2956 |
# Second call is the attachment, sent via the normal send_media() path |
| 2957 |
second_url = mock_post.call_args_list[1][0][0] |
| 2958 |
assert not second_url.endswith("/sendRichMessage") |
| 2959 |
|
| 2960 |
|
| 2961 |
@mock.patch("requests.post") |
| 2962 |
def test_plugin_telegram_template_attachment_failure(mock_post, tmpdir): |
| 2963 |
"""NotifyTelegram() - a failed attachment send flags an overall |
| 2964 |
failure in Rich Message mode.""" |
| 2965 |
response = mock.Mock() |
| 2966 |
response.status_code = requests.codes.ok |
| 2967 |
response.content = dumps({"ok": True}) |
| 2968 |
mock_post.return_value = response |
| 2969 |
|
| 2970 |
template = tmpdir.join("attach_fail.json") |
| 2971 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 2972 |
|
| 2973 |
obj = Apprise.instantiate( |
| 2974 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 2975 |
str(template) |
| 2976 |
) |
| 2977 |
) |
| 2978 |
assert isinstance(obj, NotifyTelegram) |
| 2979 |
|
| 2980 |
# Point to an attachment that cannot be accessed |
| 2981 |
attach = AppriseAttachment("file:///path/does/not/exist.gif") |
| 2982 |
|
| 2983 |
assert obj.notify(body="hi", title="y", attach=attach) is False |
| 2984 |
|
| 2985 |
|
| 2986 |
@mock.patch("requests.post") |
| 2987 |
def test_plugin_telegram_template_preview_enabled(mock_post, tmpdir): |
| 2988 |
"""NotifyTelegram() - preview=yes omits link_preview_options.""" |
| 2989 |
response = mock.Mock() |
| 2990 |
response.status_code = requests.codes.ok |
| 2991 |
response.content = dumps({"ok": True}) |
| 2992 |
mock_post.return_value = response |
| 2993 |
|
| 2994 |
template = tmpdir.join("preview.json") |
| 2995 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 2996 |
|
| 2997 |
obj = Apprise.instantiate( |
| 2998 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/" |
| 2999 |
"?preview=yes&template={}".format(str(template)) |
| 3000 |
) |
| 3001 |
assert isinstance(obj, NotifyTelegram) |
| 3002 |
assert obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is True |
| 3003 |
posted = loads(mock_post.call_args_list[0][1]["data"]) |
| 3004 |
assert "link_preview_options" not in posted |
| 3005 |
|
| 3006 |
|
| 3007 |
@mock.patch("requests.post") |
| 3008 |
def test_plugin_telegram_template_http_error(mock_post, tmpdir): |
| 3009 |
"""NotifyTelegram() - Rich Message HTTP error is handled gracefully.""" |
| 3010 |
response = mock.Mock() |
| 3011 |
response.status_code = requests.codes.internal_server_error |
| 3012 |
response.content = dumps({"description": "failure"}) |
| 3013 |
mock_post.return_value = response |
| 3014 |
|
| 3015 |
template = tmpdir.join("err.json") |
| 3016 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 3017 |
|
| 3018 |
obj = Apprise.instantiate( |
| 3019 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 3020 |
str(template) |
| 3021 |
) |
| 3022 |
) |
| 3023 |
assert isinstance(obj, NotifyTelegram) |
| 3024 |
assert ( |
| 3025 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 3026 |
) |
| 3027 |
|
| 3028 |
# Also cover the case where the error response body itself is not |
| 3029 |
# parsable JSON (falls back to the generic HTTP status string) |
| 3030 |
response.content = b"not-json" |
| 3031 |
assert ( |
| 3032 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 3033 |
) |
| 3034 |
|
| 3035 |
|
| 3036 |
@mock.patch("requests.post") |
| 3037 |
def test_plugin_telegram_template_request_exception(mock_post, tmpdir): |
| 3038 |
"""NotifyTelegram() - Rich Message RequestException is handled |
| 3039 |
gracefully.""" |
| 3040 |
mock_post.side_effect = requests.RequestException() |
| 3041 |
|
| 3042 |
template = tmpdir.join("exc.json") |
| 3043 |
template.write('{"blocks": [{"type": "paragraph", "text": "hi"}]}') |
| 3044 |
|
| 3045 |
obj = Apprise.instantiate( |
| 3046 |
"tgram://123456789:abcdefg_hijklmnop/lead2gold/?template={}".format( |
| 3047 |
str(template) |
| 3048 |
) |
| 3049 |
) |
| 3050 |
assert isinstance(obj, NotifyTelegram) |
| 3051 |
assert ( |
| 3052 |
obj.notify(body="x", title="y", notify_type=NotifyType.INFO) is False |
| 3053 |
) |