Send play/pause/stop events to my app

I'm the creator of a little home automation software package. I'd like to integrate my Plex theatre with my HA software so that I can activate certain scenes when something happens within my Plex software.

 

For example, if I'm watching Plex on my smart TV and play a movie, I'd like to be able to send an event to my HA software so that I can dim the lights. Likewise, if I hit stop, I'd like the lights to come back on. 

 

Is there an existing "WebHook" feature already inside Plex that I can configure to do http calls to a pre-defined address when these events happen? If not, where would I start for creating my own Plex plugin -- what type of plugin, etc.?

 

Thanks for any tips.

plex does support the xbmc json-rpc api.

see here: https://forums.plexapp.com/index.php/topic/54867-http-apicontrol/page-2#entry336362

Thanks... is there something I need to do to access that page? It just tells me I don't have access to it.

its plexpass only. but as it is a post from me i just repeat it here:



as far as i know there should be two ways to use the json api. one is http. this is plex port 3005. the other one should be json over tcp on port 9090.
as far as http://wiki.xbmc.org/index.php?title=JSON-RPC_API#Transports none is deprecated.

the tcp version allows bidirectional communication and is what i'm looking for.

edit: i think i found the problem. plex is listening for port 9090 only for localhost. how can i enable it for other interfaces?


ok... found it myself

true

We have official webhook support now!

I can trigger the webhook in my homeautomation software with play, pause , stop but i can´t get any payload. I thought JSON is send but the POST array is empty. What am I doing wrong?

@elan said:
We have official webhook support now!

Any chance of getting hook events on new library additions? Would love to set up my Slackbot to alert users of new movies.

@Aggadur said:
I can trigger the webhook in my homeautomation software with play, pause , stop but i can´t get any payload. I thought JSON is send but the POST array is empty. What am I doing wrong?

Make sure you’re processing a multipart MIME message.

@rssteffey said:
Any chance of getting hook events on new library additions? Would love to set up my Slackbot to alert users of new movies.

Thanks for the request, we’ve heard this one a few times and are trying to figure out the best way to do it. If you have any concrete suggestions as to what the payload would look like, I’m all ears (basically: what if you add 1000 movies? what about a first scan? etc.)

@elan said:
Make sure you’re processing a multipart MIME message.

I´m trying to get the payload with

PHP

$plexjson = file_get_contents('php://input');
$data = json_decode($plexjson);

Is this wrong? What should I do instead?

If I test the webhook with a script

 $connect = "https://myserverurl";
$plexhook = "hook/plex";


$data = '{  
   "event": "media.play",
   "user": true,
   "owner": true,
   "Account": {
      "id": 1,
      "thumb": "https://plex.tv/users/1022b120ffbaa/avatar?c=1465525047",
      "title": "elan"
   },
   "Server": {
      "title": "Office",
      "uuid": "54664a3d8acc39983675640ec9ce00b70af9cc36"
   },
   "Player": {
      "local": true,
      "publicAddress": "200.200.200.200",
      "title": "Plex Web (Safari)",
      "uuid": "r6yfkdnfggbh2bdnvkffwbms"
   },
   "Metadata": {
      "librarySectionType": "artist",
      "ratingKey": "1936545",
      "key": "/library/metadata/1936545",
      "parentRatingKey": "1936544",
      "grandparentRatingKey": "1936543",
      "guid": "com.plexapp.agents.plexmusic://gracenote/track/7572499-91016293BE6BF7F1AB2F848F736E74E5/7572500-3CBAE310D4F3E66C285E104A1458B272?lang=en",
      "librarySectionID": 1224,
      "type": "track",
      "title": "Love The One You\'re With",
      "grandparentKey": "/library/metadata/1936543",
      "parentKey": "/library/metadata/1936544",
      "grandparentTitle": "Stephen Stills",
      "parentTitle": "Stephen Stills",
      "summary": "",
      "index": 1,
      "parentIndex": 1,
      "ratingCount": 6794,
      "thumb": "/library/metadata/1936544/thumb/1432897518",
      "art": "/library/metadata/1936543/art/1485951497",
      "parentThumb": "/library/metadata/1936544/thumb/1432897518",
      "grandparentThumb": "/library/metadata/1936543/thumb/1485951497",
      "grandparentArt": "/library/metadata/1936543/art/1485951497",
      "addedAt": 1000396126,
      "updatedAt": 1432897518
   }';

		//var_dump($data);
	

$result = SendTriggerPlex($connect, $plexhook, $data);
var_dump($result);


function SendTriggerPlex($connect, $plexhook, $data)
	{
		
		$plexuser = "plexwebhookuser";
		$plexpassword = "plexwebhookpassword";
		$URL = $connect."/".$plexhook;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$URL);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout after 5 seconds
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		curl_setopt($ch, CURLOPT_USERPWD, "$plexuser:$plexpassword");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($data))
		);
		$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
		$result=curl_exec ($ch);
		curl_close ($ch);
		return $result;
	}

I get the payload via the webhook.

I found the reason for the problem

$plexjson = $_POST["payload"]; 
$data = json_decode($plexjson); 

works.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.