CJPCNZ / CustomPush

Custom Push Notifications for Sonarr, Radarr and Lidarr
12 stars 5 forks source link

Can it use with telegram? #15

Closed xiNeRoar closed 2 years ago

xiNeRoar commented 3 years ago

As title, can this be use with telegram? If so, may i know how to setup this?

CJPCNZ commented 3 years ago

Hi @xiNeRoar, this is not currently supported as Telegram requires app registration but if you have a Telegram API key already then it should be possible.

xiNeRoar commented 3 years ago

For Telegram API Key, do you mean telegram bot token?

CJPCNZ commented 3 years ago

Yes a bot token is required as well as a chat ID - I've had a read of Telegram's documentation and it seems like it should be straightforward. I'll see if I can write something for you this week and I'll let you know here once it's ready for testing.

xiNeRoar commented 3 years ago

thank you very much!

CJPCNZ commented 3 years ago

I've just uploaded what I think should work.

Please can you test and let me know which scripts you use (Sonarr/Radarr/Lidarr) and what the outcome is (or any test message). Remember to blank your API key/channel ID.

If you don't have a bot and channel ID already, please be sure to follow the instructions here: https://core.telegram.org/bots#3-how-do-i-create-a-bot

xiNeRoar commented 3 years ago

Thanks!

Since i am still at work. I will test it out around 8 to 10 hours later and let you know what is the result. Thank you so much for your help :D

在 2021年5月4日週二 14:18,Chris @.***> 寫道:

I've just uploaded what I think should work.

Please can you test and let me know which scripts you use (Sonarr/Radarr/Lidarr) and what the outcome is (or any test message). Remember to blank your API key/channel ID.

If you don't have a bot and channel ID already, please be sure to follow the instructions here: https://core.telegram.org/bots#3-how-do-i-create-a-bot

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/CJPCNZ/CustomPush/issues/15#issuecomment-831707916, or unsubscribe https://github.com/notifications/unsubscribe-auth/APZGB6T4ZY2ZMSNYNIYMOUTTL6GUZANCNFSM437B6DJQ .

xiNeRoar commented 3 years ago

Dear CJPCNZ,

Here is what i get when trying to use the script. image

xiNeRoar commented 3 years ago

forgot to mention, i am using the Radarr telegram .ps1 file, and here is the screenshot of my setup; image Some more questions that i want to ask;

  1. Can i use telegram "sendPhoto" instead of "sendMessage"? because i want to include the movie poster in my notification.
  2. Where can i have a list of the variables that available? like "radarr_movie_id"? Is there something like "radarr_movie_image"?
  3. May i ask how can i customize the format of the message? configure it in #Format content or #Prepare push notification body? Again, thank you very much!!! 👍
CJPCNZ commented 3 years ago

Thank you for the logs. It seems that the message text is not being sent correctly. I will investigate and update the code and notify you when it is ready.

To answer your questions:

  1. You can send photo as well. There is a sample of the PushBullet code in this file which I can adapt to Telegram if you want: https://github.com/CJPCNZ/CustomPush/blob/master/Radarr/PushBullet-WithImage.ps1
  2. There is a list of available API endpoints here: https://radarr.video/docs/api/
  3. You can configure the format in #Format content section of the code.
xiNeRoar commented 3 years ago

Thank you so much for your detailed explanation. :D

CJPCNZ commented 3 years ago

Could you please replace the last line: Invoke-WebRequest -Method POST -Uri "https://api.telegram.org/bot$pushkey/sendMessage" -UseBasicParsing -Header $headers -Body $pushBody With: Invoke-WebRequest -Method POST -Uri "https://api.telegram.org/bot$pushkey/sendMessage" -UseBasicParsing -Header $headers -Body "{ 'chat_id':$pushtag, 'text':$pushmessage }"

If this also fails then there might be an issue with the encoding of the body message.

xiNeRoar commented 3 years ago

i still got this message.. image does that mean i have to put some dummy message inside the file or what does it mean?

CJPCNZ commented 3 years ago

I've found the issue, we needed to ConvertTo-JSON to be compatible with the Telegram API. I've updated the Radarr script for you to test now.

xiNeRoar commented 3 years ago

I've found the issue, we needed to ConvertTo-JSON to be compatible with the Telegram API. I've updated the Radarr script for you to test now.

Thanks soooooooo much! finally I got it working right now. Just a little more to ask, i have read through all the documentations but cannot found how to customize the message body..will you possible to explain a little more on this? >< For example, if i would like my message;

