Slack to Kodi - Tellybot
#1
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

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;

?>
Reply
#2
Thanks for sharing this, had it bookmarked for ages and finally put it into use today.
Using it as part of my kodi kontroller project for a non-profit office dashboards system, aim to hack it a bit so we can send announcements direct out from slack as well.
Have you put this on GitHub? Would like to fork and attribute credit if possible
Reply
#3
Does this mean I can simply put in commands on slack and the result will be generated on my Kodi Player ? 

Example Like Install "xyz" addon, so it will automatically do so ?
Reply
#4
If this is something you meant than I want to really look into it and would be worth it.
Reply
#5
It won't install addons but it can play from them if you add the code to handle it.
It does YouTube and local files out of the box.
Reply
#6
This is great and really useful, thanks Smile
Reply
#7
If anyone is editing the php file with more functions please do share back. I'll add my additions as soon as its ready for prime time.

Also just FYI, coding practices for slack commands have moved on. Code still works but an update would make responses work better for example.
Reply
#8
Cool little project. +1 for getting it up on GitHub so we can contribute new commands.
Reply

Logout Mark Read Team Forum Stats Members Help
Slack to Kodi - Tellybot0