Skip to content

Instantly share code, notes, and snippets.

@hateshape
Created February 22, 2019 21:34
Show Gist options
  • Save hateshape/e324fab8088ba5cbabff7a7884824440 to your computer and use it in GitHub Desktop.
Save hateshape/e324fab8088ba5cbabff7a7884824440 to your computer and use it in GitHub Desktop.
#!/bin/bash
# If it "no worked" (Technical Term) then you probably need to install jq!
# Pretty Colors
RESET='\033[00m'
BLUE='\033[01;34m'
usage() {
echo -e ${BLUE}"Usage: $0 [ -a APPID ] [ -s SESSION ]"${RESET} 1>&2
echo -e ${BLUE}"\n Example: "${RESET} 1>&2
echo -e ${BLUE}' If "intercom-session-wwxxyyzz" is the Cookie Name, wwxxyyzz would be the "APPID" we need]'${RESET} 1>&2
echo -e ${BLUE}" The SESSION value would simply be the Cookie value captured for intercom-session-wwxxyyzz\n"${RESET} 1>&2
}
exitifbadstuffhappens() {
usage
exit 1
}
while getopts ":a:s:" options; do
case "${options}" in
a)
APPID=$2
;;
s)
SESSION=$4
;;
*)
exitifbadstuffhappens
;;
esac
done
if [ "$APPID" = "" ]; then
exitifbadstuffhappens
fi
if [ "$SESSION" = "" ]; then
exitifbadstuffhappens
fi
getmesomeintercoms(){
if [ -e intercom-messages-working-$(date +"%Y-%m-%d").txt ] || [ -e intercom-messages-working-temp-$(date +"%Y-%m-%d").txt ] || [ -e intercom-messages-$(date +"%Y-%m-%d").txt ] ; then
rm intercom-messages-working-$(date +"%Y-%m-%d").txt intercom-messages-working-temp-$(date +"%Y-%m-%d").txt intercom-messages-$(date +"%Y-%m-%d").txt
fi
echo -e ${BLUE}"------------------------------- Getting User Intercom Message(s) --------------------------------"${RESET}
# Make API Call to Download Intercom Messages
curl -skoX $'POST'-H $'Host: api-iam.intercom.io' --data-binary $"app_id=$1&anonymous_session=$2" -k "https://api-iam.intercom.io/messenger/web/conversations" > intercom-convo.json
# Find Number of Message IDs
LEN=$(jq '.[]|length' intercom-convo.json | sed -n 2p)
# Loop Through and Parse Messages
for (( i=0; i<${LEN}; i++ )); do
echo "Message:" $((i+1)) >> intercom-messages-working-$(date +"%Y-%m-%d").txt;
jq -r ".conversations[$i]" intercom-convo.json | jq -r '.conversation_message' | jq '[.author | {Name: .name}]'| jq .[] | grep -vE '(\{)|(\})' >> intercom-messages-working-$(date +"%Y-%m-%d").txt;
jq -r ".conversations[$i]" intercom-convo.json | jq "[.conversation_message|{Message_ID: .id,sent_at:.sent_at }]"| jq .[] | grep -vE '(\{)|(\})' >> intercom-messages-working-$(date +"%Y-%m-%d").txt;
jq -r ".conversations[$i]" intercom-convo.json | jq -r '.conversation_message' | jq '[.blocks[] | {Message: .text}]'| jq .[] | grep -vE '(\{)|(\})'>> intercom-messages-working-$(date +"%Y-%m-%d").txt;
echo >> intercom-messages-working-$(date +"%Y-%m-%d").txt;
done
sed 's/"//g' intercom-messages-working-$(date +"%Y-%m-%d").txt | sed 's/,//g' > intercom-messages-working-temp-$(date +"%Y-%m-%d").txt
# Replace EPOCH With Real Dates
for x in `grep sent_at intercom-messages-working-temp-$(date +"%Y-%m-%d").txt | cut -d: -f2 | awk '{print $1}'`; do
echo $x | sed -i "s/$x/$(date -d @$x)/" intercom-messages-working-temp-$(date +"%Y-%m-%d").txt;
done
# Remove NULL Messages
grep -v "Message: null" intercom-messages-working-temp-$(date +"%Y-%m-%d").txt >> intercom-messages-$(date +"%Y-%m-%d").txt
echo >> intercom-messages-$(date +"%Y-%m-%d").txt
cat intercom-messages-$(date +"%Y-%m-%d").txt
echo -e ${BLUE}"------------------------------- Done Getting Intercom Message(s) --------------------------------"${RESET}
}
getmesomeintercoms $2 $4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment