yangtb24 commited on
Commit
3f2d601
·
verified ·
1 Parent(s): bdd2b0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -14,6 +14,8 @@ from telegram.ext import (
14
  MessageHandler,
15
  filters,
16
  )
 
 
17
 
18
  # 配置
19
  TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
@@ -41,18 +43,18 @@ chat_histories = {} # Use a dict instead of a Map
41
  GROUP_SETTINGS = {} # Use a dict instead of a Map
42
  USER_SETTINGS = {} # Use a dict instead of a Map
43
  BOT_COMMANDS = [
44
- ("start", "显示欢迎信息和操作按钮"),
45
- ("clearall", "清空当前会话的聊天记录"),
46
- ("help", "显示此帮助信息"),
47
- ("enableai", "在群组中启用AI回复"),
48
- ("disableai", "在群组中禁用AI回复"),
49
- ("setprefix", "设置群组中触发AI回复的前缀,例如: /setprefix @bot"),
50
- ("getprefix", "获取当前群组的触发前缀"),
51
- ("settemp", "设置AI回复的温度,例如:/settemp 1.0"),
52
- ("gettemp", "获取当前AI回复的温度"),
53
- ("resetuser", "重置你的个人设置"),
54
- ("promat", "切换提示词,例如: /promat 0, 1, 2"),
55
- ("getpromat", "获取当前使用的提示词索引"),
56
  ]
57
  BOT_USERNAME = "zfs732_bot"
58
  DEFAULT_TEMP = 1.5
@@ -150,12 +152,14 @@ async def set_bot_commands(bot: Bot):
150
  return
151
  logging.info("Telegram commands deleted successfully")
152
  commands = [
153
- (command[0], command[1])
154
  for command in BOT_COMMANDS
155
  ]
156
  url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/setMyCommands"
 
 
157
  async with aiohttp.ClientSession() as session:
158
- async with session.post(url, json={"commands": commands}) as response:
159
  if not response.ok:
160
  error_text = await response.text()
161
  logging.error(f"Telegram set commands failed: {response.status}, {error_text}")
@@ -545,6 +549,18 @@ def get_help_message():
545
  - 机器人具有攻击性,请谨慎使用。
546
  """
547
 
 
 
 
 
 
 
 
 
 
 
 
 
548
  async def main():
549
  logging.basicConfig(level=logging.INFO)
550
 
@@ -552,7 +568,7 @@ async def main():
552
  logging.error("PHP proxy health check failed. Exiting.")
553
  return
554
 
555
- application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
556
 
557
  # Set bot commands
558
  try:
@@ -582,7 +598,7 @@ async def main():
582
  application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_telegram_update))
583
 
584
  # Initialize the bot
585
- await application.initialize()
586
  # Start the bot
587
  await application.start()
588
  # Start polling updates from Telegram
 
14
  MessageHandler,
15
  filters,
16
  )
17
+ import httpx
18
+ from telegram.error import NetworkError
19
 
20
  # 配置
21
  TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
 
43
  GROUP_SETTINGS = {} # Use a dict instead of a Map
44
  USER_SETTINGS = {} # Use a dict instead of a Map
45
  BOT_COMMANDS = [
46
+ {"command": "start", "description": "显示欢迎信息和操作按钮"},
47
+ {"command": "clearall", "description": "清空当前会话的聊天记录"},
48
+ {"command": "help", "description": "显示此帮助信息"},
49
+ {"command": "enableai", "description": "在群组中启用AI回复"},
50
+ {"command": "disableai", "description": "在群组中禁用AI回复"},
51
+ {"command": "setprefix", "description": "设置群组中触发AI回复的前缀,例如: /setprefix @bot"},
52
+ {"command": "getprefix", "description": "获取当前群组的触发前缀"},
53
+ {"command": "settemp", "description": "设置AI回复的温度,例如:/settemp 1.0"},
54
+ {"command": "gettemp", "description": "获取当前AI回复的温度"},
55
+ {"command": "resetuser", "description": "重置你的个人设置"},
56
+ {"command": "promat", "description": "切换提示词,例如: /promat 0, 1, 2"},
57
+ {"command": "getpromat", "description": "获取当前使用的提示词索引"},
58
  ]
59
  BOT_USERNAME = "zfs732_bot"
60
  DEFAULT_TEMP = 1.5
 
152
  return
153
  logging.info("Telegram commands deleted successfully")
154
  commands = [
155
+ {"command": command["command"], "description": command["description"]}
156
  for command in BOT_COMMANDS
157
  ]
158
  url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/setMyCommands"
159
+ json_data = {"commands": commands}
160
+ logging.info(f"Sending setMyCommands request with data: {json.dumps(json_data)}")
161
  async with aiohttp.ClientSession() as session:
162
+ async with session.post(url, json=json_data) as response:
163
  if not response.ok:
164
  error_text = await response.text()
165
  logging.error(f"Telegram set commands failed: {response.status}, {error_text}")
 
549
  - 机器人具有攻击性,请谨慎使用。
550
  """
551
 
552
+ async def initialize_with_retry(application):
553
+ max_retries = 3
554
+ for attempt in range(max_retries):
555
+ try:
556
+ await application.initialize()
557
+ return
558
+ except NetworkError as e:
559
+ logging.error(f"Error initializing application, attempt {attempt + 1}: {e}")
560
+ if attempt == max_retries - 1:
561
+ raise
562
+ await asyncio.sleep(2) # Wait for 2 seconds before retrying
563
+
564
  async def main():
565
  logging.basicConfig(level=logging.INFO)
566
 
 
568
  logging.error("PHP proxy health check failed. Exiting.")
569
  return
570
 
571
+ application = Application.builder().token(TELEGRAM_BOT_TOKEN).http_client(httpx.AsyncClient(trust_env=True)).build()
572
 
573
  # Set bot commands
574
  try:
 
598
  application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_telegram_update))
599
 
600
  # Initialize the bot
601
+ await initialize_with_retry(application)
602
  # Start the bot
603
  await application.start()
604
  # Start polling updates from Telegram