Webcam image slash command mattermost

Last Updated or created 2025-06-15

I’ve got my own chat server, because WhatsApp sucks.

With this I can play around.

Below is a script to capture an image from a (Reolink) webcam, and show this in a Mattermost channel.

You need to configure your /slash command in Mattermost and a webserver with PHP

When entering
/labcam
in a channel, an image will be shown.

Code:

<?php

// See token from screenshots above
$expected_token = 'YOUR_MATTERMOST_TOKEN';
$token = $_POST['token'] ?? '';

if ($token !== $expected_token) {
    http_response_code(403);
    echo 'Invalid token, go away';
    exit;
}


// Reolink camera settings
$ip = '192.168.1.2'; // Replace with your camera IP
$user = 'admin';       // Camera username
$pass = 'admin';// Camera password
$rs = uniqid();        // Unique request string

$url = "http://$ip/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=$rs&user=$user&password=$pass";

// Temporary image save path (ensure this directory is public and writable)
$image_filename = 'snapshot_' . time() . '.jpg';
$image_path = __DIR__ . '/snapshots/' . $image_filename;  // e.g., public_html/snapshots/
$image_url = 'https://labcam.henriaanstoot.nl/snapshots/' . $image_filename; // Public URL

// Fetch image from Reolink using cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
$image_data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($http_code !== 200 || !$image_data) {
    echo json_encode([
        'response_type' => 'ephemeral',
        'text' => 'Failed to get snapshot from Reolink camera.',
    ]);
    exit;
}

// Save image
file_put_contents($image_path, $image_data);

// Respond to Mattermost
$response = [
    'response_type' => 'in_channel',
    'text' => 'Live snapshot from camera:',
    'attachments' => [[
        'image_url' => $image_url,
        'fallback' => 'Reolink snapshot'
    ]]
];

header('Content-Type: application/json');
echo json_encode($response);
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *