Skip to content

Commit

Permalink
Merge pull request TBXark#283 from TBXark/dev
Browse files Browse the repository at this point in the history
chore: 添加绑定自定义命令到菜单的功能
  • Loading branch information
TBXark authored Sep 6, 2024
2 parents 7b60366 + 8c99bc8 commit 4172345
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 256 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 39 additions & 12 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions doc/cn/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,22 @@ CUSTOM_COMMAND_cn2en = '/setenvs {"SYSTEM_INIT_MESSAGE": "你是一个翻译下

下面是一些自定义指令帮助信息例子

| 指令描述 | 描述 ||
|-----------------------------|------------------------------|-------------------------------------------------------------------------------------------------------------------|
| COMMAND_DESCRIPTION_azure | 切换AI提供商为Azure | `/setenvs {"AI_PROVIDER": "azure"}` |
| COMMAND_DESCRIPTION_workers | 切换AI提供商为Workers | `/setenvs {"AI_PROVIDER": "workers"}` |
| COMMAND_DESCRIPTION_gpt3 | 切换AI提供商为OpenAI GPT-3.5 Turbo | `/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-3.5-turbo"}` |
| COMMAND_DESCRIPTION_gpt4 | 切换AI提供商为OpenAI GPT-4 | `/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-4"}` |
| COMMAND_DESCRIPTION_cn2en | 将对话内容翻译成英文 | `/setenvs {"SYSTEM_INIT_MESSAGE": "You are a translator. Please translate everything I say below into English."}` |
| 指令描述 | 描述 |
|-----------------------------|------------------------------|
| COMMAND_DESCRIPTION_azure | 切换AI提供商为Azure |
| COMMAND_DESCRIPTION_workers | 切换AI提供商为Workers |
| COMMAND_DESCRIPTION_gpt3 | 切换AI提供商为OpenAI GPT-3.5 Turbo |
| COMMAND_DESCRIPTION_gpt4 | 切换AI提供商为OpenAI GPT-4 |
| COMMAND_DESCRIPTION_cn2en | 将对话内容翻译成英文 |

如果你是用toml进行配置,可以使用下面的方式:

```toml
COMMAND_DESCRIPTION_azure = '/setenvs {"AI_PROVIDER": "azure"}'
COMMAND_DESCRIPTION_workers = '/setenvs {"AI_PROVIDER": "workers"}'
COMMAND_DESCRIPTION_gpt3 = '/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-3.5-turbo"}'
COMMAND_DESCRIPTION_gpt4 = '/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-4"}'
COMMAND_DESCRIPTION_cn2en = '/setenvs {"SYSTEM_INIT_MESSAGE": "You are a translator. Please translate everything I say below into English."}'
COMMAND_DESCRIPTION_azure = '切换AI提供商为Azure'
COMMAND_DESCRIPTION_workers = '切换AI提供商为Workers'
COMMAND_DESCRIPTION_gpt3 = '切换AI提供商为OpenAI GPT-3.5 Turbo'
COMMAND_DESCRIPTION_gpt4 = '切换AI提供商为OpenAI GPT-4'
COMMAND_DESCRIPTION_cn2en = '将对话内容翻译成英文'
```

如果你想将自定义命令绑定到telegram的菜单中,你可以添加如下环境变量`COMMAND_SCOPE_azure = "all_private_chats,all_group_chats,all_chat_administrators"`,这样插件就会在所有的私聊,群聊和群组中生效。
2 changes: 1 addition & 1 deletion doc/cn/PLATFORM.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

### 2. [Vercel](https://vercel.com/)

