The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::RocketChat

SYNOPSIS

Implements the REST API for Rocket.Chat

USAGE

You can also specify the username, password and server in the environment variables ROCKETCHAT_USERNAME, ROCKETCHAT_PASSWORD and ROCKETCHAT_SERVER.

Most errors die. Use eval generously.

use Net::RocketChat;
use YAML::XS;
use strict;

# specifying connection info directly
my $chat = Net::RocketChat->new(username => $username, password => $password, server => 'https://your.server.here');
# or use the environment
$ENV{ROCKETCHAT_USERNAME} = $username;
$ENV{ROCKETCHAT_PASSWORD} = $password;
$ENV{ROCKETCHAT_SERVER} = $server;

my $chat = Net::RocketChat->new;
eval {
   $chat->login;
   $chat->join(room => "general");
   my $messages = $chat->messages(room => "general");
   print Dump($messages);
   $chat->send(room => "general",message => "your message goes here");
   $chat->send(room => "general",message => "```\nmulti-line\npastes\nare\nok```");
   $chat->leave(room => "general");
};
if ($@) {
   print "caught an error: $@\n";
}

There are also example scripts in the distribution.

ATTRIBUTES

debug

If debug is set, lots of stuff will get dumped to STDERR.

username

If this isn't specified, defaults to $ENV{ROCKETCHAT_USERNAME}

password

If this isn't specified, defaults to $ENV{ROCKETCHAT_PASSWORD}

server

The URL for the server, ie. "https://rocketchat.your.domain.here"

If this isn't specified, defaults to $ENV{ROCKETCHAT_SERVER}

response

Contains the last HTTP response from the server.

METHODS

version

Returns a hashref of versions, currently of the API and server.

"versions": {
   "api": "0.1",
   "rocketchat": "0.5"
}
login

Logs in.

logout

Logs out.

publicRooms

Fetches a list of rooms, and also stores a mapping of names to ids for future use. Returns the raw decoded JSON response from the server:

my $rooms = $chat->publicRooms;

rooms:
- _id: GENERAL
  default: !!perl/scalar:JSON::PP::Boolean 1
  lm: 2016-04-30T16:45:32.876Z
  msgs: 54
  name: general
  t: c
  ts: 2016-04-30T04:29:53.361Z
  usernames:
  - someuser
  - someotheruser
- _id: 8L4QMdEFCYqRH3MNP
  lm: 2016-04-30T21:08:27.760Z
  msgs: 2
  name: dev
  t: c
  ts: 2016-04-30T05:30:59.847Z
  u:
    _id: EBbKeYF9Gvppdhhwr
    username: someuser
  usernames:
  - someuser
has_room(:$room)

Returns 1 if a room exists on the server, 0 otherwise.

if ($chat->has_room("general") {
   $chat->join(room => "general");
   $chat->send(room => "general", message => "Hello, world!");
}
else {
   ...
}
join(:$room,:$room)

Joins a room. Rooms have a human readable name and an id. You can use either, but if the name isn't known it will automatically fetch a list of rooms.

$chat->join(room => "general");
leave(:$id,:$room)

Leaves a room, specified either by name or id.

$chat->leave(room => "general");
messages(:$room,:$id)

Gets all the messages from a room, specified either by name or id.

my $messages = $chat->messages(room => "general");
send(:$room,:$id,:$message)

Sends a message to a room.

$chat->send(room => "general", message => "Hello, world!");

AUTHOR

Dale Evans, <daleevans@github> http://devans.mycanadapayday.com

SEE ALSO

https://rocket.chat/docs/master/developer-guides/rest-api/