Coder Social home page Coder Social logo

init.ai-testing's Introduction

Init.ai sample logic server

This repo contains a pre-configured webhook server for use in an Init.ai project. The server can be used to facilitate testing from within the Init.ai console and can also be deployed remotely to handle your production traffic.

Note: When taking your server to production, make sure to customize it to your needs and ensure it is fully tested!

Before getting started, make sure you have:

Quickstart

  1. Install dependencies and start development task:

    npm i && npm run dev

    You should see this in your terminal: demo-server

  2. Provide your server's address to the Init.ai console

Scaffold

This application contains a basic server configuration for handling webhooks as well as a boilerplate implementation of the Init.ai Nods.js SDK. Out of the box, this application can be deployed to Heroku, but is designed to facilitate local development and testing as well.

Server

The server is written using express.js โ€“ you may use any framework you like (or build your own, we respect that too!). Out of the box, it is configured to handle a POST request to the / root endpoint.

Logic

Running your logic

The handler for the webhook endpoint will invoke the runLogic function which can be found in the runLogic.js file. This function returns a Promise which will provide the result generated by the Init.ai client.

If you are using existing logic that was previously hosted by Init.ai, you may keep what you have written. See migrating your logic for more.

Sending the result

As mentioned above, the Promise resolved by runLogic will contain the necessary data to send to the Init.ai API for processing. This has been abstracted out into the sendLogicResult module.

Running the server locally

To start the server and watch tasks:

$ npm run dev

The dev task automatically provisions an ngrok URL to expose localhost and uses nodemon to restart the server as you save changes. The server itself will default to http://localhost:3022.

Environment variables

  • PORT - Set this if you wish to use a port other than the default (3022)
  • LOCAL - Set this to false to skip automatic configuration of an ngrok URL

Exposing localhost

This application can be run on any service you would like and ships completely ready to deploy to Heroku. During development however, it is recommended you make your local machine accessible to external services. We suggest using a tool such as ngrok or localtunnel.

This app contains an ngrok wrapper as a dependency. If you do not have ngrok installed you can run: npm run ngrok to generate a URL which you can provide to the Init.ai console for testing your project.

Configuring webhooks

Init.ai facilitates both "development" and "production" webhooks. The intention is that the "development" webhook may be used to help you iterate on and test your project from the Init.ai console without having to connect to an external messaging service.

See docs for more.

Migrating your logic

In early versions of the Init.ai platform, logic was hosted on AWS Lambda instances provisioned automatically for each project. As such, certain patterns were applied to our node library and the projects scaffolded by Init.ai. Namely, this included a file located at behavior/scripts/index.js which exported a handle function to which an instantiated client was provided as an arugment:

exports.handle = function (client) {
  ...
}

This pattern still works, however, now you are in charge of provisioning the client instance yourself. To do this from your webhook handler you may replace the Logic.run code block with the following:

const InitClient = require('initai-node')
const logicScript = require('./behavior/scripts')

const initNodeClient = InitClient.create(data, {
  succeed(result) {
    sendLogicResult(data.payload)(result)
  }
})

logicScript.handle(initNodeClient)

Additionally, you may want to delete the accompanying runLogic.js file in this case.

Deployments

Heroku

Deploy

Support

  • Email [email protected]
  • File an issue on this repo (Please limit issues to those related to running this webhook server)
  • Join our Slack comunity

Coming soon

PRs for these are welcome!

init.ai-testing's People

Contributors

kyledetella avatar jastbury avatar meganbarnes avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.