Snippets

Aerobatic Code for blog post at https://blog.bitbucket.org/2015/07/24/an-easier-way-to-call-an-api-from-your-web-app/

Created by Ivan Storck
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Send a Support Message to a HipChat Room</title>
                    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h1>Send a message</h1>
Use the form below to send a message to our HipChat room. Include your
      email and someone will contact you as soon as possible.

This form sends directly to the webhook

<form id="hipchat" action="#">
<div class="row">
<div class="twelve columns">
          <label for="name">Name:</label>
          <input class="u-full-width" type="text" id="name" placeholder="Your Name">
          <label for="email">Email:</label>
          <input class="u-full-width" type="text" id="email" placeholder="you@yoursite.com">
          <label for="message">Message:</label>
          <input class="u-full-width" type="text" id="message" placeholder="your message to the chat room">
          <button id="submit-button" type="submit" class="button button-primary">Send</button></div>
</div>
</form>
<div style="display: none" id="confirm">
      <button>Your request was sent to the HipChat room successfully</button></div>
</div>
<script charset="utf-8" src="main.js"></script>
</body>
</html>
var ready = function(fn) {
  if (document.readyState != 'loading') {
    fn();
  } else {
    document.addEventListener('DOMContentLoaded', fn);
  }
};

var checkLoad = function() {
  if (this.status >= 200 && this.status < 400) {
    document.getElementById('hipchat').style.display = 'none';
    document.getElementById('confirm').style.display = 'block';
  } else {
    alert('Error with API Endpoint\n' + this.response);
  }
};

var ajaxError = function() {
  alert('Connection Error with API Endpoint');
};

var processFormData = function() {
  var formData = 'Name: ' + document.getElementById('name').value + 
    ' | Email: ' + document.getElementById('email').value +
    ' | Message: ' + document.getElementById('message').value;

  return JSON.stringify({
    color: 'green',
    message: formData,
    notify: 'true'
  });
};

var submitter = function(event) {
  var request = new XMLHttpRequest();
  var room = '1753375';

  request.open('POST', '/hipchat/' + room, true);

  request.setRequestHeader(
    'Content-Type',
    'application/json; charset=UTF-8'
  );

  request.setRequestHeader(
    'X-Requested-With',
    'XMLHttpRequest'
  );

  request.onload = checkLoad;
  request.onerror = ajaxError;
  request.send(processFormData());
  event.preventDefault();
};

ready(function() {
   document.getElementById('hipchat')
     .addEventListener('submit', submitter);
});
{
  "name": "aerohip",
  "version": "0.0.1",
  "description": "Demo of using express-request-proxy on Aerobatic.com hosted apps",
  "main": "index.html",
  "scripts": {
    "start": "node ./node_modules/live-server/live-server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@bitbucket.org/aerobatic/aerohip.git"
  },
  "keywords": [
    "aerobatic",
    "hipchat"
  ],
  "author": "Ivan Storck",
  "license": "ISC",
  "homepage": "https://bitbucket.org/aerobatic/aerohip#readme",
  "_virtualApp": {
    "router": [
      {
        "module": "express-request-proxy",
        "path": "/hipchat/:room",
        "method": "post",
        "options": {
          "url": "https://api.hipchat.com/v2/room/:room/notification",
          "query": {
            "auth_token": "env:HIPCHAT_AUTH_TOKEN"
          }
        }
      }
    ]
  },
  "devDependencies": {
    "live-server": "^0.7.1"
  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.