@fimwise/sdk
v1.0.8
Published
The @fimwise/sdk offers client SDKs for building bot tasks in JavaScript/TypeScript
Downloads
3
Readme
Fimwise Javascript/Typescript SDK
The @fimwise/sdk
offers client SDKs for building bot tasks in JavaScript/TypeScript.
Building bot tasks in JavaScript primarily involves the following steps:
- Setting up the
@fimwise/sdk
package. - Configuring authentication.
- Developing your bot.
- Run your bot.
Setting up @fimwise/sdk
package
Obtain the package from npm:
npm i @fimwise/sdk
or
yarn add @fimwise/sdk
Authentication configuration
Create a client from the Fimwise Cloud server to authenticate and authorize access from the bot. Upon creating the client, you will receive the following information:
- Client ID: Key for authentication.
- Secret Key: Secret for the key above.
Note: The
Secret Key
is only displayed once after client creation. If lost, reset the client to generate a new one.
You can specify this information in a credentials.json
file:
{
"$schema": "https://fimwise.com/schemas/credentials.schema.json",
"clientId": "client id",
"secretKey": "secret key"
}
Place this file in the root of your project:
│ credentials.json <-- place here
│ package.json
├───node_modules
└───src
index.js
Developing your first bot
Logs the task input and responds with the "Hello World" message:
import { startSimpleBot } from '@fimwise/sdk';
await startSimpleBot(async ({input}) => {
console.log("The task input is: ", input)
return {
message: "Hello world"
}
})
To run this bot, simply call npm start
or node index.js
as with any normal Node.js project.
Requesting resources
We provide several apis for getting resources from our server such as request, task, budget...
import { requestApi } from "@fimwise/sdk";
const request = await requestApi.getRequest("de4df841-1c6d-4c99-81a2-1ac3ab9077c9");
console.log("Found request: ", request);
Developing a more complex bot
Checks for new requests and, if the supplier is available, sends an email to them. Otherwise, it rechecks later:
import { Bot, TaskBase, TaskContext } from '@fimwise/sdk';
class SendMailTask extends TaskBase {
protected async handle({ input, log }: TaskContext) {
const { requestId, subject, supplierId } = input;
log('info', `Received a new request ${requestId}`);
if (await this.checkSupplierAvailable(supplierId)) {
await this.sendMail(subject, supplierId);
log('info', `An email was sent to supplier ${supplierId} to notify a new request`);
} else {
log('warn', `Supplier ${supplierId} is not available, recheck it later`);
return recallTaskAfter();
}
}
private async checkSupplierAvailable(supplierId: string) {
// TODO: some logic here
return true;
}
private async sendMail(subject: string, supplierId: string) {
// TODO: some logic here
}
}
const bot = await Bot.builder()
.task(new SendMailTask())
.concurrency(2)// can handle 2 tasks in the same time
.interval(1000)// check available task each 1 second
.name("SendMailBot")
.build();
await bot.start();