robot.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # 导入所需的核心模块
  2. from telegram import Update, ReplyKeyboardMarkup, KeyboardButton, WebAppInfo, InlineKeyboardButton, InlineKeyboardMarkup
  3. from telegram.ext import (
  4. ApplicationBuilder,
  5. CommandHandler,
  6. ContextTypes,
  7. MessageHandler,
  8. filters
  9. )
  10. # ===================== 替换为你的信息 =====================
  11. # 1. 机器人Token(从BotFather获取)
  12. BOT_TOKEN = "8533392621:AAELrojtgNYksFxzQ1rupLfIxh9n38TPsMU"
  13. # 2. 宣传图片URL(支持公网HTTPS链接,如阿里云OSS/腾讯云COS的图片链接)
  14. #PHOTO_URL = "https://img2.baidu.com/it/u=1185072698,2725202031&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1111"
  15. PHOTO_URL = "https://thumbnail1.baidupcs.com/thumbnail/2907b31dbnf01967750bafc75df32b0c?fid=2488527323-250528-62810681873948&rt=pr&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-UYPhKyK8ZxUD%2f0gCa9NSwi%2bdGmc%3d&expires=8h&chkbd=0&chkv=0&dp-logid=9009522059578414684&dp-callid=0&time=1770692400&size=c1920_u1080&quality=90&vuk=2488527323&ft=image&autopolicy=1"
  16. # 3. Mini App访问链接(从BotFather的/myapps中复制)
  17. MINI_APP_URL = "https://barclient.bargame88.com/fruit/client/index.html"
  18. # 4. 其他链接(替换为你的实际链接)
  19. OFFICIAL_WEBSITE = "https://barclient.bargame88.com/fruit/client/index.html"
  20. TG_GROUP_LINK = "https://barclient.bargame88.com/fruit/client/index.html"
  21. TWITTER_LINK = "https://barclient.bargame88.com/fruit/client/index.html"
  22. SUPPORT_LINK = "https://t.me/BarGameService"
  23. # =========================================================
  24. # 定义/start命令的处理函数:用户发送/start时执行
  25. async def handle_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  26. # 1. 准备欢迎文案(支持MarkdownV2格式,**加粗**、\n换行)
  27. welcome_text = """
  28. @BarGamesPro_Bot
  29. 欢迎来到 Bar Game!
  30. Bar Game 是一款轻松有趣、随时随地畅玩的线上休闲娱乐平台。
  31. 🎰 经典怀旧水果机,重温童年欢乐时光
  32. ⚖️ 公平随机算法,每一局都公正透明
  33. 📱 全机型适配,随时随地想玩就玩
  34. ✨ 无需下载注册,打开即玩,轻松安心✨ """
  35. # 2. 构建多行自定义键盘(完全匹配你要的样式)
  36. # 每行按钮用[]包裹,多个按钮逗号分隔,row()表示换行
  37. keyboard_buttons = [
  38. # 第一行:打开Mini App的按钮(核心)
  39. [KeyboardButton(
  40. text="开始使用", # 按钮显示文字
  41. web_app=WebAppInfo(url=MINI_APP_URL) # 点击打开的Mini App链接
  42. )],
  43. # 第二行:官网、客服支持
  44. [
  45. # KeyboardButton(text="官网"),
  46. KeyboardButton(text="客服支持"),
  47. #KeyboardButton(text="邀请有礼")
  48. ],
  49. # 第三行:官方TG群、官方Twitter
  50. [
  51. #KeyboardButton(text="官方TG群"),
  52. #KeyboardButton(text="官方Twitter")
  53. ]
  54. # 第四行:语言切换
  55. # [
  56. # KeyboardButton(text="中文"),
  57. # KeyboardButton(text="英文")
  58. # ],
  59. # 第五行:邀请加入群组
  60. ]
  61. # 3. 配置键盘样式(resize_keyboard=True:适配手机屏幕大小)
  62. # reply_markup = ReplyKeyboardMarkup(
  63. # keyboard_buttons,
  64. # resize_keyboard=True, # 关键:让键盘自适应大小,不挤压界面
  65. # one_time_keyboard=False # False:键盘一直显示;True:点击后隐藏
  66. # )
  67. inline_keyboard = [
  68. [InlineKeyboardButton(text="开始使用", web_app={"url": MINI_APP_URL})], # Mini App按钮
  69. [
  70. InlineKeyboardButton(text="客服支持", url=SUPPORT_LINK),
  71. # InlineKeyboardButton(text="邀请有礼", url=SUPPORT_LINK)
  72. ],
  73. [
  74. # InlineKeyboardButton(text="官方TG群", url=TG_GROUP_LINK),
  75. # InlineKeyboardButton(text="官方Twitter", url=TWITTER_LINK)
  76. ]
  77. ]
  78. # 创建行内键盘对象
  79. reply_inline_markup = InlineKeyboardMarkup(inline_keyboard)
  80. # 发送「文字+行内按钮」(按钮嵌在文字下方,和文字同区域)
  81. # await update.message.reply_html(
  82. # text=welcome_text,
  83. # photo=PHOTO_URL, # 宣传图片链接
  84. # reply_markup=reply_inline_markup,
  85. # disable_web_page_preview=True # 禁用链接预览,保持样式整洁
  86. # )
  87. # 4. 发送「图片+文案+自定义键盘」给用户
  88. await context.bot.send_photo(
  89. chat_id=update.effective_chat.id, # 接收消息的用户ID
  90. photo=PHOTO_URL, # 宣传图片链接
  91. caption=welcome_text, # 图片下方的欢迎文案
  92. parse_mode="None", # 文案支持Markdown加粗/换行
  93. reply_markup=reply_inline_markup # 绑定自定义键盘
  94. )
  95. # 定义按钮点击的处理函数(如用户点击“官网”“客服支持”等按钮时响应)
  96. async def handle_button_click(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
  97. user_message = update.message.text # 获取用户点击的按钮文字
  98. chat_id = update.effective_chat.id # 获取用户ID
  99. # 根据不同按钮返回对应内容
  100. # if user_message == "官网":
  101. # await context.bot.send_message(chat_id=chat_id, text=f"🔗 官网地址:{OFFICIAL_WEBSITE}")
  102. # el
  103. if user_message == "客服支持":
  104. await context.bot.send_message(chat_id=chat_id, text=f"💬 客服支持:{SUPPORT_LINK}")
  105. elif user_message == "官方TG群":
  106. await context.bot.send_message(chat_id=chat_id, text=f"👥 官方TG群:{TG_GROUP_LINK}")
  107. elif user_message == "官方Twitter":
  108. await context.bot.send_message(chat_id=chat_id, text=f"🐦 官方Twitter:{TWITTER_LINK}")
  109. # elif user_message == "中文":
  110. # await context.bot.send_message(chat_id=chat_id, text="✅ 已切换为中文显示")
  111. # elif user_message == "英文":
  112. # await context.bot.send_message(chat_id=chat_id, text="✅ Switched to English display")
  113. elif user_message == "邀请有礼":
  114. # 生成群组邀请链接(需先将机器人加入目标群组并设为管理员)
  115. # 替换为你的群组ID(格式:-100xxxxxxxxx,从@getidsbot获取)
  116. group_id = "-1001234567890"
  117. try:
  118. invite_link = await context.bot.export_chat_invite_link(group_id)
  119. await context.bot.send_message(chat_id=chat_id, text=f"📩 群组邀请链接:{invite_link}")
  120. except Exception as e:
  121. await context.bot.send_message(chat_id=chat_id, text=f"❌ 生成邀请链接失败:请确保机器人已加入群组并拥有管理员权限\n错误信息:{str(e)}")
  122. # 主函数:启动机器人
  123. def main() -> None:
  124. # 1. 创建机器人应用实例
  125. application = ApplicationBuilder().token(BOT_TOKEN).build()
  126. # 2. 注册/start命令处理器:用户发送/start时调用handle_start函数
  127. start_handler = CommandHandler("start", handle_start)
  128. application.add_handler(start_handler)
  129. # 3. 注册按钮点击处理器:监听用户的文本消息(按钮点击本质是发送文本)
  130. button_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, handle_button_click)
  131. application.add_handler(button_handler)
  132. # 4. 启动机器人(polling:轮询模式,适合新手,无需服务器配置)
  133. print("机器人已启动,按Ctrl+C停止...")
  134. application.run_polling()
  135. # 程序入口
  136. if __name__ == "__main__":
  137. main()