Channel framework startup tutorial - Step 2: Get resources from the origin service (Polling)

In this step, you add an endpoint to support polling. Zendesk Support will periodically POST to your endpoint. Your code will retrieve data from the WordPress API, transform it to a format readable by Zendesk Support, and return it to the caller.

To query the WordPress REST API, the integration service needs connection parameters such as the REST endpoint URL, user information, and ordering parameters. The pullRequestOptions function formats this information for use with the networking library.

In wordpress.js, add the pullRequestOptions function:

function pullRequestOptions(metadata, state) {  const options = {    uri: `${metadata.wordpress_location}/wp-json/wp/v2/comments`,    qs: {      orderby: 'id',      order: 'asc',      page: '1',      per_page: '100'    },    auth: {      user: metadata.login,      pass: metadata.password    }  };
  if (state && state.most_recent_item_timestamp) {    options.qs.after = state.most_recent_item_timestamp;  }
  return options;}

When the integration service receives a response from WordPress, the integration service needs to transform the data to Zendesk Support format.

In wordpress.js, add the transformComments logic:

function transformComments(comments) {  let link;
  return comments.length ? => {    link =;
    return {      external_id: externalCommentId(link,,,      message: stripHTML(comment.content.rendered),      parent_id: externalCommentId(link, comment.parent,,      created_at: (new Date(comment.date_gmt)).toISOString(),      author: {        external_id:,        name: comment.author_name || 'Anonymous'      }    };  }) : [];}

In wordpress.js, add the pull logic:

exports.pull = (metadata, state, res) => {  request.get(    pullRequestOptions(metadata, state),    (error, wordpressResponse, body) => {      let bodyInfo;      let transformedComments;      let newState;      let errorDescription;
      if (!error && wordpressResponse.statusCode === 200) {        try {          bodyInfo = JSON.parse(body);          transformedComments = transformComments(bodyInfo);          newState = pullState(bodyInfo, state);          res.send({            external_resources: transformedComments,            state: JSON.stringify(newState)          });        } catch (e) {          // Bad/unexpected data from WordPress          // 502 == bad gateway          res.sendStatus(502);        }      } else if (wordpressResponse && wordpressResponse.statusCode) {        // WordPress returned an error, pass through the status code and        // error description        errorDescription = {};        if (body) errorDescription = { error_info: body };        res.status(wordpressResponse.statusCode).send(errorDescription);      } else {        // Networking error or similar- no response        // 503 == service unavailable        res.sendStatus(503);      }    }  );};

This is a lot of code, but don’t worry, we’ll explain the pull function in detail.

On line three, we call pullRequestOptions to set up the GET request parameters. On line two, we perform the GET to WordPress based on those parameters.

Everything else is a callback function to handle the response from WordPress.

Lines 11-23 handle the success case. The code parses the response from WordPress and converts it to the Zendesk Support format by calling transformComments . More details on the Zendesk Support format are available in the Channel framework documentation .

Lines 20-22 handle errors raised by the integration code. Read the documentation on how Zendesk Support handles errors from integration services for details.

Now you can verify step 2 works by using curl to hit the pull endpoint.

Start the server by running node server.js in the integration service directory (the location of wordpress.js). The integration service will be available at http://localhost:3000.

Set the WordPress variables and run this curl command:

curl -d "metadata={\"password\":\"$WORDPRESS_PASSWORD\", \"login\":\"$WORDPRESS_USER\", \"wordpress_location\":\"$WORDPRESS_URL\",\"author\":\"1\"}&state={}" http://localhost:3000/pull

This command posts to the pull endpoint you built. It passes the metadata and state for calling WordPress. Your pullRequestOptions function processes the metadata and state. We will discuss metadata and state later. If you have created some WordPress posts and comments, the response should look similar to this:

{"external_resources":[{"external_id":"8:2:http://localhost:25789/index.php/rick-astley-50/#comment-2","message":"Never Gonna Give You Up.\n","parent_id":"8:0:http://localhost:25789/index.php/rick-astley-50/#comment-0","created_at":"2016-07-19T22:56:33.000Z","author":{"external_id":"1","name":"lchan"}}],"state":"{\"most_recent_item_timestamp\":\"2016-07-19T22:56:33\"}"}

Continue on Channel framework startup tutorial - Step 3: Add logging of event callbacks.