Skip to content

Instantly share code, notes, and snippets.

@yeoldegrove
Created June 16, 2019 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yeoldegrove/8133435c7b0ac0d5e85e17de448486f2 to your computer and use it in GitHub Desktop.
Save yeoldegrove/8133435c7b0ac0d5e85e17de448486f2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This stupid script can convert slack json exports
# (created with https://solawivuml.slack.com/apps/A19P6G35Y-export?next_id=0)
# to rocket.chat csv imports.
# The zip file can be directelly imported into rocket.chat.
test -z $1 && echo "usage: $0 slack_export_json_file.json channel_name" && exit 1
# format users (needed for import)
cat $1 | jq '.[] | {name,real_name} | @json' | awk -F '"' '{print $5","$5"@dummy.import,"$9}' | sed 's#\\##g' | sort -n | uniq >users.csv
# format channel messages
mkdir -p $2
cat $1|jq '.[] | {name,ts,text} | map(.) | @csv' | sed 's#\"\\\"#"#g'|sed 's#\\\",\\\"#","#g' | sed 's#\\\"\"#"#g' | sed "s#\\\\\"#'#g" >$2/messages.csv
grep -v '^",' $2/messages.csv >$2/messages.csv2
# cleanup ts digit
sed -i 's#\.[[:digit:]]\+##g' $2/messages.csv2
# multiply ts to miliseconds
awk -F'","' '{print $1"\",\""$2*1000"\",\""$3}' $2/messages.csv2 >$2/messages.csv
rm $2/messages.csv2
# add newlines again
sed -i "s#\\\n#\n#g" $2/messages.csv
# create zip file
zip -ru channel_$2.zip channels.csv users.csv $2
@sam2kb
Copy link

sam2kb commented Nov 3, 2019

This script made my day, even though it produced an invalid CSV that Rocket.Chat can't read, and shows MESSAGES: 0
To be more specific, it fails to add a closing " in the following situations:

"text": "blah\\" becomes "blah\'
"text": "blah \"blah\" : \"2021-10-10\"," becomes "blah 'blah' : '2021-10-10

@sam2kb
Copy link

sam2kb commented Nov 3, 2019

So I had to open the file in LibreOffice Calc and modify the rows in question, only took 5 minutes or so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment