Skip to content

Instantly share code, notes, and snippets.

@weavenet
Created December 2, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weavenet/68dbb941bade0999cb105d065fbe1a8e to your computer and use it in GitHub Desktop.
Save weavenet/68dbb941bade0999cb105d065fbe1a8e to your computer and use it in GitHub Desktop.
Python script to delete all chat messages in key base

keybase-chat-cleanup

Simple python script to delete all chat messages in your keybase account.

It shells out to the keybase CLI command to access the keybase api.

It expects you to have logged into the device where it is run with the account you wish to cleanup. It also requires python3 be installed.

Verify it is correctly setup via:

$ keybase chat ls

Then run via

python3 keybase-chat-clenaup.py USERNAME

It has been tested on OSX.

#!/usr/bin/env python3
import json
import sys
import subprocess
import time
from time import sleep
def list_conversations():
list_json = json.dumps({"method": "list"})
output = subprocess.check_output(["keybase", "chat", "api", "-m", list_json])
return json.loads(output)["result"]["conversations"]
def delete_message(channel_name, message_id):
delete_json = json.dumps(
{"method": "delete",
"params": {
"options": {
"channel": {"name": channel_name},
"message_id": message_id}}})
output = subprocess.check_output(["keybase", "chat", "api", "-m", delete_json])
return json.loads(output)
def list_messages(conversation_name):
messages_json = json.dumps(
{"method": "read",
"params": {
"options": {
"channel": {"name": conversation_name}}}})
output = subprocess.check_output(["keybase", "chat", "api", "-m", messages_json])
return json.loads(output)["result"]["messages"]
def delete_all_my_messages(username):
for c in list_conversations():
try:
name = c["channel"]["name"]
print("Processing channel {}".format(name))
for m in list_messages(name):
message_id = m["msg"]["id"]
channel_name = m["msg"]["channel"]["name"]
sender_username = m["msg"]["sender"]["username"]
content_type = m["msg"]["content"]["type"]
# Skip messages if they were not sent by me or are already deleted
if (sender_username != username) or (content_type == "delete"):
print("Skipping message '{}' from channel '{}' sent by '{}' with contenct_type '{}'.".format(message_id, channel_name, sender_username, content_type))
continue
print("Deleting message '{}' from channel '{}' sent by '{}' with contenct_type '{}'.".format(message_id, channel_name, sender_username, content_type))
res = delete_message(channel_name, message_id)
result_message = res["result"]["message"]
print("Result for '{}' '{}': '{}'.".format(message_id, channel_name, result_message))
except Exception as e:
print("Error: {}".format(e))
continue
def run():
if (len(sys.argv) < 2):
print("Usage {} USERNAME".format(sys.argv[0]))
sys.exit(1)
username = sys.argv[1]
delete_all_my_messages(username)
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment