Skip to content

bots in tribes

Evan Feenstra edited this page Sep 22, 2020 · 4 revisions

problem

Since message text is encrypted on client side, in app we need to calculate the price that might be attached to a message meant for a Sphinx Bot.

solution

In the /tribe/{uuid} call (when you open a chat), there is a new field called "bots". It is a JSON string, that parses out into an array of BotJSON objects:

interface BotJSON {
  prefix: string,
  price: number,
  commands: BotCommand[] | null,
}
interface BotCommand {
  command: string,
  price: number,
  min_price: number,
  max_price: number,
  price_index: number,
  admin_only: boolean,
}

Before sending a message from app, we need to calculate the price to attach based on this JSON. Here is a JS implementation: https://github.com/stakwork/sphinx-android/blob/master/src/store/hooks/chat.ts

bot options

Loop through the bots array and check the following:

  • Does the text start with the prefix?
  • If the bot has a defined price, that is the price to attach for ALL commands.

bot command options

The bot might also have a commands array, that will vary the price for different commands. If so, loop through the bot.commands

  • Split the message text on " " (needs at least 2 words)
  • If the command is * that means all commands match
  • If command is defined, then check that it equals the second item in the word array. (For example, if the text is "/btc quote", then check against "quote")
  • If price is defined, then that is the price for this command
  • If price_index is defined, then we need to parse the amount from the message text iteself:
    • get the amount by parsing an integer from the word array[price_index]
    • if the amount < min_price return a "Amount too low" error
    • if the amount > max_price return a "Amount too high" error
    • else the amount in the message is the price to attach
Clone this wiki locally