详情看[Vercel](VERCEL.md)。免费,无需域名,无需服务器。需要配置本地开发环境部署,不能通过复制粘贴部署。无存储服务,需要自己配置数据库。可以使用[Redis Cloud](https://redis.com)的免费redis。可以连接github自动部署,但是需要了解vercel的配置。
详情看[Vercel](VERCEL.md)。免费,无需域名,无需服务器。需要配置本地开发环境部署,不能通过复制粘贴部署。无存储服务,需要自己配置数据库。可以使用[UpStash Redis](https://upstash.com)的免费redis。可以连接github自动部署,但是需要了解vercel的配置。


### 3. Local
Expand Down
26 changes: 14 additions & 12 deletions doc/en/CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,22 @@ If you want to add help information for a custom command, you can use environmen

The following are some examples of custom command help information.

| Command | Description | Value |
|-----------------------------|--------------------------------------------------|-------------------------------------------------------------------------------------------------------------------|
| COMMAND_DESCRIPTION_azure | Switch AI provider to Azure. | `/setenvs {"AI_PROVIDER": "azure"}` |
| COMMAND_DESCRIPTION_workers | Switch AI provider to Workers | `/setenvs {"AI_PROVIDER": "workers"}` |
| COMMAND_DESCRIPTION_gpt3 | Switch AI provider to OpenAI GPT-3.5 Turbo. | `/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-3.5-turbo"}` |
| COMMAND_DESCRIPTION_gpt4 | Switch AI provider to OpenAI GPT-4. | `/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-4"}` |
| COMMAND_DESCRIPTION_cn2en | Translate the conversation content into English. | `/setenvs {"SYSTEM_INIT_MESSAGE": "You are a translator. Please translate everything I say below into English."}` |
| Command | Value |
|-----------------------------|--------------------------------------------------|
| COMMAND_DESCRIPTION_azure | Switch AI provider to Azure. |
| COMMAND_DESCRIPTION_workers | Switch AI provider to Workers |
| COMMAND_DESCRIPTION_gpt3 | Switch AI provider to OpenAI GPT-3.5 Turbo. |
| COMMAND_DESCRIPTION_gpt4 | Switch AI provider to OpenAI GPT-4. |
| COMMAND_DESCRIPTION_cn2en | Translate the conversation content into English. |

If you are using TOML for configuration, you can use the following method:

```toml
COMMAND_DESCRIPTION_azure = '/setenvs {"AI_PROVIDER": "azure"}'
COMMAND_DESCRIPTION_workers = '/setenvs {"AI_PROVIDER": "workers"}'
COMMAND_DESCRIPTION_gpt3 = '/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-3.5-turbo"}'
COMMAND_DESCRIPTION_gpt4 = '/setenvs {"AI_PROVIDER": "openai", "OPENAI_CHAT_MODEL": "gpt-4"}'
COMMAND_DESCRIPTION_cn2en = '/setenvs {"SYSTEM_INIT_MESSAGE": "You are a translator. Please translate everything I say below into English."}'
COMMAND_DESCRIPTION_azure = 'Switch AI provider to Azure.'
COMMAND_DESCRIPTION_workers = 'Switch AI provider to Workers'
COMMAND_DESCRIPTION_gpt3 = 'Switch AI provider to OpenAI GPT-3.5 Turbo.'
COMMAND_DESCRIPTION_gpt4 = 'Switch AI provider to OpenAI GPT-4.'
COMMAND_DESCRIPTION_cn2en = 'Translate the conversation content into English.'
```

If you want to bind custom commands to the menu of Telegram, you can add the following environment variable `COMMAND_SCOPE_azure = "all_private_chats,all_group_chats,all_chat_administrators"`, so that the plugin will take effect in all private chats, group chats and groups.
2 changes: 1 addition & 1 deletion doc/en/PLATFORM.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The easiest way, the deployment method supported by this project by default, see
### 2. [Vercel](https://vercel.com/)

See details at [Vercel](VERCEL.md). It is free, does not require a domain name or server. Deployment requires configuring the local development environment and cannot be done by copying and pasting. There is no storage service, so you need to configure your own database. You can use the free Redis from [Redis Cloud](https://redis.com). It can connect to GitHub for automatic deployment, but you need to understand Vercel's configuration.
See details at [Vercel](VERCEL.md). It is free, does not require a domain name or server. Deployment requires configuring the local development environment and cannot be done by copying and pasting. There is no storage service, so you need to configure your own database. You can use the free Redis from [UpStash Redis](https://upstash.com). It can connect to GitHub for automatic deployment, but you need to understand Vercel's configuration.

### 3. Local

Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"type": "module",
"version": "1.9.3",
"description": "The easiest and quickest way to deploy your own ChatGPT Telegram bot is to use a single file and simply copy and paste it. There is no need for any dependencies, local development environment configuration, domain names, or servers.",
"author": "TBXark",
"author": "tbxark <[email protected]>",
"license": "MIT",
"repository": "[email protected]:TBXark/ChatGPT-Telegram-Workers.git",
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -23,6 +24,7 @@
"build": "vite build",
"build:local": "BUILD_MODE=local vite build",
"build:docker": "npm run build:local && cd dist && docker build -t chatgpt-telegram-workers:latest .",
"build:dockerx": "npm run build:local && cd dist && docker build --platform linux/amd64,linux/arm64 -t tbxark/chatgpt-telegram-workers:latest --push .",
"build:vercel": "BUILD_MODE=vercel vite build",
"build:pack": "BUILD_MODE=pack vite build",
"deploy:dist": "wrangler deploy",
Expand All @@ -36,23 +38,23 @@
"wrangler": "wrangler"
},
"dependencies": {
"cloudflare-worker-adapter": "^1.3.2"
"cloudflare-worker-adapter": "^1.3.3"
},
"devDependencies": {
"@antfu/eslint-config": "^2.27.3",
"@antfu/eslint-config": "^3.0.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/node": "^22.5.1",
"@vercel/node": "^3.2.9",
"@types/node": "^22.5.3",
"@vercel/node": "^3.2.11",
"eslint": "^9.8.0",
"eslint-plugin-format": "^0.1.2",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-node-externals": "^7.1.3",
"telegram-bot-api-types": "^7.9.12",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
"vite": "^5.4.2",
"vite": "^5.4.3",
"vite-plugin-checker": "^0.7.2",
"vite-plugin-dts": "^4.0.3",
"wrangler": "^3.72.3"
"vite-plugin-dts": "^4.1.0",
"wrangler": "^3.74.0"
}
}
2 changes: 2 additions & 0 deletions plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ 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.


## Interpolation Template

Expand Down
4 changes: 3 additions & 1 deletion plugins/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ PLUGIN_DESCRIPTION_dns = "DNS查询 /dns <类型> <域名>"
然后在命令行中输入`/dns A www.baidu.com`即可调用插件

其中`PLUGIN_COMMAND_dns`是插件的json文件地址,`PLUGIN_DESCRIPTION_dns`是插件的描述。
`PLUGIN_COMMAND_dns`可以是完整的json也可以是一个json的url
`PLUGIN_COMMAND_dns`可以是完整的json也可以是一个json的url。

如果你想将插件命令绑定到telegram的菜单中,你可以添加如下环境变量`PLUGIN_SCOPE_dns = "all_private_chats,all_group_chats,all_chat_administrators"`,这样插件就会在所有的私聊,群聊和群组中生效。


## 插值模板
Expand Down
3 changes: 3 additions & 0 deletions plugins/dicten.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"url": "https://api.dictionaryapi.dev/api/v2/entries/en/{{DATA}}",
"method": "GET",
"input": {
"required": true
},
"response": {
"content": {
"input_type": "json",
Expand Down
3 changes: 2 additions & 1 deletion plugins/dns.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"accept": "application/dns-json"
},
"input": {
"type": "space-separated"
"type": "space-separated",
"required": true
},
"query": {
"type": "{{DATA[0]}}",
Expand Down
5 changes: 4 additions & 1 deletion src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Environment extends EnvironmentConfig {
this.mergeCommands(
'CUSTOM_COMMAND_',
'COMMAND_DESCRIPTION_',
'COMMAND_SCOPE_',
source,
this.CUSTOM_COMMAND,
);
Expand All @@ -88,6 +89,7 @@ class Environment extends EnvironmentConfig {
this.mergeCommands(
'PLUGIN_COMMAND_',
'PLUGIN_DESCRIPTION_',
'PLUGIN_SCOPE_',
source,
this.PLUGINS_COMMAND,
);
Expand Down Expand Up @@ -119,13 +121,14 @@ class Environment extends EnvironmentConfig {
this.I18N = loadI18n(this.LANGUAGE.toLowerCase());
}

private mergeCommands(prefix: string, descriptionPrefix: string, source: any, target: Record<string, CommandConfig>) {
private mergeCommands(prefix: string, descriptionPrefix: string, scopePrefix: string, source: any, target: Record<string, CommandConfig>) {
for (const key of Object.keys(source)) {
if (key.startsWith(prefix)) {
const cmd = key.substring(prefix.length);
target[`/${cmd}`] = {
value: source[key],
description: source[`${descriptionPrefix}${cmd}`],
scope: source[`${scopePrefix}${cmd}`]?.split(',').map((s: string) => s.trim()),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ConfigMerger {

static trim(source: AgentUserConfig, lock: string[]): Record<string, any> {
const config: Record<string, any> = { ...source };
const keysSet = new Set<string>(source.DEFINE_KEYS || []);
const keysSet = new Set<string>(source?.DEFINE_KEYS || []);
for (const key of lock) {
keysSet.delete(key);
}
Expand Down
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface APIGuard {
export interface CommandConfig {
value: string;
description?: string | null;
scope?: string[] | null;
}
3 changes: 2 additions & 1 deletion src/plugins/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface RequestTemplate {
headers: { [key: string]: string }; // 可选, Key为固定值,Value支持插值
input: {
type: TemplateInputType;
required: boolean; // 必选, 是否必须输入
};
query: { [key: string]: string }; // 可选, Key为固定值,Value支持插值
body: {
Expand Down Expand Up @@ -144,7 +145,7 @@ export async function executeRequest(template: RequestTemplate, data: any): Prom
};
}

export function formatInput(input: string, type: TemplateInputType): string | string[] | object {
export function formatInput(input: string, type: TemplateInputType): string | string[] | any {
if (type === 'json') {
return JSON.parse(input);
} else if (type === 'space-separated') {
Expand Down
Loading

0 comments on commit 4172345

Please sign in to comment.