Initial files

This commit is contained in:
John Crisp
2025-09-02 14:27:20 +02:00
parent bb8fedb899
commit ffef244d17
9 changed files with 1996 additions and 1 deletions

3
.env Normal file
View File

@@ -0,0 +1,3 @@
SERVER_HOST="https://chat.server.com"
SERVER_USER="user"
SERVER_PASS="pass"

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
composer.phar
/vendor/
*.code-workspace
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

View File

@@ -1,4 +1,16 @@
# rocket-chat-php-scripts
Some PHP scripts to manage Rocket via the API using this Class
https://github.com/alekseykuleshov/rocket-chat#login
https://github.com/alekseykuleshov/rocket-chat#login
To install:
git clone https://src.koozali.org/jcrisp/rocket-chat-php-scripts.git
cd rocket-chat-php-scripts
composer install
Set your .env vars
php -f somefile.php

6
composer.json Normal file
View File

@@ -0,0 +1,6 @@
{
"require": {
"atdev/rocket-chat": "~1.0",
"vlucas/phpdotenv": "^5.6"
}
}

1666
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,73 @@
<?php
// https://github.com/alekseykuleshov/rocket-chat
/**
* Send a message to a public channel
*/
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 = "OK - upgraded to 7.9.3 after a few wriggles with docker";
// All Rooms are converted to hyphenated
$channelID = "smestuff";
$channelID = strtolower($channelID);
$myUserId = $result->getUserId();
$listing = \ATDev\RocketChat\Channels\Channel::listing();
if (!$listing) {
// Log the error
$error = \ATDev\RocketChat\Channels\Channel::getError();
// Bail and logout
} else {
foreach ($listing as $channel) {
$mychannel = strtolower($channel->getName());
//echo $channel->getName() . "<br>";
//$myRoomId = $channel->getRoomId();
echo "my channel $mychannel - ChannelId $channelID \n";
if ($mychannel == $channelID) {
// Send a message
$myChannelId = $channel->getChannelId();
echo "ID = $myChannelId\n";
$message = new \ATDev\RocketChat\Messages\Message();
// Always a Message->RoomID - use either group or channelID from above
$message->setRoomId($myChannelId);
$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
}

View File

@@ -0,0 +1,93 @@
<?php
// https://github.com/alekseykuleshov/rocket-chat
/**
* DeActivate old users
*
* Add protected users to array
*
*/
$protectedUsers = array("rocket.cat", "dani", "botpress-connector.bot", "jitsi.bot", "admin", "john");
$bestAfterDate = "2023";
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 {
$userObj = new \ATDev\RocketChat\Users\User;
$listing = $userObj->listing(0, 150);
// Will retrieve use list inc last login
if (!$listing) {
// Log the error
$error = \ATDev\RocketChat\Users\User::getError();
} else {
//var_dump($listing);
$x = 1;
$userArr = array();
foreach ($listing as $user) {
$userId = $user->getUserName($user);
$lastLogin = $user->getLastLogin($user);
$userArr[$userId] = ($lastLogin);
}
unset($userObj);
//asort($userArr);
$protectedUsers = array("rocket.cat", "dani", "botpress-connector.bot", "jitsi.bot", "admin", "john");
$bestAfterDate = "2023";
foreach ($userArr as $ID => $date) {
// If $ID not in array
if (! in_array($ID, $protectedUsers)) {
$dateTimeInstance = new DateTime($date);
$formattedFromObject = $dateTimeInstance->format('Y');
if ($formattedFromObject >= $bestAfterDate) {
echo "Keep Key=" . $ID . ", Value=" . $formattedFromObject;
echo "\n";
} else {
$user = new \ATDev\RocketChat\Users\User($ID);
$active = $user->getActive();
echo "Delete Key=" . $ID . ", Value=" . $formattedFromObject;
echo "\n";
}
} else {
echo "Protected User=" . $ID;
echo "\n";
}
}
}
$result = \ATDev\RocketChat\Chat::logout();
}

72
koozali-group-message.php Normal file
View 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
}

69
koozali-sample-login.php Normal file
View File

@@ -0,0 +1,69 @@
<?php
/**
* Sample login
*/
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 = "Some message to @all users";
// All Rooms are converted to hyphenated
$groupID = "IT-Stuff";
$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
}