Skip to content

Commit a5296b0

Browse files
committed
Fix the bug that prevents the use of search, add whitelist function to prevent abuse and information leakage
1 parent 5a9b851 commit a5296b0

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use
1818

1919
✅ 支持流式输出,实现打字机效果
2020

21+
✅ 支持白名单,防止滥用与信息泄漏
22+
2123
✅ 全平台,随时随地,只要有 telegram 就可以打破知识壁垒
2224

2325
✅ 支持一键 Zeabur,Replit 部署,真正的零成本,傻瓜式部署,支持 kuma 防睡眠。同时支持 docker,fly.io 部署
@@ -35,6 +37,7 @@ Join the [Telegram Group](https://t.me/+_01cz9tAkUc1YzZl) chat to share your use
3537
| PASS_HISTORY(可选) | 默认为真,表示机器人会记住对话历史,下次回复时会考虑上下文。如果设置为假,机器人会忘记对话历史,只考虑当前对话。 |
3638
| GOOGLE_API_KEY(可选) | 如果需要谷歌搜索,则需要设置。如果不设置此环境变量,机器人默认提供 duckduckgo 搜索。在 Google cloud 的 [API 与服务](https://console.cloud.google.com/apis/api/customsearch.googleapis.com) 中创建凭据,在凭据页面 API Key 就是 GOOGLE_API_KEY。Google 搜索一天可以查询 100 次,轻度使用完全足够,达到限额,机器人会自动关闭 Google 搜索。 |
3739
| GOOGLE_CSE_ID(可选) | 如果需要谷歌搜索,则需要与 GOOGLE_API_KEY 一起设置。在[可编程搜索引擎](https://programmablesearchengine.google.com/) 中新建搜索引擎,其中 搜索引擎 ID 就是 GOOGLE_CSE_ID 的值。 |
40+
| whitelist(可选) | 设置哪些用户可以访问机器人,将授权使用机器人的用户 ID 用`,`连接起来。默认值为`None`,即对所有人开放机器人。 |
3841

3942
## Zeabur 远程部署 (推荐)
4043

bot.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import config
44
import logging
5+
import decorators
56
from md2tgmd import escape
67
from runasync import run_async
78
from chatgpt2api.V3 import Chatbot as GPT
@@ -26,6 +27,7 @@
2627
botNicKLength = len(botNick) if botNick else 0
2728
print("nick:", botNick)
2829
translator_prompt = "You are a translation engine, you can only translate text and cannot interpret it, and do not explain. Translate the text to {}, please do not explain any sentences, just translate or leave them as they are. this is the content you need to translate: "
30+
@decorators.Authorization
2931
async def command_bot(update, context, language=None, prompt=translator_prompt, title="", robot=None, has_command=True):
3032
if config.SEARCH_USE_GPT and not has_command:
3133
title = f"`🤖️ {config.DEFAULT_SEARCH_MODEL}`\n\n"
@@ -40,7 +42,7 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
4042
message = prompt + message
4143
if config.API and message:
4244
await context.bot.send_chat_action(chat_id=update.message.chat_id, action=ChatAction.TYPING)
43-
await getChatGPT(update, context, title, robot, message, config.SEARCH_USE_GPT, has_command=True)
45+
await getChatGPT(update, context, title, robot, message, config.SEARCH_USE_GPT, has_command)
4446
else:
4547
message = await context.bot.send_message(
4648
chat_id=update.message.chat_id,
@@ -75,6 +77,7 @@ async def command_bot(update, context, language=None, prompt=translator_prompt,
7577
print(result)
7678
await context.bot.send_message(chat_id=update.message.chat_id, text=escape(result), parse_mode='MarkdownV2', disable_web_page_preview=True)
7779

80+
@decorators.Authorization
7881
async def reset_chat(update, context):
7982
if config.API:
8083
config.ChatGPTbot.reset(convo_id=str(update.message.chat_id), system_prompt=config.systemprompt)
@@ -351,7 +354,7 @@ async def button_press(update, context):
351354
parse_mode='MarkdownV2'
352355
)
353356

354-
357+
@decorators.Authorization
355358
async def info(update, context):
356359
info_message = (
357360
f"`Hi, {update.effective_user.username}!`\n\n"
@@ -368,6 +371,7 @@ async def info(update, context):
368371
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=update.message.message_id)
369372

370373
from agent import pdfQA, getmd5, persist_emdedding_pdf
374+
@decorators.Authorization
371375
async def handle_pdf(update, context):
372376
# 获取接收到的文件
373377
pdf_file = update.message.document
@@ -397,6 +401,7 @@ async def handle_pdf(update, context):
397401
print(result)
398402
await context.bot.send_message(chat_id=update.message.chat_id, text=escape(result), parse_mode='MarkdownV2', disable_web_page_preview=True)
399403

404+
@decorators.Authorization
400405
async def qa(update, context):
401406
if (len(context.args) != 2):
402407
message = (
@@ -439,6 +444,7 @@ async def error(update, context):
439444
logger.warning('Update "%s" caused error "%s"', update, context.error)
440445
await context.bot.send_message(chat_id=update.message.chat_id, text="出错啦!请重试。", parse_mode='MarkdownV2')
441446

447+
@decorators.Authorization
442448
async def unknown(update, context): # 当用户输入未知命令时,返回文本
443449
await context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
444450

@@ -452,9 +458,6 @@ def setup(token):
452458
BotCommand('zh2en', 'translate to English'),
453459
BotCommand('start', 'Start the bot'),
454460
BotCommand('reset', 'Reset the bot'),
455-
# BotCommand('gpt_use_search', 'open or close gpt use search'),
456-
# BotCommand('history', 'open or close chat history'),
457-
# BotCommand('google', 'open or close google search'),
458461
]))
459462

460463
application.add_handler(CommandHandler("start", start))

config.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@
2424
from chatgpt2api.V3 import Chatbot as GPT
2525
if API:
2626
ChatGPTbot = GPT(api_key=f"{API}", engine=GPT_ENGINE, system_prompt=systemprompt, temperature=temperature)
27-
Claude2bot = GPT(api_key=f"{API}", engine="claude-2-web")
27+
Claude2bot = GPT(api_key=f"{API}", engine="claude-2-web")
28+
29+
whitelist = os.environ.get('whitelist', None)
30+
if whitelist:
31+
whitelist = [int(id) for id in whitelist.split(",")]

decorators.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import config
2+
3+
# 判断是否是管理员
4+
def Authorization(func):
5+
async def wrapper(*args, **kwargs):
6+
if config.whitelist == None:
7+
return await func(*args, **kwargs)
8+
if (args[0].effective_chat.id not in config.whitelist):
9+
message = (
10+
f"`Hi, {args[0].effective_user.username}!`\n\n"
11+
f"id: `{args[0].effective_user.id}`\n\n"
12+
f"无权访问!\n\n"
13+
)
14+
await args[1].bot.send_message(chat_id=args[0].effective_chat.id, text=message, parse_mode='MarkdownV2')
15+
return
16+
return await func(*args, **kwargs)
17+
return wrapper

test/test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
a = "werc"
2-
if ("q" or "c") in a:
3-
print(1)
1+
a = ["1", "2"]
2+
a = [int(i) for i in a]
3+
print(type(a[0]))

0 commit comments

Comments
 (0)