This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
projects:matrix_owncast [2021/04/07 20:00] admin created |
projects:matrix_owncast [2021/04/07 20:18] (current) admin |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Matrix/Owncast ====== | ====== Matrix/Owncast ====== | ||
| - | I run this code to link an Owncast chat to a Matrix room. It's designed to be temporary (you just run it from the command line) but it would be easy to modify to 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. | + | 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. | ||
| <code php OwncastToMatrix.php> | <code php OwncastToMatrix.php> | ||
| Line 22: | Line 28: | ||
| if($line['type'] == "CHAT") { | if($line['type'] == "CHAT") { | ||
| if(!array_key_exists($line['id'], $tracker)) { | if(!array_key_exists($line['id'], $tracker)) { | ||
| - | $str = "<Owncast: ".$line['author']."> ".htmlspecialchars_decode(strip_tags($line['body'])); | + | $str = "<Owncast: ".$line['author']."> ".htmlspecialchars_decode(strip_tags($line['body']), ENT_QUOTES); |
| $tracker[$line['id']] = strip_tags($line['body']); | $tracker[$line['id']] = strip_tags($line['body']); | ||
| if(!empty($str)) { | if(!empty($str)) { | ||
| Line 54: | Line 60: | ||
| $room_id = "!room_id:homeserver"; | $room_id = "!room_id:homeserver"; | ||
| + | |||
| + | $sender_name = "@botname:homeserver"; // IMPORTANT: Set this to the name of the user associated with the access token, this stops it from getting stuck in a loop (and repeating what is said in both rooms forever). | ||
| while(true) { | while(true) { | ||
| Line 60: | Line 68: | ||
| if(is_array($data['chunk']) and sizeof($data['chunk'])>0) { | if(is_array($data['chunk']) and sizeof($data['chunk'])>0) { | ||
| foreach($data['chunk'] as $message) { | foreach($data['chunk'] as $message) { | ||
| - | if($message['sender'] != '@adelaide:glasgow.social') { | + | if($message['sender'] != $sender_name) { |
| if($message['content']['msgtype'] == "m.text") { | if($message['content']['msgtype'] == "m.text") { | ||
| if(!empty($message['content']['body'])) { | if(!empty($message['content']['body'])) { | ||