Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'telegram' has no attribute 'notify' #260

Open
colossatr0n opened this issue Jan 29, 2023 · 2 comments
Open

AttributeError: module 'telegram' has no attribute 'notify' #260

colossatr0n opened this issue Jan 29, 2023 · 2 comments

Comments

@colossatr0n
Copy link

This issue is for documentation purposes and for others running into this same error.

When running ntfy -b telegram send "test message" I get the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/ntfy/__init__.py", line 61, in notify
    notify_ret = notifier.notify(
AttributeError: module 'telegram' has no attribute 'notify'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/ntfy", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/ntfy/cli.py", line 362, in main
    return notify(
  File "/usr/local/lib/python3.10/site-packages/ntfy/__init__.py", line 76, in notify
    args, _, _, defaults = getargspec(notifier.notify)
AttributeError: module 'telegram' has no attribute 'notify'

This doesn't have anything to do with ntfy but is actually due to a bug in the telegram-send repo for version 20.0.

The issue can be found here: rahiel/telegram-send#115

A workaround for this issue can be found here.

@karlicoss
Copy link

karlicoss commented Jul 11, 2023

IMO the issue not even in logging ImportError (although this does help a bit), but because the error handling isn't robust enough.
There are actually multiple issues here at the same time:

ntfy/ntfy/__init__.py

Lines 41 to 50 in 698333c

try:
notifier = import_module('ntfy.backends.{}'.format(backend))
except ImportError:
try:
notifier = import_module(backend)
except ImportError:
logging.getLogger(__name__).error(
'Invalid backend {}'.format(backend))
ret = 1
continue

  • the first import fails due to the MAX_MESSAGE_LENGTH issue
    ImportError: cannot import name 'MAX_MESSAGE_LENGTH' from 'telegram.constants'

  • since ImportError is very broad (it doesn't check that the name of the module in exception is ntry.backedns.telegram), it tries to handle the error

    imo .args or .name for the exception object should be checked first, and if it's unrelated to the backend itself, it should be simply rethrown so it's clear what's the actual error.

  • then for some reason the code tries to import the backend as telegram

    not sure what the idea here, in case you wrote a backend and don't want it under ntfy.backends.* namespace?

  • which actually works because telegram is a library which accesses telegram API, but not a notify backend

    imo here right after importing we should check if the module we imported contains .notify function. If not, it's not a ntfy backend and the original Exception should be rethrown (again so there is less indirection and easier to understand what actually failed)

@karlicoss
Copy link

Overall, even if you hack around the issue above with MAX_MESSAGE_LENGTH, sending messages to telegram still doesn't work and telegram_send library seems abandoned. So likely we need to switch to something else :( rahiel/telegram-send#121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants