{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://telebotbuilder.com/blueprints/spec-v1.json",
  "title": "TeleBotBuilder blueprint, format version 1",
  "description": "One Telegram bot flow as text. Paste it into TeleBotBuilder with Import blueprint. The flow starts at the first block (or at \"start\"). Edges are written on the block they leave: \"next\", \"branches\", or \"next\" on each button or menu option. Variables are written {{name}} in text fields. Docs: https://telebotbuilder.com/blueprints/",
  "type": "object",
  "required": [
    "telebot",
    "name",
    "trigger",
    "blocks"
  ],
  "additionalProperties": false,
  "properties": {
    "telebot": {
      "const": 1,
      "description": "The format version."
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100
    },
    "trigger": {
      "$ref": "#/$defs/trigger"
    },
    "start": {
      "$ref": "#/$defs/blockId",
      "description": "The first block. Defaults to the first item of blocks."
    },
    "collections": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/collection"
      },
      "description": "Saved lists the flow uses. At most 3 new ones per import (Pro)."
    },
    "variables": {
      "type": "array",
      "maxItems": 200,
      "items": {
        "$ref": "#/$defs/variableName"
      },
      "description": "Names the flow reads but another flow sets."
    },
    "blocks": {
      "type": "array",
      "minItems": 1,
      "maxItems": 99,
      "items": {
        "$ref": "#/$defs/block"
      }
    },
    "notes": {
      "type": "string",
      "maxLength": 2000
    }
  },
  "$defs": {
    "blockId": {
      "type": "string",
      "pattern": "^[a-z0-9_]{1,50}$",
      "not": {
        "const": "trigger"
      }
    },
    "pos": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "items": {
        "type": "number",
        "minimum": -100000,
        "maximum": 100000
      }
    },
    "variableName": {
      "type": "string",
      "pattern": "^[A-Za-z_]\\w*$"
    },
    "trigger": {
      "type": "object",
      "required": [
        "type"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "enum": [
            "start",
            "command",
            "keyword",
            "default",
            "start_payload",
            "any_message",
            "menu"
          ]
        },
        "command": {
          "type": "string",
          "pattern": "^/[a-z0-9_]{1,31}$",
          "not": {
            "enum": [
              "/start"
            ]
          }
        },
        "keyword": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "pos": {
          "$ref": "#/$defs/pos"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "command"
              }
            }
          },
          "then": {
            "required": [
              "command"
            ],
            "not": {
              "required": [
                "keyword"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "keyword"
              }
            }
          },
          "then": {
            "required": [
              "keyword"
            ],
            "not": {
              "required": [
                "command"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "not": {
                  "enum": [
                    "command",
                    "keyword"
                  ]
                }
              }
            }
          },
          "then": {
            "not": {
              "anyOf": [
                {
                  "required": [
                    "command"
                  ]
                },
                {
                  "required": [
                    "keyword"
                  ]
                }
              ]
            }
          }
        }
      ]
    },
    "collection": {
      "type": "object",
      "required": [
        "name",
        "scope",
        "columns"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "scope": {
          "enum": [
            "per_user",
            "shared"
          ]
        },
        "columns": {
          "type": "array",
          "maxItems": 32,
          "items": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9_]{0,63}$"
          }
        }
      }
    },
    "block": {
      "oneOf": [
        {
          "$ref": "#/$defs/block_text"
        },
        {
          "$ref": "#/$defs/block_buttons"
        },
        {
          "$ref": "#/$defs/block_menu"
        },
        {
          "$ref": "#/$defs/block_image"
        },
        {
          "$ref": "#/$defs/block_link"
        },
        {
          "$ref": "#/$defs/block_delay"
        },
        {
          "$ref": "#/$defs/block_condition"
        },
        {
          "$ref": "#/$defs/block_loop"
        },
        {
          "$ref": "#/$defs/block_api"
        },
        {
          "$ref": "#/$defs/block_variable"
        },
        {
          "$ref": "#/$defs/block_storage_save"
        },
        {
          "$ref": "#/$defs/block_storage_find"
        },
        {
          "$ref": "#/$defs/block_storage_find_many"
        },
        {
          "$ref": "#/$defs/block_storage_update"
        },
        {
          "$ref": "#/$defs/block_storage_delete"
        },
        {
          "$ref": "#/$defs/block_schedule_reminder"
        },
        {
          "$ref": "#/$defs/block_stars_payment"
        }
      ]
    },
    "block_text": {
      "type": "object",
      "description": "Sends a message; can wait for the reply and store it.",
      "required": [
        "id",
        "type",
        "text"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "text"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "text": {
          "type": "string"
        },
        "waitForReply": {
          "type": "boolean",
          "default": false
        },
        "replyVar": {
          "type": "string",
          "default": "user_reply"
        },
        "showBack": {
          "type": "boolean",
          "default": false
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_buttons": {
      "type": "object",
      "description": "Sends a message with inline buttons; each button is an output port.",
      "required": [
        "id",
        "type",
        "text",
        "buttons"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "buttons"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "text": {
          "type": "string"
        },
        "buttons": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "text": {
                "type": "string"
              },
              "next": {
                "$ref": "#/$defs/blockId",
                "description": "The block this item leads to."
              }
            },
            "required": [
              "text"
            ]
          },
          "minItems": 1,
          "maxItems": 8
        },
        "showBack": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "block_menu": {
      "type": "object",
      "description": "Shows a reply keyboard of options; each option is an output port.",
      "required": [
        "id",
        "type",
        "options"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "menu"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "text": {
          "type": "string",
          "default": "Choose an option:"
        },
        "options": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "text": {
                "type": "string"
              },
              "next": {
                "$ref": "#/$defs/blockId",
                "description": "The block this item leads to."
              }
            },
            "required": [
              "text"
            ]
          },
          "minItems": 1,
          "maxItems": 6
        },
        "showBack": {
          "type": "boolean",
          "default": false
        }
      }
    },
    "block_image": {
      "type": "object",
      "description": "Sends a photo from a URL, with an optional caption.",
      "required": [
        "id",
        "type",
        "url"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "image"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "url": {
          "type": "string"
        },
        "caption": {
          "type": "string"
        },
        "showBack": {
          "type": "boolean",
          "default": false
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_link": {
      "type": "object",
      "description": "Sends a message with one button that opens a URL.",
      "required": [
        "id",
        "type",
        "text",
        "label",
        "url"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "link"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "text": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "url": {
          "type": "string"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_delay": {
      "type": "object",
      "description": "Waits a few seconds before the next block.",
      "required": [
        "id",
        "type"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "delay"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "seconds": {
          "type": "number",
          "default": 5
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_condition": {
      "type": "object",
      "description": "Compares a variable with a value and branches true/false. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "varName"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "condition"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "varName": {
          "type": "string"
        },
        "operator": {
          "enum": [
            "==",
            "!=",
            ">",
            "<",
            "contains",
            "matches"
          ],
          "default": "=="
        },
        "value": {
          "type": "string"
        },
        "branches": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "true": {
              "$ref": "#/$defs/blockId"
            },
            "false": {
              "$ref": "#/$defs/blockId"
            }
          }
        }
      }
    },
    "block_loop": {
      "type": "object",
      "description": "Repeats its body a number of times, or until a condition holds. Needs the Pro plan.",
      "required": [
        "id",
        "type"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "loop"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "loopType": {
          "enum": [
            "count",
            "condition"
          ],
          "default": "count"
        },
        "count": {
          "type": "integer",
          "default": 3
        },
        "condVar": {
          "type": "string"
        },
        "condOp": {
          "enum": [
            "==",
            "!=",
            ">",
            "<",
            "contains",
            "matches"
          ],
          "default": "=="
        },
        "condVal": {
          "type": "string"
        },
        "branches": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "body": {
              "$ref": "#/$defs/blockId"
            },
            "done": {
              "$ref": "#/$defs/blockId"
            }
          }
        }
      }
    },
    "block_api": {
      "type": "object",
      "description": "Calls an external HTTP API and stores the response in variables. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "url"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "api"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "method": {
          "enum": [
            "GET",
            "POST",
            "PUT",
            "DELETE"
          ],
          "default": "GET"
        },
        "url": {
          "type": "string"
        },
        "headers": {
          "type": [
            "object",
            "string"
          ],
          "description": "An object, or the same object written as JSON text.",
          "default": "{}"
        },
        "body": {
          "type": [
            "object",
            "string"
          ],
          "description": "An object, or the same object written as JSON text.",
          "default": "{}"
        },
        "responseVar": {
          "type": "string",
          "default": "api_result"
        },
        "responsePath": {
          "type": "string"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_variable": {
      "type": "object",
      "description": "Sets a variable; spaced arithmetic like {{n}} + 1 is calculated. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "varName"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "variable"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "varName": {
          "type": "string"
        },
        "value": {
          "type": "string"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_storage_save": {
      "type": "object",
      "description": "Saves a new row to a collection. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "collection"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "storage_save"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "collection": {
          "type": "string",
          "minLength": 1,
          "description": "The name of a collection (a saved list)."
        },
        "values": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          }
        },
        "resultVar": {
          "type": "string",
          "default": "saved_row"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_storage_find": {
      "type": "object",
      "description": "Finds one row; branches found/not_found. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "collection"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "storage_find"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "collection": {
          "type": "string",
          "minLength": 1,
          "description": "The name of a collection (a saved list)."
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "operator": {
                "enum": [
                  "==",
                  "!=",
                  "contains"
                ],
                "default": "=="
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          }
        },
        "resultVar": {
          "type": "string",
          "default": "row"
        },
        "branches": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "found": {
              "$ref": "#/$defs/blockId"
            },
            "not_found": {
              "$ref": "#/$defs/blockId"
            }
          }
        }
      }
    },
    "block_storage_find_many": {
      "type": "object",
      "description": "Finds matching rows; sets a count; branches found/not_found. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "collection"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "storage_find_many"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "collection": {
          "type": "string",
          "minLength": 1,
          "description": "The name of a collection (a saved list)."
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "operator": {
                "enum": [
                  "==",
                  "!=",
                  "contains"
                ],
                "default": "=="
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          }
        },
        "resultVar": {
          "type": "string",
          "default": "rows"
        },
        "branches": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "found": {
              "$ref": "#/$defs/blockId"
            },
            "not_found": {
              "$ref": "#/$defs/blockId"
            }
          }
        }
      }
    },
    "block_storage_update": {
      "type": "object",
      "description": "Changes the listed columns of matching rows. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "collection",
        "values"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "storage_update"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "collection": {
          "type": "string",
          "minLength": 1,
          "description": "The name of a collection (a saved list)."
        },
        "values": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          },
          "minItems": 1
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "operator": {
                "enum": [
                  "==",
                  "!=",
                  "contains"
                ],
                "default": "=="
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          }
        },
        "resultVar": {
          "type": "string",
          "default": "updated"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_storage_delete": {
      "type": "object",
      "description": "Deletes matching rows. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "collection"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "storage_delete"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "collection": {
          "type": "string",
          "minLength": 1,
          "description": "The name of a collection (a saved list)."
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "column": {
                "type": "string"
              },
              "operator": {
                "enum": [
                  "==",
                  "!=",
                  "contains"
                ],
                "default": "=="
              },
              "value": {
                "type": "string"
              }
            },
            "required": [
              "column"
            ]
          }
        },
        "resultVar": {
          "type": "string",
          "default": "deleted"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_schedule_reminder": {
      "type": "object",
      "description": "Schedules a message, or a jump to another block, for later. Needs the Pro plan.",
      "required": [
        "id",
        "type",
        "delayValue"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "schedule_reminder"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "delayValue": {
          "type": "string"
        },
        "delayUnit": {
          "enum": [
            "minutes",
            "hours",
            "days"
          ],
          "default": "minutes"
        },
        "text": {
          "type": "string"
        },
        "target": {
          "type": "string",
          "description": "The id of the block to jump to later, or empty."
        },
        "resultVar": {
          "type": "string",
          "default": "reminder"
        },
        "next": {
          "$ref": "#/$defs/blockId",
          "description": "The block that runs next. Leave it out to end here."
        }
      }
    },
    "block_stars_payment": {
      "type": "object",
      "description": "Sells a digital product for Telegram Stars; branches paid/cancelled.",
      "required": [
        "id",
        "type",
        "title",
        "description",
        "branches"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "$ref": "#/$defs/blockId"
        },
        "type": {
          "const": "stars_payment"
        },
        "pos": {
          "$ref": "#/$defs/pos"
        },
        "title": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "price": {
          "type": "integer",
          "default": 50
        },
        "photoUrl": {
          "type": "string"
        },
        "productKey": {
          "type": "string"
        },
        "resultVar": {
          "type": "string",
          "default": "payment"
        },
        "branches": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "paid": {
              "$ref": "#/$defs/blockId"
            },
            "cancelled": {
              "$ref": "#/$defs/blockId"
            }
          },
          "required": [
            "paid",
            "cancelled"
          ]
        }
      }
    }
  }
}
