Files
rocket-chat-php-scripts/koozali-group-message.php
2025-09-02 14:27:20 +02:00

73 lines
1.9 KiB
PHP

<?php
// https://github.com/alekseykuleshov/rocket-chat
/**
* Send a message to a private group
*/
use Dotenv\Dotenv;
require 'vendor/autoload.php';
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required(['SERVER_HOST', 'SERVER_USER', 'SERVER_PASS']);
$server = $_SERVER['SERVER_HOST'];
$user = $_SERVER['SERVER_USER'];
$pass = $_SERVER['SERVER_PASS'];
// Firstly, init
\ATDev\RocketChat\Chat::setUrl($server); // No trailing / on the https://host.domain
// Now, login
$result = \ATDev\RocketChat\Chat::login($user, $pass);
if (!$result) {
// Log the error
$error = \ATDev\RocketChat\Chat::getError();
// And out
} else {
// Do something
$myText = "Hello @all - need to upgrade this instance. Hopefully back shortly";
// All Rooms are converted to hyphenated
$groupID = "systemadmin";
$groupID = strtolower($groupID);
$myUserId = $result->getUserId();
$listing = \ATDev\RocketChat\Groups\Group::listing();
if (!$listing) {
// Log the error
$error = \ATDev\RocketChat\Groups\Group::getError();
// Bail and logout
} else {
foreach ($listing as $group) {
$myGroup = strtolower($group->getName());
// echo $group->getName() . "<br>";
//$myRoomId = $group->getRoomId();
if ($myGroup == $groupID) {
// Send a message
$myRoomId = $group->getRoomId();
$message = new \ATDev\RocketChat\Messages\Message();
$message->setRoomId($myRoomId);
$message->setText($myText);
$messageresult = $message->postMessage();
if (!$messageresult) {
// Log the error
$error = $message->getError();
}
}
}
$result = \ATDev\RocketChat\Chat::logout();
}
// Never logged in or has logged out
}