MonoChat SDK Documentation
Overview
MonoChat SDK enables businesses to integrate and manage custom business logic within the MonoChat platform. This SDK offers a wide range of utilities, methods, and modules, allowing you to interact seamlessly with the platform's various features such as custom collections, payment systems, chat functions, and more.
You can access the SDK in your CustomFunctions via the sdk
variable.
const { sdk } = CustomFunction;
SDK Structure
Environment Variables
The env
object provides access to essential environment variables.
const customAppInstanceId = sdk.env.customAppInstanceId;
customAppInstanceId
: The unique identifier for the custom application instance.
Utilities
The utils
object contains utility functions for common operations.
// Serialize an object
const serializedObject = sdk.utils.serialize({ key: "value" });
// Deserialize a string back to an object
const deserializedObject = sdk.utils.deserialize(serializedObject);
// Construct an absolute path string
const pathString = sdk.utils.absolutePathString("newPath");
serialize(obj)
: Serializes a JavaScript object into a string.deserialize(obj)
: Deserializes a string back into a JavaScript object.absolutePathString(name)
: Constructs an absolute path string based on the current path and the provided name.
Method Calls
The method
object allows you to make asynchronous method calls within the MonoChat platform.
const response = await sdk.method.callAsync("methodName", { key: "value" });
method
: The name of the Meteor method you want to call.payload
: The data to be sent as the payload for the method call.
callAsync(method, payload)
: Asynchronously calls a Meteor method with the specified payload.
Collection Methods
The collection
function provides access to predefined collections within the MonoChat platform.
const myCollection = sdk.collection("myCollectionKey");
const items = await myCollection.listAsync({ filtering: {}, sorting: {} });
const item = await myCollection.showAsync("itemId");
key
: The key that identifies the collection.
listAsync(options)
:
options
: An object containing options for filtering and sorting the collection items.
showAsync(_id)
:
_id
: The unique identifier of the item you want to retrieve.
listAsync(options)
: Asynchronously lists items in the collection.showAsync(_id)
: Asynchronously retrieves a specific item by its ID.
Custom Collections
Manage custom collections using the customCollection
function.
const myCustomCollection = sdk.customCollection("myCollectionName");
const items = await myCustomCollection.listAsync({
filtering: {},
sorting: {},
});
const item = await myCustomCollection.showAsync("itemId");
const newItem = await myCustomCollection.createAsync({ key: "value" });
await myCustomCollection.updateAsync("itemId", { key: "newValue" });
await myCustomCollection.deleteAsync("itemId");
name
: The name of the custom collection.useAppStorage
: A boolean indicating whether to use app-specific storage. Default istrue
.
listAsync(options)
:
options
: An object containing options for filtering and sorting the custom collection items.
showAsync(_id)
:
_id
: The unique identifier of the item you want to retrieve.
createAsync(payload)
:
payload
: The data for the new item you want to create.
updateAsync(_id, payload)
:
_id
: The unique identifier of the item you want to update.payload
: The updated data for the item.
deleteAsync(_id)
:
_id
: The unique identifier of the item you want to delete.
listAsync(options)
: Asynchronously lists items in the custom collection.showAsync(_id)
: Asynchronously retrieves a specific item by its ID.createAsync(payload)
: Asynchronously creates a new item in the custom collection.updateAsync(_id, payload)
: Asynchronously updates an existing item by its ID.deleteAsync(_id)
: Asynchronously deletes an item by its ID.
Store Operations
Manage key-value storage with the store
function.
// Create a key-value pair
await sdk
.store({ useAppStorage: true })
.createAsync({ scope: "tenant", key: "myKey", value: "myValue" });
// Retrieve a key-value pair
const result = await sdk
.store({ useAppStorage: true })
.showAsync({ scope: "tenant", key: "myKey" });
useAppStorage
: A boolean indicating whether to use app-specific storage. Default isfalse
.
createAsync({ scope, key, value })
:
scope
: The scope in which the key-value pair exists. Default istenant
.key
: The key for the value you want to store.value
: The value you want to store.
showAsync({ scope, key })
:
scope
: The scope in which the key-value pair exists. Default istenant
.key
: The key for the value you want to retrieve.
createAsync({ scope = 'tenant', key, value })
: Asynchronously creates a key-value pair in the store.showAsync({ scope = 'tenant', key })
: Asynchronously retrieves a key-value pair from the store.
Modules
Wallet Module
The wallet
module provides methods to manage customer wallets and transactions.
// Retrieve balances for a customer
const balances = await sdk.modules.wallet.balances.showAsync({
customerUserId: "userId",
});
// Credit a customer's wallet
await sdk.modules.wallet.transactions.creditAsync({
customerUserId: "userId",
amount: 100,
currencyIsoCode: "USD",
});
// Debit a customer's wallet
await sdk.modules.wallet.transactions.debitAsync({
customerUserId: "userId",
amount: 50,
currencyIsoCode: "USD",
});
// Create a payment system order
const order =
await sdk.modules.wallet.transactions.createPaymentSystemOrderAsync({
paymentSystemId: "paymentSystemId",
customerUserId: "userId",
amount: 150,
currencyIsoCode: "USD",
});
balances.showAsync({ customerUserId })
:
customerUserId
: The unique identifier of the customer whose balances you want to retrieve.
transactions.creditAsync({ customerUserId, amount, currencyIsoCode, description = null })
:
customerUserId
: The unique identifier of the customer whose wallet you want to credit.amount
: The amount to credit to the customer's wallet.currencyIsoCode
: The ISO code of the currency for the transaction.description
: An optional description for the transaction. Default is'Credit'
.
transactions.debitAsync({ customerUserId, amount, currencyIsoCode, description = null })
:
customerUserId
: The unique identifier of the customer whose wallet you want to debit.amount
: The amount to debit from the customer's wallet.currencyIsoCode
: The ISO code of the currency for the transaction.description
: An optional description for the transaction. Default is'Debit'
.
transactions.createPaymentSystemOrderAsync({ paymentSystemId, customerUserId = payload.userId, amount, currencyIsoCode, details = {}, payload = null })
:
paymentSystemId
: The unique identifier of the payment system.customerUserId
: The unique identifier of the customer for whom the order is being created. Default ispayload.userId
.amount
: The amount for the order.currencyIsoCode
: The ISO code of the currency for the order.details
: An object containing additional details about the order.payload
: An optional payload for the order.
balances.showAsync({ customerUserId })
: Retrieves the balance for a specific customer in various currencies.transactions.creditAsync({ customerUserId, amount, currencyIsoCode, description = null })
: Credits a specific amount to the customer’s wallet.transactions.debitAsync({ customerUserId, amount, currencyIsoCode, description = null })
: Debits a specific amount from the
customer’s wallet.
transactions.createPaymentSystemOrderAsync({ paymentSystemId, customerUserId, amount, currencyIsoCode, details = {}, payload = null })
: Creates a payment system order and returns its ID and URL.
Automation Module
The automation
module enables interaction with AI models and performing automated tasks.
// List available AI models
const models = sdk.modules.automation.ai.models.list();
// Generate AI inference
const inference = await sdk.modules.automation.ai.inferences.generateAsync({
input: "Hello",
modelSlug: "modelSlug",
});
// Chat with AI
const chatResponse = await sdk.modules.automation.ai.inferences.chatAsync({
modelSlug: "chatModel",
scopes: [],
});
// Transcribe a file using AI
const transcript = await sdk.modules.automation.ai.inferences.transcriptAsync({
modelSlug: "transcriptionModel",
fileUrl: "https://example.com/audiofile.mp3",
prompt: "Transcribe this file",
});
models.list()
: No parameters.
inferences.generateAsync({ input, modelSlug, scopes })
:
input
: The input text for the AI model to process.modelSlug
: The slug identifier of the AI model you want to use.scopes
: An optional array of scopes for the AI inference.
inferences.chatAsync({ modelSlug, scopes, toolReferences, toolChoiceReference })
:
modelSlug
: The slug identifier of the AI model you want to use for chat.scopes
: An optional array of scopes for the chat inference.toolReferences
: Optional references to tools that may be used in the chat.toolChoiceReference
: An optional reference to a specific tool choice.
inferences.transcriptAsync({ modelSlug, scopes, fileUrl, prompt })
:
modelSlug
: The slug identifier of the AI model you want to use for transcription.scopes
: An optional array of scopes for the transcription inference.fileUrl
: The URL of the file you want to transcribe.prompt
: An optional prompt to guide the transcription.
models.list()
: Lists available AI models.inferences.generateAsync({ input, modelSlug, scopes })
: Generates an AI inference based on the input.inferences.chatAsync({ modelSlug, scopes, toolReferences, toolChoiceReference })
: Initiates a chat-based AI inference.inferences.transcriptAsync({ modelSlug, scopes, fileUrl, prompt })
: Transcribes audio or video files using AI.
Payment Module
The payment
module provides methods to interact with payment systems.
// Retrieve the default payment system
const defaultPaymentSystem =
await sdk.modules.payment.paymentSystems.getDefaultPaymentSystemAsync();
paymentSystems.getDefaultPaymentSystemAsync()
: No parameters.
paymentSystems.getDefaultPaymentSystemAsync()
: Retrieves the default payment system for the tenant.
Chat Module
The chat
module allows interaction with chat channels and sessions.
const chatSession = sdk.modules.chat.sessions.messages({
sessionId: "sessionId",
});
// Send a text message to a chat session
await chatSession.sendTextAsync({
text: "Hello, how can I help you?",
});
// Send choices to a chat session
await chatSession.sendChoicesAsync({
text: "Please choose an option:",
sections: [
{
title: "Options",
items: [
{ title: "Option 1", description: "Description 1" },
{ title: "Option 2", description: "Description 2" },
],
},
],
});
messages({ sessionId })
:
sessionId
: The unique identifier of the chat session.
sendTextAsync({ text, previewUrl, isWhisper })
:
text
: The text message to send.previewUrl
: An optional URL for a preview image or link.isWhisper
: A boolean indicating whether the message should be whispered. Default isfalse
.
sendChoicesAsync({ text, header, footer, sections, button, isWhisper })
:
text
: The text message to send.header
: An optional header for the choices.footer
: An optional footer for the choices.sections
: An array of sections, each containing a list of choices.button
: An optional button label.isWhisper
: A boolean indicating whether the message should be whispered. Default isfalse
.
messages({ sessionId })
: Initializes a message handler for the specified session ID.sendTextAsync({ text, previewUrl, isWhisper })
: Sends a text message to the specified chat session.sendChoicesAsync({ text, header, footer, sections, button, isWhisper })
: Sends a choice-based message to the specified chat session.
Libraries
The lib
object provides access to third-party libraries integrated with MonoChat.
// Use OpenAI for advanced AI capabilities
const response = await sdk.lib.OpenAI.callModel({
model: "text-davinci-003",
prompt: "Hello, AI!",
});
// Use Algolia for search functionalities
const results = await sdk.lib.Algolia.search({
indexName: "products",
query: "laptop",
});
callModel({ model, prompt })
:
model
: The model you want to call from OpenAI.prompt
: The prompt you want to send to the model.
search({ indexName, query })
:
indexName
: The name of the Algolia index to search.query
: The search query.
OpenAI
: Access OpenAI features.Algolia
: Access Algolia search features.