[
  {
    "path": "LICENSE",
    "content": "Copyright (c) HipChat, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "hipchat-cli\n===========\n\nSome command line scripts for performing [HipChat][hc] API calls.\nFor details on how to obtain a token and room id, see\n[the tutorial](tutorial/README.md).\n\n./hipchat\\_room\\_message\n-----\nUsed to send a message to a room.\n\n```bash\n$ cat message.txt | ./hipchat_room_message -t <token> -r <room> -f \"System\"\n```\n\n[hc]: http://www.hipchat.com\n\nConfiguration\n-----\n\nhipchat-cli can be configured with one of the following options in a combination of those.\n\n* Command-line options\n* Environment variables\n* Configuration file\n\n### Command-line options\n\nCommand-line options are passed into hipchat-cli. A list of options is available by executing ```hipchat_room_message -h```.\n```\n$ ./hipchat_room_message -h\nUsage: ./hipchat_room_message -t <token> -r <room id> -f <from name>\n\nThis script will read from stdin and send the contents to the given room as\na system message. Or use -i message.\n\nOPTIONS:\n   -h             Show this message\n   -t <token>     API token\n   -r <room id>   Room ID\n   -f <from name> From name (optional in v2 API)\n   -c <color>     Message color (yellow, red, green, purple, gray\n                                 or random - default: yellow)\n   -m <format>    Message format (html or text - default: html)\n   -i <input>     Optional: Input to send to room (default: stdin)\n   -l <level>     Nagios message level (critical, warning, unknown,\n                                        ok, down, up).  Will override color.\n   -n             Trigger notification for people in the room\n   -o             API host (api.hipchat.com)\n   -v <version>   API version (default: v1)\n   -k             Allow curl to make insecure SSL connections\n```\n\n#### Usage example:\n```bash\n$ ./hipchat_room_message -vv2 -t <TOKEN> -r <ROOM> -i \"This is a message\"\n```\n\n### Environment variables\n\nAll options available as command-line options can be passed in as environment variables.\n\nEnvironment variable | Description\n-------------------- | -----------\nHIPCHAT_TOKEN        | API token\nHIPCHAT_ROOM_ID      | Room ID\nHIPCHAT_FROM         | From name\nHIPCHAT_COLOR        | Message color (yellow, red, green, purple, gray or random - default: yellow)\nHIPCHAT_FORMAT       | Message format (html or text - default: html)\nHIPCHAT_NOTIFY       | Trigger notification for people in the room (default: 0)\nHIPCHAT_HOST         | API host (default: api.hipchat.com)\nHIPCHAT_LEVEL        | Message Level (targetting Nagios states, critical, warning, unknown, ok)\nHIPCHAT_API          | API version (default: v1)\n\n#### Usage example:\n```bash\n$ cat message.txt | HIPCHAT_TOKEN=<token> HIPCHAT_ROOM_ID=1234 ./hipchat_room_message -f \"System\"\n```\n\n### Configuration file\n\nAll environment variables can be specified in a configuration file. The configuration file is ```/etc/hipchat```.\n\n#### Usage example:\n\nConfiguration in ```/etc/hipchat```:\n```bash\nHIPCHAT_TOKEN=<token>\nHIPCHAT_ROOM_ID=1234\n```\n\nCommand-line:\n```bash\n$ cat message.txt | HIPCHAT_FROM=\"System\" ./hipchat_room_message -c green\n```\n"
  },
  {
    "path": "hipchat_room_message",
    "content": "#!/usr/bin/env bash\n\n###############################################################################\n#\n# ./hipchat_room_message\n#\n# A script for sending a system message to a room.\n#\n# Docs: http://github.com/hipchat/hipchat-cli\n#\n# Usage:\n#   cat message.txt | ./hipchat_room_message -t <token> -r 1234 -f \"System\"\n#   echo -e \"New\\nline\" | ./hipchat_room_message -t <token> -r 1234 -f \"System\"\n#\n###############################################################################\n\n# exit on failure\nset -e\n# http://apple.stackexchange.com/questions/68684/why-sh-c-echo-n-1-is-different-from-bash-c-echo-n-1\nshopt -u xpg_echo\n\nusage() {\n  cat << EOF\nUsage: $0 -t <token> -r <room id> -f <from name>\n\nThis script will read from stdin and send the contents to the given room as\na system message. Or use -i message.\n\nOPTIONS:\n   -h             Show this message\n   -t <token>     API token\n   -r <room id>   Room ID\n   -f <from name> From name (optional in v2 API)\n   -c <color>     Message color (yellow, red, green, purple, gray\n                                 or random - default: yellow)\n   -m <format>    Message format (html or text - default: html)\n   -i <input>     Optional: Input to send to room (default: stdin)\n   -l <level>     Nagios message level (critical, warning, unknown,\n                                        ok, down, up).  Will override color.\n   -n             Trigger notification for people in the room\n   -o             API host (api.hipchat.com)\n   -v <version>   API version (default: v1)\n   -k             Allow curl to make insecure SSL connections\nEOF\n}\n\n# Include hipchat defaults if available\ntest -f /etc/hipchat && . /etc/hipchat\ntest -f ~/.hipchat && . ~/.hipchat\n\nTOKEN=${HIPCHAT_TOKEN:-}\nROOM_ID=${HIPCHAT_ROOM_ID:-}\nFROM=${HIPCHAT_FROM:-}\nCOLOR=${HIPCHAT_COLOR:-yellow}\nFORMAT=${HIPCHAT_FORMAT:-html}\nMESSAGE=${HIPCHAT_MESSAGE:-html}\nNOTIFY=${HIPCHAT_NOTIFY:-0}\nHOST=${HIPCHAT_HOST:-api.hipchat.com}\nLEVEL=${HIPCHAT_LEVEL:-}\nAPI=${HIPCHAT_API:-v1}\nALLOW_INSECURE=false\n\nwhile getopts \"ht:r:f:c:m:o:i:l:v:nk\" OPTION; do\n  case $OPTION in\n    h) usage; exit 1;;\n    t) TOKEN=$OPTARG;;\n    r) ROOM_ID=$OPTARG;;\n    f) FROM=$OPTARG;;\n    c) COLOR=$OPTARG;;\n    m) FORMAT=$OPTARG;;\n    n) NOTIFY=1;;\n    i) INPUT=$OPTARG;;\n    l) LEVEL=$OPTARG;;\n    o) HOST=$OPTARG;;\n    v) API=$OPTARG;;\n    k) ALLOW_INSECURE=true;;\n    [?]) usage; exit;;\n  esac\ndone\n\n# check for required args\nif [[ -z $TOKEN ]] || [[ -z $ROOM_ID ]] || [[ -z $FROM && $API = \"v1\" ]]; then\n  if [[ -z $TOKEN ]]; then\n    echo \"$(basename $0): missing TOKEN\"\n  fi\n  if [[ -z $ROOM_ID ]]; then\n    echo \"$(basename $0): missing ROOM_ID\"\n  fi\n  if [[ -z $FROM && $API = \"v1\" ]]; then\n    echo \"$(basename $0): missing FROM\"\n  fi\n  usage\n  exit 1\nfi\n\n# nagios levels\nif [ ! -z \"$LEVEL\" ]; then\n  if [[ $LEVEL == 'CRITICAL' ]] || [[ $LEVEL == 'critical' ]]; then\n    COLOR=\"red\";\n  elif [[ $LEVEL == 'WARNING' ]] || [[ $LEVEL == 'warning' ]]; then\n    COLOR=\"yellow\";\n  elif [[ $LEVEL == 'UNKNOWN' ]] || [[ $LEVEL == 'unknown' ]]; then\n    COLOR=\"gray\";\n  elif [[ $LEVEL == 'OK' ]] || [[ $LEVEL == 'ok' ]]; then\n    COLOR=\"green\";\n  elif [[ $LEVEL == 'DOWN' ]] || [[ $LEVEL == 'down' ]]; then\n    COLOR=\"red\";\n  elif [[ $LEVEL == 'UP' ]] || [[ $LEVEL == 'up' ]]; then\n    COLOR=\"green\";\n  fi\nfi\n\nif [ -z \"$INPUT\" ]; then\n  # read stdin\n  INPUT=$(cat)\nfi\n\n# replace newlines with XHTML <br>\nif [ $FORMAT == 'html' ]; then\n    INPUT=$(echo -n \"${INPUT}\" | perl -p -e 's/\\n/<br \\/>/')\nfi\n\n# replace bare URLs with real hyperlinks\nINPUT=$(echo -n \"${INPUT}\" | perl -p -e \"s/(?<!href=\\\"|href='| src=\\\"| src=')((?:https?|ftp|mailto)\\:\\/\\/[^ \\n]*)/\\<a href=\\\"\\1\\\"\\>\\1\\<\\/a>/g\")\n\n# urlencode with perl\nif [ $API == 'v1' ]; then\n  INPUT=$(echo -n \"${INPUT}\" | perl -p -e 's/([^A-Za-z0-9])/sprintf(\"%%%02X\", ord($1))/seg')\nfi\n\n# escape special characters in json\nif [ $API == 'v2' ]; then\n  INPUT=$(echo -n \"${INPUT}\" | sed 's/\\\\/\\\\\\\\/g' | sed 's/\"/\\\\\\\"/g')\nfi\n\n# replace notification boolean from 1/0 to 'true'/'false' for API v2\nif [ $API == 'v2' ]; then\n  if [ $NOTIFY -eq 0 ]; then\n    NOTIFY='false'\n  else\n    NOTIFY='true'\n  fi\nfi\n\ncurl_opts=\"-sS\"\nif [ $ALLOW_INSECURE == true ]; then\n    curl_opts+=\" --insecure\"\nfi\n\n# do the curl, using a temporary filename\n# (stored in BODY) to avoid long URL errors\nBODY=`mktemp`\n\nif [ $API == 'v2' ]; then\n    (echo \"{\\\"color\\\":\\\"$COLOR\\\", \"\n\n     if [[ -n \"$FROM\" ]] ; then\n         echo \"\\\"from\\\": \\\"$FROM\\\", \"\n     fi\n     echo \"\\\"message\\\":\\\"$INPUT\\\", \\\"message_format\\\":\\\"$FORMAT\\\", \\\"notify\\\":$NOTIFY}\"\n    ) > $BODY\n  curl ${curl_opts} \\\n    -H 'Content-type: application/json' \\\n    -H \"Authorization: Bearer $TOKEN\" \\\n    -d @$BODY \\\n    https://$HOST/v2/room/$ROOM_ID/notification\nelse\n  echo \"auth_token=$TOKEN&room_id=$ROOM_ID&from=$FROM&color=$COLOR&message_format=$FORMAT&message=$INPUT&notify=$NOTIFY\" > $BODY\n  curl ${curl_opts} \\\n    -d @$BODY \\\n    https://$HOST/v1/rooms/message\nfi\n\nrm -f $BODY\n"
  },
  {
    "path": "tutorial/README.md",
    "content": "Tutorial\n========\n\nHere is a step-by-step tutorial on how to use this command-line\ntool to integration with [HipChat][hc].  This tutorial only\ndescribes `v2` access.\n\n* [Obtaining an Access Token](#obtaining-an-access-token)\n* [Obtaining a Room API ID](#obtaining-a-room-api-id)\n* [Sending Messages](#sending-messages)\n  * [Getting Fancy](#getting-fancy)\n    * [Color](#color)\n    * [From](#from)\n    * [Inline Markup](#inline-markup)\n\n## Obtaining an Access Token\n\nIn order to talk to the hipchat servers, you will require an access\ntoken.  This is easily obtained from your profile.\n\nLog in to your site's home page, http://_site_.hipchat.com\n\n![HipChat Home Page](image/01-frontpage.png)\n\nFrom the home page, click on the Edit Profile button.  You may need to\nre-authenticate, but that should bring up your profile page:\n\n![Profile Page](image/02-editing-profile.png)\n\nThere, on the left menu is a panel for **API access**.  Click\nthat to bring up the API access management page:\n\n![API Access Page](image/04-create-key.png)\n\nAt the bottom of that page is a **Create new token** section;\nselect the scopes you want (for normal posting of messages\nto a room, all you need is the _Send Notification_ scope).\nGive the token a label so you can remember later why you created\nit, and click **Create**.\nThat should update the page to include the new token:\n\n![New Token Display](image/05-created-key.png)\n\nThe Personal token is the one you want.  Save it off for later use.\n\n## Obtaining a Room API ID\n\nNow you need to figure out the room ID.  Click on the **Rooms**\ntab in the admin page.\n\n![Rooms Listing](image/06-rooms-list.png)\n\nYou can search for the room or rooms you are interested; in this case\nI only have a few.  Click on the name of the room of interest to get\nits details:\n\n![Room Details](image/07-room-details.png)\n\nThe room details page includes the **API ID** of the room, which is\nwhat you will use in the CLI to send messages to that room.\n\n## Sending Messages\n\nNow you have all the information needed to send messages.  (Note that\nthe API keys obtained in the first step are is only for v2 of the API,\nbe sure to include the HIPCHAT_API settting)\n\n    export HIPCHAT_TOKEN=jPn....Pq8\n    export HIPCHAT_ROOM_ID=32...8\n    export HIPCHAT_API=v2\n    echo Hello | ./hipchat_room_message\n\nCongratulations!  That should have sent a message to your room.\n\nAlso, if you like, you can configure these variables in a `~/.hipchat\nfile, which will be sourced by the command-line tool.\n\n![Hello Message](image/08-hello.png)\n\n### Getting Fancy\n\nThere are several options that can be used to make your messages\na little fancier.\n\n#### Color\n\nThe `-c` option sets the background color of the message:\n\n    ./hipchat_room_message -c red -i \"Something is amiss\"\n\n![Amiss Message](image/09-amiss.png)\n\n#### From\n\nYou can use the **from** option (`-f`) to mark your message as\nbeing from a particular place.\n\n    echo Work with your destiny. Stop trying to outrun it. | \\\n      ./hipchat_room_message -c green -f \"Fortune Cookie\"\n\n![Fortune Cookie Message](image/10-fortune.png)\n\n#### Inline Markup\n\nAnd, the message body supports HTML-style inline markup.\n\n    echo '<b>Scorpio:</b> <i>Carpe diem!</i>' | \\\n      ./hipchat_room_message -c green -f \"Fortune Cookie\"\n\n![Inline Markup](image/11-inline-markup.png)\n\n[hc]: http://www.hipchat.com\n"
  }
]