Latest Movie :
(Movie title) (Released year) is available right now!
Please login to (http://mydomain.com) to watch!

Movie introduction :
(Movie overview)

(Movie image)

How can i achieve this?

CJPCNZ commented 3 years ago

I'm glad it is now working! 😄

I am working on getting the images to send however I have not had time the past two days. I'll hopefully get some time tonight or tomorrow to finish it. In the meantime you can change $pushmessage to a 'here-string' instead like this:

$pushmessage = @"
Latest Movie :
$radarr_movie_title $env:radarr_movie_year is available right now!
Please login to http://mydomain.com to watch!

Movie introduction :
$radarr_description
"@

If the $env:radarr_movie_year doesn't work, just declare it in the first few lines like so: $radarr_movie_year = $env:radarr_movie_year

All of the fields can be found under the API Schemas at the bottom of the page here: Radarr API

Once I get the image part working I'll let you know.

CJPCNZ commented 3 years ago

Sadly I cannot get the image upload working in PowerShell 5.

Can you confirm what version of PowerShell you have? Just type $PSVersiontable at the prompt

You should get something like this:

PS C:\Users\CJPCNZ> $PSVersiontable
Name                           Value
----                           -----
PSVersion                      5.1.19041.906
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.906
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
xiNeRoar commented 3 years ago

here is my screencap following your instruction; image

CJPCNZ commented 3 years ago

Unfortunately the image attachment won't work. For some reason Telegram will not accept images in any format from PowerShell version 5.x however PushBullet seems to manage this without issues.

I have not done adequate testing of the below steps so please use them at your own risk!

This one-liner will trigger the install of PowerShell version 7: iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

You will need to MANUALLY call this for the telegram image script by updating your Radarr custom script path to: C:\Program Files\PowerShell\7\pwsh.exe

For PowerShell Version 6.1.0 or higher you can use the following code to send images to Telegram:

# Define required variables
$radarr_movie_id = $env:radarr_movie_id 
$radarr_movie_title = $env:radarr_movie_title 
$radarr_moviefile_quality = $env:radarr_moviefile_quality
$radarr_movie_year = $env:radarr_movie_year

$apikey="" # Your Radarr API key 
$radarr_address="http://localhost:7878" # Your Radarr address (including base_url) 
$pushkey="12345678:replace-me-with-real-token" # Your Telegram Bot API key
$pushtag="@channelusername" # Your Teleram Chat ID

# Grab movie information
$radarr_movie=$(Invoke-WebRequest -URI $radarr_address/api/movie/$radarr_movie_id -UseBasicParsing -Header @{"X-Api-Key" = $apikey}) | ConvertFrom-Json
$radarr_description = $radarr_movie | Select-Object -ExpandProperty overview
$radarr_image = $radarr_address + "/MediaCover/" + $radarr_movie_id + "/poster.jpg"
Invoke-WebRequest $radarr_image -UseBasicParsing -OutFile "$PSScriptRoot\poster.jpg"

# Format content
$pushmessage = @"
Latest Movie :
$radarr_movie_title $radarr_movie_year is available right now!
Please login to http://mydomain.com to watch!

Movie introduction :
$radarr_description
"@

# Prepare push notification body
$form = @{
    caption = "$pushmessage"
    chat_id = $pushtag
    photo = Get-Item "$PSScriptRoot\poster.jpg"
} 

# Send push notification
Invoke-WebRequest -Method POST -Uri "https://api.telegram.org/bot$pushkey/sendPhoto" -UseBasicParsing -Header $headers -Form $form

Remove-Item "$PSScriptRoot\poster.jpg"
xiNeRoar commented 3 years ago

thank you so much!! i will try it out when i have time and will let you know the result. Thank you so much for all the work you have done :D

CJPCNZ commented 3 years ago

Hi @xiNeRoar how did you get on with testing?

Let me know if there are any issues or if you would like this closed.

xiNeRoar commented 3 years ago

Hi @xiNeRoar how did you get on with testing?

Let me know if there are any issues or if you would like this closed.

Hi @CJPCNZ, sorry for replying late. I have tried it and found it does not work. Can't even send out a proper notification and i do not know why.

xiNeRoar commented 3 years ago

Hi @xiNeRoar how did you get on with testing?

Let me know if there are any issues or if you would like this closed.

I can share some screenshot to you later to let you see what is happening

CJPCNZ commented 3 years ago

Hi @xiNeRoar are you please able to share logs or screenshots for the error?

Thanks!

CJPCNZ commented 2 years ago

Closing as Telegram is now working but sending images is not supported in PowerShell version 5.