Notification to telegram IM

hello world,

i am quite unaware of coding but i would like to replace pushbullet and get notification on my telegram IM on grab or download of new files.

could anyone be so kind and give me some instruction?

thanks in advance,
cP

1 Like

I have a script for bash for telegram. Don’t know what system are you using.

using osx, so might work, right?

cheers
cp

Should be ok, this is using telegram bot

#!/bin/sh
### get chatid curl "https://api.telegram.org/bot[BOTTOKEN]/getUpdates?offset=1"
TOKEN='THETOKEN'
URL='https://api.telegram.org/bot'$TOKEN
MSG_URL=$URL'/sendMessage?chat_id='
TG_CHAT_ID="NUMBERID"

size=$(stat -c "%s" "$sonarr_episodefile_path")
sizeMb=$( expr $size / 1048576 )

send_message () {
        curl --data-urlencode "text=$2" --data-urlencode "parse_mode=Markdown" "$MSG_URL$1&"
}

MSG=$(printf "*Episode Imported* \n*Source:* $sonarr_episodefile_scenename | $sizeMb MB \n*Destination:* $sonarr_episodefile_path ")

send_message "$TG_CHAT_ID" "$MSG"

You need to place your bot token and chat_id in the script. To get the chat_id you need to send a message to your bot and execute curl as pointed in the beginning of the script or place the url in the browser, don’t place the square brackets.

Also don’t forget to set the script to executable.
You select in sonarr->connect->custom scripts, select the path where you put the script, and type the name of the script in the field after the slash. There is not grab, just download, upgrade or rename.

The format of the message is the following

Episode Imported Source: Criminal.Minds.S11E11.HDTV.x264-KILLERS | 221 MB Destination: /tv/Criminal Minds/Season 11/Criminal Minds - S11E11 - SDTV - KILLERS.mp4

great!!! works. except that the size is not shown.

one more question:

the tg_chat_id is it the one from my personell account or the one from the bot?

cheers

Is the chat_id, is the chat interaction in between your account and the bot. Send a message to the bot and put the url getupdates in the browser, you’ll get a json response. Look for the chat id

I’m trying to get this to work on ubuntu but having issues. I’ve put the chat_id and token information into the script, set permission to 0755 and in sonarr it’s linked to /scripts/telegram.sh. When I click test notification it says completed successfully but I never receive them and I can’t see any reference to this in the logs to try troubleshooting.
Any ideas?

Well, run the script manually, replace $MSG at the end with some random text in between like “test message”

then run /scripts/./telegram.sh in terminal

If you get the text in telegram means is working. You’ll need run debug/trace logs to see what sonarr is doing when the import moment comes.

custom scripts doesn’t support on grab, so you’ll only get messages when series are imported.

By the way i changed a little bit the script to make it more portable in pure shell /bin/sh

When I run the script in my shell I get info on Telegram, but than
when I add it to the custom script part in Sonarr with

path
/home/to/telegram/notification/go.sh
arguments: none

The test is ok, but I do not see that sonarr is actually testing the script
Traced the log, but Do not see that it is actually starting the shell script.
manually downloaded a show to see if it is functioning, but no info in sonarr …

I think I am doing something wrong in Sonarr …

Sonarr just tests the file exists. Doesn’t execute the script. You need to enable debug/trace logs to see whats going on when the import happens.

Probably Sonarr needs to know that after you added the custom script and saved it, the whole ‘connect’ part needs to be saved as well. Suddenly 2x I was notified by Telegram that my manually started download was finished (yeah) the sh script and php script (a modified sonar example script for sickbeard) (calling the sh script, duh) … :wink:
Cause now the log (traced it) is telling me info

The only thing I modified was the echo -e
the -e was being echoed as message to Telegram as well, so I removed the -e (dunno the meaning for it in my shell = (/bin/bash)

Echo -e is to evaluate breaking lines \n

So the message is receive in the example I posted at the beginning.

Strange I’ll test it again. Did you get the size?

This is what I get (with the -e removed)

Episode Imported
Source: The.
100.S03E02.Wanheda.Part.Two.720p.WEB-DL.DD5.1.H264-DRACULA
| 1405 MB
Destination:/media/usenetTV/The100/Season 3/The 100 - S03E02 -
WEBDL-720p DRACULA.mkv

so yes with size

Ok, i had a typo i believe in the chatid variable i changed that, also i changed echo for printf.

The script should run now in pure shell, without bash.

it runs :wink:
I slightly altered it
create an ENV for the chatID, as the TOKEN
cause it’s little unclear that after the = you need to type the chatid …
ok, after messing around for 30 minutes it will be clear that you need to do that, but if you declare it as an environment variable it’s more clear

#!/bin/sh
### get chatid curl "https://api.telegram.org/bot[BOTTOKEN]/getUpdates?offset=1"
TOKEN='THETOKEN'
CHATID='THECHATID'
URL='https://api.telegram.org/bot'$TOKEN
MSG_URL=$URL'/sendMessage?chat_id='$CHATID
TG_CHAT_ID="NUMBERID"

size=$(stat -c "%s" "$sonarr_episodefile_path")
sizeMb=$( expr $size / 1048576 )

send_message () {
        res=$(curl --data-urlencode "text=$2" --data-urlencode "parse_mode=Markdown" "$MSG_URL$1&")
}

MSG=$(printf "*Episode Imported* \n*Source:* $sonarr_episodefile_scenename | $sizeMb MB \n*Destination:* $sonarr_episodefile_path ")

send_message "$TG_CHAT_ID" "$MSG"

all worked flawlessly this night.

The telegram TG_CHAT_ID gets appended in the send_message function as the first positional argument $1

curl --data-urlencode "text=$2" --data-urlencode "parse_mode=Markdown" "$MSG_URL$1&"

Your double appending the chatID in curl.

The res=$(curl) that i have in there is also unnecessary so i’ll take it out also

Wierd…
I think I am missing something

MSG_URL=$URL’/sendMessage?chat_id=67xxxxx’
TG_CHAT_ID=“NUMBERID”

is giving me with the test following output

./grab.sh
stat: cannot stat ‘’: No such file or directory
expr: syntax error
{“ok”:true,“result”:{“message_id”:489,“from”:{“id”:xxxx,“first_name”:“XXX_Sonarr”,“username”:“XXX_Sonarrbot”},“chat”:{“id”:67xxxxxx,“first_name”:“John Doe”,“username”:“John Doe”,“type”:“private”},“date”:1454333817,“text”:“Episode Imported \nSource: | MB \nDestination:”}}user@media:/home/dennis/Telegram-Grab#

Cannot stat end syntax error is no issue do to executing in shell without sonarr
And Telegram responds

Than try
TOKEN='143xxxxxM0ZdC_K3TkKoWg’
URL=‘https://api.telegram.org/bot’$TOKEN
MSG_URL=$URL’/sendMessage?chat_id='
TG_CHAT_ID=“67334809”

Dunno if this is correct (not, cause … it gives me)
{“ok”:false,“error_code”:400,“description”:"[Error]: Bad Request: chat_id is empty"}user@media:/home/dennis/Telegram-Grab# nano -w grab.sh

Thats why I created a ENV for the chatID :wink:

(ps I am a nono on programming and bashing :wink: but the only remaining question is where the hell do we put the chatid if my examples are incorrect (but do work) :wink:

hi there,

the only thing that does not work on my OSX is displaying the filesize.
any hint how to manage this on OSX?

regards,
CP