Skip to content

Web API provider

Overview

Web API/Restful provider allow you to use any web API in your chatbot. Defining Web API provider is simple.

Create Web API provider

On the project list page inside an organization, click Create and select Create provider. On the create provider popup, select Web API provider type. Answer the rest of the questions on the popup window and click Create.

Implement functions

On the service tab, first add the interface you want to implement. The function you need to implemented from these interfaces will show up in the function section. You can also add local functions that is needed to help the implementation.

Double click on one of the function, you provide the implementation in two different ways.

Provider dependent functions: Web API function

To implement a Web API function, you will need to select the http method (GET, POST, etc), and add url. You can add the headers for the function call, which will combined with global headers defined in the provider's configuration. These headers are useful for variety of things, including authentication. And finally, if needed, you can add json template in the body. Both key, value and body can be string template (in Kotline sense, inside ${}), where the actual value will the determined at the runtime.

The types for input parameter and return will need to be defined on the platform. We will generate the Kotlin data class with Json serialization enabled when the chatbot make use of these interfaces and providers are deployed.

Kotlin functions

To update and append your business, OpenCUI provides external functions: update and append. You can call these functions using connection.update and connection.append in Kotlin functions. Check out the definitions of these functions in io.opencui.provider.GoogleSheetsConnection. To learn the source of the function, see spreadsheets.values.update and spreadsheets.values.append.

  • The input parameters are the same in these two functions.
NameTypeReference
rangekotlin.StringUse range to select specific ranges. To learn how to define it, see A1 notation.
valueskotlin.Any[]Use values to add/append a row of values by putting values in a list, likelistOf(a, b, c).
valueInputOptionkotlin.StringUse valueInputOption to determine how input data should be interpreted. To know what options you should choose, see ValueInputOption.
  • The return types are UpdateValuesResponse and AppendValuesResponse. You can use the methods in these classes to check if the response meets expectations. For example, if you expect to update 3 values, use UpdateValuesResponse.getUpdatedCells == 3 to check.

Suppose you want to update a user's delivery address and phone number in the range of "'UserInfo'!B5:C5". If the input parameters are address and phoneNumber, the Kotlin function will be like this:

kotlin
var values = listOf(address, phoneNumber)
var result = connection!!.update("'UserInfo'!B2", values, "RAW")
// if the update is successful, return true, otherwise, return false
return result!!.getUpdatedCells() == 2