FreshBooks のIFTTT Flows をAzure App Service からキックする

本記事では、IFTTT ワークフローをLigic Apps の標準ウィザードから自動化する方法を説明します。

OData やSwagger などの標準化されたインターフェースを使って、CData API Server とADO.NET Provider for FreshBooks (もしくは170+ の他のADO.NET Providers) を組み合わせて、Logic Apps やMicrosoft Flow からFreshBooks へのデータ連携を実装できます。OData は、データへのリアルタイム連携を、Swagger はLogic Apps およびMicrosoft Flow のスキャッフォールディング、コード生成を可能にするほか、PowerApps でのスキャッフォールディングを可能にします。本記事では、Logic App のIFTTT ワークフローにFreshBooks を追加する方法を説明します。

API Server を設定

Follow the steps below to begin producing secure and Swagger-enabled FreshBooks APIs:

デプロイ

The API Server runs on your own server. On Windows, you can deploy using the stand-alone server or IIS. On a Java servlet container, drop in the API Server WAR file. See the help documentation for more information and how-tos.

The API Server is also easy to deploy on Microsoft Azure, Amazon EC2, and Heroku.

FreshBooks に接続

After you deploy the API Server and the ADO.NET Provider for FreshBooks, provide authentication values and other connection properties by clicking Settings -> Connections and adding a new connection in the API Server administration console. You can then choose the entities you want to allow the API Server access to by clicking Settings -> Resources.

To connect to FreshBooks, you can set the CompanyName and Token connection properties. Alternatively, you can use the OAuth authentication standard.

OAuth can be used to enable other users to access their own company data. To authenticate using OAuth, you will need to obtain the OAuthClientId and OAuthClientSecret by registering an app. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

You will also need to enable CORS and define the following sections on the Settings -> Server page. As an alternative, you can select the option to allow all domains without '*'.

  1. Access-Control-Allow-Origin: Set this to a value of '*'. An Access-Control-Allow-Origin header value of '*' is required to use the API Server in the Logic Apps Designer.
  2. Access-Control-Allow-Methods: Set this to a value of "GET,PUT,POST,OPTIONS".
  3. Access-Control-Allow-Headers: Set this to "x-ms-client-request-id, authorization, content-type".

API Server Users の認可

After determining the OData services you want to produce, authorize users by clicking Settings -> Users. The API Server uses authtoken-based authentication and supports the major authentication schemes. You can authenticate as well as encrypt connections with SSL. Access can also be restricted by IP address; access is restricted to only the local machine by default.

For simplicity, we will allow the authtoken for API users to be passed in the URL. You will need to add a setting in the Application section of the settings.cfg file, located in the data directory. On Windows, this is the app_data subfolder in the application root. In the Java edition, the location of the data directory depends on your operation system:

  1. Windows: C:\ProgramData\CData
  2. Unix or Mac OS X: ~/cdata
[Application] AllowAuthtokenInURL = true

Logic App からFreshBooks データにアクセス

You can use the API Server in a Logic App to create process flows around FreshBooks data. The HTTP + Swagger action provides a wizard to define the operations you want to execute to FreshBooks. The following steps below show how to retrieve FreshBooks data in a Logic App.

If your table has a column containing the creation date of a record, you can follow the steps below to write a function to check the column values for any new records. Otherwise, skip to the Create a Logic App section to send out emails to entities that match a filter.

新しいFreshBooks Entities を確認

To find new FreshBooks entities since a certain time, you can write a function that retrieves a datetime value for the start of the interval:

  1. In the Azure Portal, click New -> Function App -> Create.
  2. Enter a name and select the subscription, resource group, App Service plan, and storage account.
  3. Select your Function App and select the Webhook + API scenario.
  4. Select the language. This article uses JavaScript.
  5. Add the following code to return the previous hour in a JSON object:
    module.exports = function (context, data) { 
      var d = new Date();
      d.setHours(d.getHours()-1); 
      // Response of the function to be used later.
      context.res = { 
        body: { 
          start: d 
        } 
      }; 
      context.done(); 
    };
    

トリガーにFreshBooks を追加

Follow the steps below to create a trigger that searches FreshBooks for results that match a filter. If you created the function above, you can search for objects that were created after the start of the interval returned.

  1. In the Azure Portal, click New and in the Web + Mobile section select Logic App and select a resource group and App Service plan.
  2. You can then use the wizards available in the Logic App Designer, which can be accessed from the settings blade for the Logic App. Select the Blank Logic App template.
  3. Add a Recurrence action that will poll for the FreshBooks objects. This article polls every hour. Select the timezone -- the default is UTC.
  4. Add a function action: Expand the menu in the Add Action dialog and select the option to show Azure functions in the same region. Select the Function App you created earlier and select the function that returns the interval start.
  5. Enter an empty pair of curly brackets, "{}", to pass an empty payload object to the function.
  6. Add the HTTP + Swagger action and enter the swagger URL of the API Server: http://MySite:MyPort/api.rsc/@MyAuthtoken/$swagger
  7. Select the "Return Clients" operation.
  8. Use the descriptions for each property to specify additional parameters such as the columns to retrieve, filters, etc. Below is an example filter:

    Email eq 'Captain Hook'

    The API Server returns the descriptions and other documentation in the swagger document. You can find more information on using the OData API and supported OData in the API Server help documentation.

  9. To use the datetime value returned from the getInterval function, use the "ge" operator with a datetime column in the Clients table and select the Body parameter in the dialog. Note that quotes must be used to surround the datetime value.

  10. Switch to Code View and modify the $filter expression to extract the property containing the start of the interval. Use the syntax '@{body('MyFunc')['MyProp']'.

    "getAllAccount": {
      "inputs": {
        "method": "get",
          "queries": {
            "$filter": "CreatedDate ge '@{body('getInterval')['start']}'"
          },
          "uri": "https://MySite:MyPort/api.rsc/@MyAuthtoken/Clients"
      }
    

You can now access FreshBooks as data sources and destinations in your workflows.

新しいレコードをEmail で通知

Follow the steps below to email a report with any new Clients entities.

  1. In the Logic Apps Designer, add an SMTP - Send Email action.
  2. Configure the necessary information for the SMTP server.
  3. Configure the From, To, Subject, and Body. You can add parameters from the FreshBooks columns returned.

Click Save and then click Run to send email notifications on any FreshBooks records created in the last hour.

 
 
ダウンロード