Pushing into the Rocket

Pushing into the Rocket
Photo by Felipe Simo / Unsplash

A Rocket Chat instance, the same 60 users already pushed into a PeerTube instance, and poor will to understand the too much complex native feature to import by CSV.

Let's write another script (very similar to the PeerTube's one...):

<?php

require_once('vendor/autoload.php');
use GuzzleHttp\Client;

$endpoint = 'https://my.rocketchat.server/api/v1/';
$username = 'MY_USERNAME';
$password = 'MY_PASSWORD';

$client = new Client();

$response = $client->request('POST', $endpoint . 'login', [
    'json' => [
        'user' => $username,
        'password' => $password,
    ],
    'headers' => [
        'Content-type' => 'application/json',
    ]
]);
$response = json_decode($response->getBody());

$f = fopen('users.csv', 'r');

while($row = fgetcsv($f)) {
    try {
        $client->request('POST', $endpoint . 'users.create', [
            'json' => [
                /*
                    Take care in sorting the CSV columns as required (or change the indexes here).
                */
                'name' => trim($row[0]),
                'username' => trim($row[1]),
                'password' => trim($row[2]),
                'email' => trim($row[3]),
                'roles' => ['user'],
            ],
            'headers' => [
                'X-Auth-Token' => $response->data->authToken,
                'X-User-Id' => $response->data->userId,
                'Content-type' => 'application/json',
            ]
        ]);
    }
    catch(\Exception $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
}

Same instructions as before: to execute put both this script (with, on top, your own server URL, and valid credentials for an admin user) and your CSV file into a folder and execute:

composer require guzzlehttp/guzzle:^7.0
php rocket.php