Skip to content

Instantly share code, notes, and snippets.

@thehowl
Last active March 22, 2018 22:15
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 thehowl/97c77114859c64c67d357adf604229f4 to your computer and use it in GitHub Desktop.
Save thehowl/97c77114859c64c67d357adf604229f4 to your computer and use it in GitHub Desktop.
Claws piping example

Claws was originally built because I had to scratch my own itch, which is to say I was implementing a WebSocket API for a project of mine, Ripple (not the cryptocurrency!) and there was no good command-line WS client that wasn't completely barebones. So that's really how the project was born.

Here's an example of the piping feature, using the Ripple WS API. It's really only to demonstrate what it can do (I needed something I could build in a reasonable amount of time and that was easily understandable :P). But, with programming, the sky is the limit, so create your own scripts to serve your purpose!

If you want to try it out for yourself, download new_score.sh, place it in e.g. ~/scripts, then add to ~/.config/claws.json:

	"Pipe": {
		"In": [
			"bash", "/home/howl/scripts/new_score.sh"
		],
		"Out": null
	}

(Of course, change the username :P)

Then open claws, connect to wss://api.ripple.moe/api/v1/ws, and send {"type": "subscribe_scores", "data": []}. Unless you're sending the message at three AM, you should start receiving new scores that will be run through the script first, as shown in the screenshot above.

#!/bin/bash
# Take the data from stdin.
data="$(cat -)"
# Using jq (https://stedolan.github.io/jq/), get the type of the message.
# We use -r, so that there are no quotes around the resulting string.
message_type="$(echo "$data" | jq -r .type)"
# Handle the different kinds of messages from the API.
case "$message_type" in
pong) echo "Pong!" ;;
subscribed_to_scores) echo "Subscribed!" ;;
new_score) echo "New score made by $(echo "$data" | jq -r .data.user.username )" ;;
*) echo "idk: $data" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment