import * as adamant_api from 'adamant-api'; import { CustomLogger, LogLevel, ChatMessageTransaction, MessageType, AdamantApi, AdamantAddress, KeyPair, AddressOrPublicKeyObject, TransactionQuery, ChatroomsOptions } from 'adamant-api'; import * as adamant_api_dist_helpers_validator from 'adamant-api/dist/helpers/validator'; interface ApiOptions { /** * List of ADAMANT nodes to connect to, bot will automaticly choose the fastest one. */ nodes: string[]; enableSSL?: boolean; logger?: CustomLogger; logLevel?: LogLevel; checkHealthAtStartup?: boolean; timeout?: number; } type DecodedMessageTransaction = Omit & { asset: { chat: { message: string; type: MessageType; }; }; }; declare class Api { private passphrase; api: AdamantApi; address: AdamantAddress; publicKey: string; keyPair: KeyPair; options: ApiOptions; constructor(passphrase: string, options: ApiOptions); listen(onNewMessage: (transaction: ChatMessageTransaction) => void): void; decode(transaction: ChatMessageTransaction): Promise<{ success: false; error: string; } | { success: true; decodedTransaction: DecodedMessageTransaction; }>; getAccountBalance(address?: `U${string}`): Promise>; getAccountInfo(options?: AddressOrPublicKeyObject): Promise>; getChatMessages(address: string, options?: TransactionQuery): Promise>; sendTokens(addressOrPublicKey: string, amount: number, isAmountInADM?: boolean): Promise<{ success: boolean; error: string; } | adamant_api_dist_helpers_validator.AdamantApiResult>; sendMessage(addressOrPublicKey: string, message: string, messageType?: MessageType, amount?: number, isADM?: boolean): Promise<{ success: boolean; error: string; } | (Omit & { success: true; }) | { success: boolean; errorMessage: string; }>; } /** * @nav BotFactoryError */ declare class BotFactoryError extends Error { transaction?: DecodedMessageTransaction; constructor(message: string, transaction: DecodedMessageTransaction); } /** * @nav User */ interface UserData { address: AdamantAddress; publicKey: string; } /** * @nav User */ declare class User { private api; address: AdamantAddress; publicKey: string; constructor(api: Api, options: UserData); /** * Returns the user's account information. */ info(): Promise>; /** * Gets the user's account balance. */ balance(): Promise>; /** * Returns list of the chat messages between the bot and the user. */ messages(): Promise>; /** * Sends the given amount of tokens to the user. */ transfer(amount: number, isADM?: boolean): Promise<{ success: boolean; error: string; } | adamant_api_dist_helpers_validator.AdamantApiResult>; /** * Sends a message to the user */ reply(message: string, messageType?: MessageType, amount?: number, isADM?: boolean): Promise<{ success: boolean; error: string; } | (Omit & { success: true; }) | { success: boolean; errorMessage: string; }>; } type MatchPattern = string | RegExp | MatchPattern[]; type MatchType = 'command' | 'text'; declare class Layer { type: MatchType; pattern: MatchPattern; private handlers; constructor(type: MatchType, pattern: MatchPattern, handlers: RouterHandler[]); handle(user: User, transaction: DecodedMessageTransaction, done: (error?: string) => void): void; match(transaction: DecodedMessageTransaction): boolean; } /** * You can provide multiple callback functions that behave like middleware to handle a request. * The only exception is that these callbacks might invoke next() to bypass the remaining route callbacks. * You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if * there’s no reason to proceed with the current route. * * @example * * In the following example, the `/stats` command will only execute handler if the message was sent by an admin. * * ```js * bot.use((usr, tx, next) => { * if (isAdmin(usr)) { * next() * } * }) * * bot.command('stats', () => { * // ... * }) * ``` * * @nav Router */ interface RouterHandler { (usr: User, tx: DecodedMessageTransaction, next: (error?: string) => void): void; } /** * @nav Router */ declare class Router { protected stack: Array; constructor(); /** * Registers some middlewares or routers. */ use(...handlers: (Layer | RouterHandler)[]): this; /** * Registers some middleware(s) that will only be executed when the message contains some text specified by `pattern` * * @example * * This route will match butterfly and dragonfly, but not butterflyman, dragonflyman, and so on. * * ```js * bot.hears(/.*fly$/, () => {}) * ``` */ hears(pattern: RegExp, ...handlers: RouterHandler[]): this; /** * Registers some middleware that will only be executed when the bot received a message that starts with the specifid command. * * @details * * The commands must meet the following criteria: * - The command must start with a forward slash character (/). * - The command must be composed of one or more uppercase or lowercase letters. * - The letters in the command may be separated by one underscore (_). * - The command must end with a single letter. * * @example * * The following route will match `/hello` command * * ```js * bot.command('hello', () => {}) * ``` */ command(name: string, ...handlers: RouterHandler[]): this; private push; /** * Handles a new chat message transaction sent by a user. */ handle(usr: User, tx: DecodedMessageTransaction, done: (error?: string) => void): void; } /** * @nav Bot */ type ErrorHandler = (error: BotFactoryError) => void; /** * @nav Bot */ declare class Bot extends Router { private api; /** * Error handler set via `Bot.catch()`. */ handleError: ErrorHandler; constructor(passphrase: string, options: ApiOptions); /** * Creates copy of the bot with the same handlers */ static extends(bot: Bot): (passphrase: string, options: ApiOptions) => Bot; /** * Creates a user within bot's api. */ createUser(user: UserData): User; /** * Bot's ADAMANT address. */ get address(): `U${string}`; /** * Bot's public key. */ get publicKey(): string; /** * Bot's public/private keys. */ get keyPair(): adamant_api.KeyPair; /** * Node url that bot is connected to right now. */ get node(): string; /** * Starts the webhook client and listens for new messages. */ start(callback?: () => void): void; /** * Sets the bot's error handler. * * @param errorHandler Function that will be called when error was thrown. */ catch(errorHandler: ErrorHandler): void; /** * Decodes and processes a new transaction, can be used for * testing and handling transactions from other sources. * * @param transaction Encrypted transaction object to process. */ handleTransaction(transaction: ChatMessageTransaction): Promise; } /** * Creates new bot instance within given passphrase and options. * * @param passphrase Bot's account passphrase. * @param options Bot options, including list of nodes to connect. * * @nav Bot */ declare function createBot(passphrase: string, options: ApiOptions): Bot; /** * Creates a new bot instance with the same middlewares as a given bot. * * @details * Returns `createBot`-like function. * * @nav Bot */ declare function copyBot(bot: Bot): (passphrase: string, options: ApiOptions) => Bot; export { Api, ApiOptions, Bot, BotFactoryError, DecodedMessageTransaction, ErrorHandler, Layer, MatchPattern, MatchType, Router, RouterHandler, User, UserData, copyBot, createBot };