tg / src /main.py
l-g-t's picture
Update src/main.py
b6b51ed verified
import argparse
import asyncio
import telebot
from telebot.async_telebot import AsyncTeleBot
import handlers
# Init args
parser = argparse.ArgumentParser()
parser.add_argument("tg_token", help="telegram token")
parser.add_argument("GOOGLE_GEMINI_KEY", help="Google Gemini API key")
options = parser.parse_args()
print("Arg parse done.")
async def main():
# Init bot
##bot = AsyncTeleBot(options.tg_token)
# 关键修改:添加 api_url 参数,强制 Bot 使用 Cloudflare Workers 代理
# 注意:{0} 是 Bot Token 的占位符,不能删除。
##bot = AsyncTeleBot(options.tg_token, api_url='https://tg.tonycn.ggff.net/bot{0}')
# 恢复为使用域名,因为 hosts 文件会负责解析到 IP。
bot = AsyncTeleBot(options.tg_token, api_url='https://tg.tonycn.ggff.net/bot{0}')
#bot = AsyncTeleBot(options.tg_token, api_url='https://tg.tonycn.ggff.net/bot{0}')
##bot = AsyncTeleBot(options.tg_token, api_url='https://104.21.12.226/bot{0}')
##bot = AsyncTeleBot(options.tg_token, api_url='https://172.67.196.135/bot{0}')
await bot.delete_my_commands(scope=None, language_code=None)
await bot.set_my_commands(
commands=[
telebot.types.BotCommand("start", "Start"),
telebot.types.BotCommand("gemini", "Chat with gemini"),
telebot.types.BotCommand("clear", "Clear all history"),
telebot.types.BotCommand("switch","switch model")
],
)
print("Bot init done.")
# Init commands
bot.register_message_handler(handlers.start, commands=['start'], pass_bot=True)
bot.register_message_handler(handlers.gemini_handler, commands=['gemini'], pass_bot=True)
bot.register_message_handler(handlers.clear, commands=['clear'], pass_bot=True)
bot.register_message_handler(handlers.switch, commands=['switch'], pass_bot=True)
bot.register_message_handler(handlers.gemini_photo_handler, content_types=["photo"], pass_bot=True)
bot.register_message_handler(handlers.gemini_private_handler, content_types=['text'], pass_bot=True, func=lambda message: message.chat.type == "private")
# Start bot
print("Starting Gemini_Telegram_Bot.")
await bot.polling(none_stop=True)
if __name__ == '__main__':
asyncio.run(main())