caronc / nzb-notify

Push Notifications to a large number of supported services for NZBGet and SABnzbd (based on Apprise)
GNU General Public License v3.0
129 stars 15 forks source link

Add Telegram Notification support (https://telegram.org) #9

Closed Royal2000H closed 8 years ago

Royal2000H commented 8 years ago

Hi, would it be possible to support Telegram notifications?

For implementation reference, PlexPy supports sending Telegram notifications for Plex events. Would be great to have notifications for nzb events too. Ideally, both PlexPy and nzb-notify would be able to play along and relay notifications into Telegram through the same bot username. PlexPy prefaces every message with "PlexPy" so perhaps nzb-notify would have a similar prefacing system.

caronc commented 8 years ago

Hi,

I don't have a telegram account, but you've given me more than enough information to work with. Plus the API documentation on Telegrams website is awesome.

It might appear rather crude at this point, but it should work; would you mind trying it out for me? You'll need to check out the master branch (not the release).

Royal2000H commented 8 years ago

Whoops, sorry for not responding, I didn't see the notification.

Once the next stable release of Sabnzbd comes out, I'll install it along with nzb-notify and then I can test this out.

Royal2000H commented 8 years ago

I finally got around to doing this but got an error. Perhaps this error should be its own issue:

File "sabnzbd-notify.py", line 25, in from signal import SIGKILL ImportError: cannot import name SIGKILL

This is both through sabnzbd and through command line using sabnzbd-notify.py. I suspect me being on Windows may be the issue.

Royal2000H commented 8 years ago

If I try to test with Notify.py:

python Notify.py -s tgram://bot:token/chatid -t "Hello" -b `"World"

(With actual numbers/letters in bot token and chatid (that work in PlexPy).

I get:

2016-09-17 20:00:39,365 - 5864 - ERROR - Could not parse URL: None

The number before the error is different every time.

caronc commented 8 years ago

Thanks for your feedback! I just pushed a small change that 'may' or may not fix the problem.

Royal2000H commented 8 years ago

Now I get:

2016-09-17 22:21:20,927 - 15160 - DEBUG - Telegram POST URL: https://api.telegram.org/bot*******:**********************************/sendMessage 2016-09-17 22:21:20,927 - 15160 - DEBUG - Telegram Payload: {'text': u'<h1>Hello</h1><p>World</p>', 'parse_mode': 'HTML', 'chat_id': '********'} 2016-09-17 22:21:21,743 - 15160 - WARNING - Failed to send Telegram:<_sre.SRE_Match object at 0x01CA6A70> notification: Bad Request - Unsupported Parameters. (error=400).

caronc commented 8 years ago

Hmm... it might be related to how you're formatting the URL (although it seems right). I pushed a small fix that just cleans up that error message (but doesn't fix your problem).

What happens if you run the same command with the -D switch (--debug mode). It might help out with what's going on here.

It's the few lines of the debug output we're especially interested in:

2016-09-17 22:31:38,629 - 32469 - DEBUG - Telegram POST URL: https://api.telegram.org/bot12456789:BBEZa-i2Yhnf74SudtSW_mCrgjkjkhah0p8/sendMessage
2016-09-17 22:31:38,629 - 32469 - DEBUG - Telegram Payload: {'text': u'<h1>test</h1><p>body</p>', 'parse_mode': 'HTML', 'chat_id': '@lead2gold'}

You want the output to look something like that.

Royal2000H commented 8 years ago

I probably should have submitted another comment instead of editing above, so you'd see it. Anyway I did it again since your new commit, although the results are essentially the same.

2016-09-17 22:34:21,644 - 12052 - DEBUG - Server parsed to: {'host': '1234567890', 'user': None, 'query': '0123456789', 'url': 'tgram://1234567890/ABCdefGHIJKLmnop1q23rSTUvwXyZ/0123456789', 'path': '/ABCdefGHIJKLmnop1q23rSTUvwXyZ/', 'fullpath': '/ABCdefGHIJKLmnop1q23rSTUvwXyZ/0123456789', 'password': None, 'port': None, 'qsd': {}, 'schema': 'tgram'} 2016-09-17 22:34:21,651 - 12052 - DEBUG - Telegram POST URL: https://api.telegram.org/bot1234567890:ABCdefGHIJKLmnop1q23rSTUvwXyZ/sendMessage 2016-09-17 22:34:21,651 - 12052 - DEBUG - Telegram Payload: {'text': u'<h1>Hello</h1><p>World</p>', 'parse_mode': 'HTML', 'chat_id': '0123456789'} 2016-09-17 22:34:22,414 - 12052 - WARNING - Failed to send Telegram:0123456789 notification: Bad Request - Unsupported Parameters. (error=400).

(Replaced my bot tokens and chat IDs)

caronc commented 8 years ago

Maybe you can give me a hand so I don't bother you so much. I will just set up my own bot and work with it until I'm satisfied. Then I'll ping you again with great news and prevent you from wasting your time testing all of this out for me (which by the way I really appreciate - thank you for your patience so far).

Here's my problem: I created 2 bots with the BotFather and both of the API keys that were generated for me give me an authorization denied (401 error http error code) when I try to interface with them. I'm definitely typing them out right. I've Googled everywhere and am not having much luck. The fact you're getting a 400 error code (payload issue) means you're key is working great and you're getting past the part I'm stuck at. Is there something I need to do to the bots after I create them to enable there ability to relay messages? I feel like I'm missing some (obvious) step to get to where you're at.

Royal2000H commented 8 years ago

For potential future visitors: We debugged offline. The problem was unsupported html tags (<h1> and <p> are unsupported by telegram).

caronc commented 8 years ago

@Royal2000H, you are a saint; I appreciate your patients. Hopefully we nailed it this time. Please try again and let me know.

caronc commented 8 years ago

With respect to your SIGKILL error, i just pushed this commit which will hopefully fix that for you: 22b8d57e7d88237db93726dc03b3583f8e48d454