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

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:

  • test connection to server (--whoami)
  • send a message to a user. (--send --user justin --text "hi there!")
  • send a message to a group. (--send --group groupname --text "Hi there!")
  • send a message to a channel. (--send --channel channelname --text "Hi there!"
  • delete all messages in a group. (--delete --group roomname --iknow)
  • delete all messages in a channel. (--delete --channel roomname --iknow)
  • install saved rocketchat credentials. (--install --rcuser user --rcpass pass --rchost hostname)
  • spam a group with test messages (--trash --iknow --group groupname)
  • 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.

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

python -m pip install cosmicray-rocketchat

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

#!/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                    

once saved, use the following examples:

first install example:  
./rc-client.py --install --rcuser rocketchatuser --rcpass rocketchatpass --rchost http://chat.reiners.io

delete messages from channel:  
./rc-client.py --delete --iknow --channel general

delete messages from room:  
./rc-client.py --delete --iknow --group mygroup

send a message to a group:  
./rc-client.py --send --group mygroup --text "Hello there"

send a message to a channel:  
./rc-client.py --send --channel general --text "Hello there"

send a message to a user:  
/.rc-client.py --send --user username --text "Hello there"

fill a channel with test messages:

./rc-client.py --trash --channel general 

file a group with test messages:

./rc-client.py --trash --group mygroup