Skip to content

Instantly share code, notes, and snippets.

@webbson
Last active June 14, 2020 12:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webbson/af15741f027b1b59f0c755fa0f9a8169 to your computer and use it in GitHub Desktop.
Save webbson/af15741f027b1b59f0c755fa0f9a8169 to your computer and use it in GitHub Desktop.
Telegram bot for Sonarr/Radarr
<?php
function processMessage($message) {
$groupchat = '-XXXX'; // Group chat id
// process incoming message
$message_id = $message["message_id"];
$chat_id = $message["chat"]["id"];
$response = array("chat_id" => $chat_id);
$plextoken = "YYYY"; // Plex token
$plexbaseurl = "https://ASFASFASF.plex.direct:32400"; // PLEX DIRECT URL
$eol = PHP_EOL;
if (isset($message["text"])) {
$message["text"] = str_replace("@BOT_NAME", "", $message["text"]);
// incoming text message
$args = explode(" ", $message["text"]);
$result = [];
preg_match("/imdb.com.*\/(tt\d+)\//i", $message['text'], $result);
if (count($result) == 2) {
$args[0] = "/addmovie";
$args[1] = $result[1];
$response["disable_web_page_preview"] = true;
}
switch($args[0]) {
case "/addmovie":
if (!isAllowed($chat_id, $groupchat))
exit;
if ($args[1] == undefined || strlen($args[1]) == 0)
$response["text"] = "You must send me an IMDb id";
else {
$movies = callRadarr("movie");
$continue = true;
foreach ($movies as $movie)
{
if ($movie->imdbId == $args[1]){
$ml = movieLink($movie);
if ($movie->downloaded)
$response["text"] = "I've already downloaded $ml!";
else
$response["text"] = "$ml is already on my wanted list!";
$continue = false;
break;
}
}
if ($continue) {
$movie = callRadarr("movies/lookup", "term=imdb:".$args[1]);
if(count($movie) == 0){
$response["text"] = "Couldn't find a movie with that ID";
} else {
$movie = $movie[0];
$movie->profileId = "4";
$movie->monitored = true;
$movie->episodeFileCount = 0;
$movie->episodeCount = 0;
$movie->isExisting = false;
$movie->rootFolderPath = "E:\\Movies\\";
$movie->addOptions = [
"ignoreEpisodesWithFiles" => false,
"ignoreEpisodesWithoutFiles" => false,
"searchForMovie" => true,
];
$movie->imdbId = $args[1];
$res = callRadarr("movie", $movie);
$ml = movieLink($movie);
if ($res->monitored)
$response["text"] = "Added movie: $ml to my wanted list.";
else
$response["text"] = "Something went wrong, I'm so sorry...";
}
}
}
break;
case "/history":
$history = callRadarr("history", "pageSize=30")->records;
$response["text"] = "*Recently downloaded movies*:$eol";
$response["disable_web_page_preview"] = true;
$count = 1;
foreach ($history as $rec){
if ($rec->eventType == "downloadFolderImported") {
$date = (object)date_parse($rec->date);
$ml = movieLink($rec->movie);
$response["text"] .= $count++.": $ml ($date->day/$date->month $date->hour:$date->minute)$eol";
}
if ($count == 11) break;
}
break;
case "/disk":
$res = callRadarr("DiskSpace");
foreach ($res as $disk)
{
$percFree = round(($disk->freeSpace/$disk->totalSpace) * 100,1);
$free = formatSizeUnits($disk->freeSpace);
$total = formatSizeUnits($disk->totalSpace);
$response["text"] .= "*$disk->path* ($disk->label) *$percFree%* free. ($free / $total)$eol";
}
break;
case "/help":
$response["text"] = "*Welcome to Download bot*$eolMy commands are:$eol";
$response["text"] .= "/disk - Show disk usage$eol";
$response["text"] .= "/history - Show latest downloaded movies$eol";
if ($chat_id == $groupchat) {
$response["text"] .= "/addmovie *{IMDb id}* - Add a movie to download, send the IMDb id in the *tt2381991* format$eol";
}
break;
case "/stop":
$response["text"] = "Nah I don't feel like it.";
$response["reply_to_message_id"] = $message_id;
break;
default:
exit();
break;
}
} else {
exit();
}
sendMessage($response);
}
function sendMessage($postData) {
$postData['parse_mode'] = "Markdown";
$url = "https://api.telegram.org";
$botid = "BOTID"; // TELEGRAM Bot ID
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"$url/$botid/sendMessage");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output = curl_exec($ch);
curl_close($ch);
}
function callRadarr ($method, $m2 = ""){
$ch = curl_init();
$url = "http://127.0.0.1:7878/api/$method?apikey=YYYY"; // Radarr API key
if (gettype($m2) == "string" && strlen($m2) > 0)
$url .= "&$m2";
else if (gettype($m2) == "object"){
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($m2));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen(json_encode($m2)))
);
}
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
$output = curl_exec($ch);
return json_decode($output);
curl_close($ch);
}
function formatSizeUnits($bytes)
{
if ($bytes >= 1073741824)
$bytes = number_format($bytes / 1073741824, 2)." GB";
elseif ($bytes >= 1048576)
$bytes = number_format($bytes / 1048576, 2)." MB";
elseif ($bytes >= 1024)
$bytes = number_format($bytes / 1024, 2)." KB";
elseif ($bytes > 1)
$bytes = "$bytes bytes";
elseif ($bytes == 1)
$bytes = "$bytes byte";
else
$bytes = "0 bytes";
return $bytes;
}
function isAllowed($target, $allowed) {
$me = 'ASDJKHFSA'; // MY Private telegram id should always be allowed
if ($target == $me) return true;
if ($target != $allowed) {
sendMessage(array("chat_id" => $target, "text" => "I am *not* allowed to do that *here*."));
return false;
}
return true;
}
function movieLink($movie) {
return "[$movie->title](https://www.imdb.com/title/$movie->imdbId/)";
}
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if (!$update) {
exit;
}
if (isset($update["message"])) {
processMessage($update["message"]);
}
@pefman
Copy link

pefman commented Feb 27, 2017

Looks nice, but im new to telegram. I have created the bot but how do i incoorperate this?

@matteomac
Copy link

im interested too, how can I implement this?

@schumi2004
Copy link

Seems to be only for Radarr and not both Sonarr and Radarr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment