Skip to content

Instantly share code, notes, and snippets.

@u1i
Created October 1, 2024 05:32
Show Gist options
  • Save u1i/33394e6a6ace7c9692059343a79d25b4 to your computer and use it in GitHub Desktop.
Save u1i/33394e6a6ace7c9692059343a79d25b4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <USER_ID> <MESSAGE_TEXT>"
exit 1
fi
# Assign input arguments to variables
TOPIC="projects/yopa-433507/topics/backend_messages"
USER_ID=$1
MESSAGE_TEXT=$2
# Define a temporary file to hold the JSON payload
TEMP_FILE=$(mktemp)
# Construct the JSON payload
cat <<EOF > "$TEMP_FILE"
{
"userId": "$USER_ID",
"content": "$MESSAGE_TEXT"
}
EOF
# Publish the message to the Pub/Sub topic with attributes
gcloud pubsub topics publish "$TOPIC" --message="$(cat "$TEMP_FILE")" --attribute="userId=$USER_ID,type=msg,timestamp=$(date +%s)"
# Clean up the temporary file
rm "$TEMP_FILE"
# Inform the user of success
echo "Message sent to topic $TOPIC"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment