Slack to Kodi – Tellybot

As a little hack project at work I decided to get Slack working with Kodi. It’s now easy to display pictures and videos on the big TV that’s on our office wall. I won’t pretend it’s a polished effort but it works and might help someone get something more impressive started.

[Image: nboIA4t.png]

This thread was useful in finding some working Kodi API examples. This was useful on the Slack side.

You don’t write any code in Slack. Instead, Slack needs to point to your PHP script on a webserver that has an SSL certificate. SSL required. I used my 1and1 account to host. The PHP script then talks to the Kodi box which will need to be accessible from the wider internet. Getting Slack talking to the PHP on the webserver, talking to the Kodi box took the majority of the effort.

To get things going, I used the Simple REST Client for Chrome. I was able to get Kodi to display a JPG when the data was in this format:

Quote:URL:
http://192.168.4.168:8080/jsonrpc?request=
Header:
Content-Type: application/json
Data:
{ “jsonrpc”: “2.0”, “method”: “Player.Open”, “params”: { “item”: { “file”: “http://www.underconsideration.com/brandnew/archives/google_2015_logo_detail.png” } }, “id”: 1 }

Kodi’s functionality is great. With the YouTube extension installed I got this to work.

Quote:URL:
http://192.168.4.168:8080/jsonrpc?request=
Header:
Content-Type: application/json
Data:
{ “jsonrpc”: “2.0”, “method”: “Player.Open”, “params”: { “item”: { “file”: “plugin://plugin.video.youtube/?path=/root/video&action=play_video&videoid=KnJaayv6fsI” } }, “id”: 1 }

So from there I made some PHP code so that Slack can talk to the PHP script and then control Kodi. You’ll need to set up the Slack commands in Slack as described here. In the code below, you’ll have to change the IP address of Kodi and the token for Slack. You’ll need to install Youtube on the Kodi box if you want to use that functionality. I was using Kodi Jarvis on a Raspberry Pi 3, LibreELEC build.

I recognise that it’s not the clearest example nor the most elegant of code, but I hope someone finds it useful

PHP Code:
<?php

 # Grab some of the values from the slash command, create vars for post back to Slack
 
$command $_POST['command'];
 
$text $_POST['text'];
 
$token $_POST['token'];

 # Check the token and make sure the request is from our team
 
if($token != 'iMoCAW9Tpj5AFh3aUazMtJN1'){ #replace this with the token from your slash command configuration page
   
$msg "The token for the slash command doesn't match. Check your script.";
   die(
$msg);
   echo 
$msg;
 }

 #using Simple REST Client in Chrome Plugin
 #URL:
 #http://192.168.4.168:8080/jsonrpc?request=
 #header:
 #Content-Type: application/json
 #data:
 #{ "jsonrpc": "2.0", "method": "Player.Open", "params": { "item": { "file": "http://www.underconsideration.com/brandnew/archives/google_2015_logo_detail.png" } }, "id": 1 }

 #$text = 'http://www.underconsideration.com/brandnew/archives/google_2015_logo_detail.png';
 #$text = 'youtube KnJaayv6fsI';

 if (substr($text07) == 'youtube') {
      
$code substr($text, -11);
      
$uniquepart "plugin://plugin.video.youtube/?path=/root/video&action=play_video&videoid=";
      
$uniquepart .= $code;
      
#echo $uniquepart;
 
} elseif (substr($text05) == 'local') {
      
$uniquepart "smb://tellybot2:password@DISKSTATION/Temp/tellybot/";
      
$filename substr($text6);
      
$uniquepart .= $filename;
 } elseif (
substr($text, -4) == '.png') {
      
$uniquepart $text;
      
#echo $uniquepart;
 
} elseif (substr($text, -4) == '.jpg') {
      
$uniquepart $text;
      
#echo $uniquepart;
 
} else {
   
$msg "Usage:\n";
   
$msg .= "To display a jpg or png, just use the URL\n";
   
$msg .= "To play a youtube, use _youtube <ID>_\n";
   
$msg .= "To show a file from \\\\diskstation\\temp\\tellybot use _local <filename>_\n";
   die(
$msg);
   echo 
$msg;
 }

 #this is the URL of the Kodi box. Needs to be available via the internet.
 
$url "http://88.202.102.11:2012/jsonrpc?request=";

 $data_string "{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"Input.Home\"}";

 #first wake it from screensaver
 
$ch curl_init($url);
 
curl_setopt($chCURLOPT_POSTFIELDS$data_string);
 
curl_setopt($chCURLOPT_HTTPHEADER, array(
     
'Content-Type: application/json',
     
'Content-Length: ' strlen($data_string))
 );
 
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
 
$ch_response curl_exec($ch);

 echo "Your wish is my command...";

 #debug
 #echo "\n{ \"jsonrpc\": \"2.0\", \"method\": \"Player.Open\", \"params\": { \"item\": { \"file\": \"";
 #echo $uniquepart;
 #echo "\" } }, \"id\": 1 }";

 sleep(1);

 #now send the command
 
$data_string "{ \"jsonrpc\": \"2.0\", \"method\": \"Player.Open\", \"params\": { \"item\": { \"file\": \"";
 
$data_string .= $uniquepart;
 
$data_string .= "\" } }, \"id\": 1 }";

 $ch curl_init($url);
 
curl_setopt($chCURLOPT_POSTFIELDS$data_string);
 
curl_setopt($chCURLOPT_HTTPHEADER, array(
     
'Content-Type: application/json',
     
'Content-Length: ' strlen($data_string))
 );
 
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
 
$ch_response curl_exec($ch);

 #echo 'Response: ';
 #echo $ch_response;

?>