Skip to content

Instantly share code, notes, and snippets.

@cgsdev0
Created January 26, 2022 09:30
Show Gist options
  • Save cgsdev0/01df13007be39b1072ad2b4ef118f7c8 to your computer and use it in GitHub Desktop.
Save cgsdev0/01df13007be39b1072ad2b4ef118f7c8 to your computer and use it in GitHub Desktop.
Display live twitch chat directly in your terminal
#!/bin/zsh
# depends on 'websocat' https://github.com/vi/websocat
#
# twitch_chat {channel} - opens a websocket for reading chat
twitch_chat () {
rm -f /tmp/twitch_tunnel;
mkfifo /tmp/twitch_tunnel;
clear;
# Example chat msg:
# @badge-info=;badges=;client-nonce=31ab40a7bef7162bfc8eade860eb185a;color=#DAA520;display-name=BuddysPizza;emotes=;first-msg=0;flags=;id=e86038a5-4d49-4868-aa96-b563da8fc689;mod=0;room-id=56931496;subscriber=0;tmi-sent-ts=1643181677955;turbo=0;user-id=42089909;user-type= :[email protected] PRIVMSG #badcop_ :!help
# Format a twitch chat message
parsechat() {
color=$(echo -n "$1" | grep -oP "color=.*?;" | cut -d'=' -f2- | tr -d '#;\n')
name=$(echo -n "$1" | grep -oP "display-name=.*?;" | cut -d'=' -f2- | tr -d ';\n')
if [[ "${#color}" != "6" ]]; then
color=${"$(echo "$name" | md5sum)":0:6}
fi
printf "\x1b[38;2;$(( 16#${color:0:2} ));$(( 16#${color:2:2} ));$(( 16#${color:4:2} ))m"
printf '$name\033[0m'
printf ": "
echo "$1" | grep -oP "PRIVMSG.*$" | cut -d':' -f2-
}
# reads websocket messages and replies to pings;
# formats chat messages and writes them to stderr
responder() {
while IFS= read line; do
[[ $line == "PING"* ]] && echo "PONG :tmi.twitch.tv\r";
[[ "$1" == *" PRIVMSG "* ]] && parsechat "$line" 1>&2;
done;
exit 0;
}
(echo "CAP REQ :twitch.tv/tags twitch.tv/commands\r\nPASS SCHMOOPIIE\r\nNICK justinfan21808\r\nJOIN #${1}\r"; \
</tmp/twitch_tunnel | responder) \
| websocat wss://irc-ws.chat.twitch.tv:443 \
| >/tmp/twitch_tunnel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment