How do I clean messages out of a rocketchat room or channel?

Posted Leave a comment

Trimming RocketChat

This functionality was added to newer versions of rocketchat, this same way WILL work, but it’s easier to do it in the room interface.

We switched from a paid slack account last year, to rocketchat.

I’ve written bots in the past to alert coworkers of system issues in a more public place, but some of my rooms were getting upwards of 10,000 messages and the browser and phones would be extremely slow pulling the room up as message counts were growing.

Sending messages to a chat in a controlled manor is a great way to alert a lot of people to issues, or log something. This should make it super easy to clean the rooms out when they grow larger than you thought they would!

python to the rescue!

My client can do the following:

  1. test connection to server (–whoami)
  2. send a message to a user. (–send –user justin –text “hi there!”)
  3. send a message to a group. (–send –group groupname –text “Hi there!”)
  4. send a message to a channel. (–send –channel channelname –text “Hi there!”
  5. delete all messages in a group. (–delete –group roomname –iknow)
  6. delete all messages in a channel. (–delete –channel roomname –iknow)
  7. install saved rocketchat credentials. (–install –rcuser user –rcpass pass –rchost hostname)
  8. spam a group with test messages (–trash –iknow –group groupname)
  9. spam a channel with test messages (–trash –iknow –channel channelname)

What are these groups and channels?

On rocketchat it gets a little confusing sometimes. If you have a room, where everyone is allowed to view and post, it’s considered a “channel”.

If you have a private channel, it’s considered a “group”.

Cosmicray-rocketchat

I found a newer api, as my last client was using rocket-python, which didnt seem to allow me to delete messages in a somewhat controlled manor.

The api can be installed like so

[code]
python -m pip install cosmicray-rocketchat
[/code]

to use the software paste the following in a file, name it rc-client.py

you might want to install python3:

it’s in the repo for centos, and fedora, and pretty much any operating system, since I use rpm based distros, these instructions are for CentOS.

[code]
sudo yum install python3 pip3
[/code]

[code]
#!/usr/bin/python3

import rocketchat
import argparse
import datetime

parser = argparse.ArgumentParser()
parser.add_argument(‘–rcuser’, help=’The username used with rocketchat, Only needs to be set once.’)
parser.add_argument(‘–rcpass’, help=’The password used with rocketchat. Only needs to be set once.’)
parser.add_argument(‘–rchost’, help=’Chat host to use for API access’)
parser.add_argument(‘–install’, help=’create config file on this machine for access, pass –rcuser (user@domain.com) –rcpass (rcpassword) and –rchost http://chat.domain.com’,action=’store_true’)
parser.add_argument(‘–channel’, help=’channel to send to/operate on’)
parser.add_argument(‘–group’, help=’group to send to/operate on’)
parser.add_argument(‘–user’, help=’user to send to/operate on’)
parser.add_argument(‘–whoami’, help=’test of connection to rocketchat api’, action=’store_true’)
parser.add_argument(‘–delete’, help=’delete messages from group or channel’, action=’store_true’)
parser.add_argument(‘–verbose’, help=’enable more logging to console’)
parser.add_argument(‘–send’, help=’text to send to room, user, or channel’, action=’store_true’)
parser.add_argument(‘–iknow’, help=’required for deletion of messages.’, action=’store_true’)
parser.add_argument(‘–text’, help=’text to send to room, user, or channel’)
parser.add_argument(‘–trash’,help=’user with –iknow to spam a room with messages, mainly used for testing’, action=’store_true’)
args = parser.parse_args()

#store variables
whoami = args.whoami
text = args.text
user = args.user
group = args.group
channel = args.channel
sendmsg = args.send
install = args.install
rcuser = args.rcuser
rcpass = args.rcpass
rchost = args.rchost
verbose = args.verbose
iknow = args.iknow
delete = args.delete
now = datetime.datetime.now()
trash = args.trash

#first setup, creates local file with config data defaults.
if install == True:
if rcuser != None:
if rcpass != None:
if rchost != None:
rocketchat.create_creds_file(rcuser,rcpass)
rocketchat.configure(domain=rchost)
if verbose == True:
print(“storing config”)
rocketchat.store_config()

#prints basic info about logged in bot user, used to test config
if whoami == True:
print(rocketchat.models.User.me)

#send message code
if sendmsg == True:
if channel != None:
if text != None:
myroom = rocketchat.channels()[channel]
myroom.send(text)
if user != None:
if text != None:
users = rocketchat.users()
foo = users[user]
foo.send(text)
#users[user].send(text)
if group != None:
if text != None:
mygroup = rocketchat.groups()[group]
mygroup.send(text)

#channel cleaning
if iknow == True:
if delete == True:
if channel != None:
while True:
try:
myroom = rocketchat.channels()[channel]
message = myroom.messages.last[0]
message.delete()
except:
myroom.send(“Room cleaned: ” + str(now))
break

    if group != None:
        while True:
            try:
                myroom = rocketchat.groups()[group]
                message = myroom.messages.last[0]
                message.delete()
            except:
                myroom.send("Room cleaned: " + str(now) )
                break

#room trashers for testing deletes
if trash == True:
if channel != None:
if iknow == True:
count = 0
mychannel = rocketchat.channels()[channel]
while True:
print(count)
mychannel.send(‘testmessage ‘ + str(count))
count += 1
if count == 3000:
mychannel.send(‘room successfully trashed’)
break

if group != None:
    if iknow == True:
        count = 0
        mygroup = rocketchat.groups()[group]
        while True:
            print(count)
            mygroup.send('*testmessage* ' + str(count))
            count += 1
            if count == 3000:
                mygroup.send('room successfully trashed')
                if verbose == True:
                    print('room trashed successfully!')
                break                    

[/code]

once saved, use the following examples:

first install example:

[code]
./rc-client.py –install –rcuser rocketchatuser –rcpass rocketchatpass –rchost http://chat.reiners.io
[/code]

delete messages from channel:
[code]
./rc-client.py –delete –iknow –channel general
[/code]

delete messages from room:
[code]
./rc-client.py –delete –iknow –group mygroup
[/code]

send a message to a group:
[code]
./rc-client.py –send –group mygroup –text “Hello there”
[/code]

send a message to a channel:
[code]
./rc-client.py –send –channel general –text “Hello there”
[/code]

send a message to a user:
[code]
/.rc-client.py –send –user username –text “Hello there”
[/code]

fill a channel with test messages:
[code]
./rc-client.py –trash –channel general
[/code]

file a group with test messages:
[code]
./rc-client.py –trash –group mygroup
[/code]