Initial files
This commit is contained in:
72
koozali-group-message.php
Normal file
72
koozali-group-message.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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
|
||||
}
|
Reference in New Issue
Block a user