The plugin system is still under development, and the functions may change, but the documentation will be updated as much as possible.
A plugin system is a system that allows users to customize functions. Users can add new functions through the plugin system. The definition of a plugin is a JSON file, and users can call the plugin by binding the corresponding command.
/**
* TemplateInputType: The type of input data, converting the data input from Telegram into the corresponding data type
* json: JSON format
* space-separated: Space-separated string
* comma-separated: Comma-separated string
* text: Text, not split (default value)
*/
export type TemplateInputType = 'json' | 'space-separated' | 'comma-separated' | 'text';
/**
* TemplateBodyType: The type of the request body
* json: JSON format, at this time the value of the content field should be an object, where the key is a fixed value, and the value supports interpolation
* form: Form format, at this time the value of the content field should be an object, where the key is a fixed value, and the value supports interpolation
* text: Text format, at this time the value of the content field should be a string, supporting interpolation
*/
export type TemplateBodyType = 'json' | 'form' | 'text';
/**
* TemplateResponseType: The type of response body
* json: JSON format, at this time the response body will be parsed into JSON format and passed to the next template for rendering
* text: Text format, at this time the response body will be parsed into text format and passed to the next template for rendering
*/
export type TemplateResponseType = 'json' | 'text';
/**
* TemplateOutputType: The type of output data
* text: Text format, sends the rendering result as plain text to Telegram
* image: Image format, sends the rendering result as an image URL to Telegram
* html: HTML format, sends the rendering result in HTML format to Telegram
* markdown: Markdown format, sends the rendering result in Markdown format to Telegram
*/
export type TemplateOutputType = 'text' | 'image' | 'html' | 'markdown';
export interface RequestTemplate {
url: string; // Required, supports interpolation
method: string; // Required, fixed value
headers: { [key: string]: string }; // Optional, Key is a fixed value, Value supports interpolation.
input: {
type: TemplateInputType;
};
query: { [key: string]: string }; // Optional, Key is a fixed value, Value supports interpolation.
body: {
type: TemplateBodyType;
content: { [key: string]: string } | string; // When content is an object, Key is a fixed value, and Value supports interpolation. When content is a string, it supports interpolation.
};
response: {
content: { // Required, handling when the request is successful.
input_type: TemplateResponseType;
output_type: TemplateOutputType;
output: string;
};
error: { // Required, handling when the request fails.
input_type: TemplateResponseType;
output_type: TemplateOutputType;
output: string;
};
};
}
For example, define the following variables in the environment variables:
PLUGIN_COMMAND_dns = "https://raw.githubusercontent.com/TBXark/ChatGPT-Telegram-Workers/dev/plugins/dns.json"
PLUGIN_DESCRIPTION_dns = "DNS query /dns <type> <domain>"
Then enter /dns A www.baidu.com
in the command line to call the plugin.
Where PLUGIN_COMMAND_dns
is the address of the plugin's json file, and PLUGIN_DESCRIPTION_dns
is the description of the plugin.
PLUGIN_COMMAND_dns
can be a complete json or a url of a json.
If you want to bind plugin commands to the menu of Telegram, you can add the following environment variable PLUGIN_SCOPE_dns = "all_private_chats,all_group_chats,all_chat_administrators"
, so that the plugin will take effect in all private chats, group chats and groups.
You can test the interpolation template in the interpolation template test page.
<b>{{title}}</b>
<b>
{{#each item in items}}
{{#each:item i in item}}
{{ i.value }}
{{#if i.enable}}
{{#if:sub i.subEnable}}
sub enable
{{#else:sub}}
sub disable
{{/if:sub}}
{{#else}}
disable
{{/if}}
{{/each:item}}
{{/each}}
</b>
{{title}}
represents the variable of interpolation template, supporting key path and array subscript.{{#each item in items}}
represents traversing the array items, item is each element of the array, and must include the ending{{/each}}
.{{#each:item i in item}}
Nested traversal operations need to add aliases to the traversal, and the ending also needs to correspond to the alias{{/each:item}}
.{{#if i.enable}}
represents conditional judgment. The judgment condition does not support expressions and can only judge non-empty and non-zero. It must include the ending{{/if}}
.{{#else}}
represents the negative branch of conditional judgment.{{#if:sub i.subEnable}}
Nested conditional judgment needs to add aliases to the condition, and the ending also needs to correspond to the alias{{/if:sub}}
.- There should be no spaces in the interpolation or expression in
{{}}
, otherwise it will be parsed as a string. For example, this is an incorrect interpolation{{ title }}
. {{.}}
represents the current data, which can be used in#each
or globally.
The default data structure passed into interpolation is as follows:
{
"DATA": [],
"ENV": {}
}
- Among them,
DATA
is the data input by the user, and the data structure of DATA varies according to differentTemplateInputType
. ENV
is an environment variable that users can use to pass in data. The plugin's environment variables are isolated from the global environment variables and require different syntax to pass in.
You can save the token required for the request in the plugin environment variables. The plugin environment variables must start with PLUGIN_ENV_
, for example:
PLUGIN_ENV_access_token = "xxxx"
It will be parsed as.
{
"DATA": [],
"ENV": {
"access_token": "xxxx"
}
}