====== Matrix/Owncast ======
I run this code to link an [[https://owncast.online/|Owncast]] chat to a [[https://matrix.org/|Matrix]] room.
It's designed to be temporary (you just run it from the command line) but it would be easy to modify to run as a cronjob and always be searching for when a stream starts.
I've seperated out the Owncast-to-Matrix and Matrix-to-Owncast components so you can choose which one you want (or run both).
OwncastToMatrix uses my very simple [[Projects/Matrix PHP Class]] but MatrixToOwncast doesn't require it. It should work as is, but might best serve as inspiration in your own language of choice.
".htmlspecialchars_decode(strip_tags($line['body']), ENT_QUOTES);
$tracker[$line['id']] = strip_tags($line['body']);
if(!empty($str)) {
$mat = new matrix();
$room = "#geek";
$mat->post($str, $room);
}
}
}
}
file_put_contents($tracker_file, json_encode($tracker));
sleep(20);
}
0) {
foreach($data['chunk'] as $message) {
if($message['sender'] != $sender_name) {
if($message['content']['msgtype'] == "m.text") {
if(!empty($message['content']['body'])) {
$nick_parts = explode(":", $message['sender']);
$relay = "[".substr($nick_parts[0],1)."] ".$message['content']['body'];
echo $relay."\n";
send_owncast_system_message($relay);
}
}
}
}
}
sleep(10);
}
function send_owncast_system_message($str) {
if(empty($str))
return false;
$url = "http://owncast_server.com";
$access_token = "YOUR_OWNCAST_ACCESS_TOKEN_HERE"; // you may have to escape hyphens with a backslash
$endpoint = "/api/integrations/chat/system";
$post = array("body" => $str);
$auth = "Authorization: Bearer $access_token";
$ch = curl_init($url.$endpoint);
$headers = array("Content-Type: application/json",
$auth);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($post));
$result = curl_exec($ch);
curl_close($ch);
}
Then just run them on the command line with something like:
php OwncastToMatrix.php &
php MatrixToOwncast.php