| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- # 导入所需的核心模块
- from telegram import Update, ReplyKeyboardMarkup, KeyboardButton, WebAppInfo, InlineKeyboardButton, InlineKeyboardMarkup
- from telegram.ext import (
- ApplicationBuilder,
- CommandHandler,
- ContextTypes,
- MessageHandler,
- filters
- )
- # ===================== 替换为你的信息 =====================
- # 1. 机器人Token(从BotFather获取)
- BOT_TOKEN = "8533392621:AAELrojtgNYksFxzQ1rupLfIxh9n38TPsMU"
- # 2. 宣传图片URL(支持公网HTTPS链接,如阿里云OSS/腾讯云COS的图片链接)
- #PHOTO_URL = "https://img2.baidu.com/it/u=1185072698,2725202031&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1111"
- 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"
- # 3. Mini App访问链接(从BotFather的/myapps中复制)
- MINI_APP_URL = "https://barclient.bargame88.com/fruit/client/index.html"
- # 4. 其他链接(替换为你的实际链接)
- OFFICIAL_WEBSITE = "https://barclient.bargame88.com/fruit/client/index.html"
- TG_GROUP_LINK = "https://barclient.bargame88.com/fruit/client/index.html"
- TWITTER_LINK = "https://barclient.bargame88.com/fruit/client/index.html"
- SUPPORT_LINK = "https://t.me/BarGameService"
- # =========================================================
- # 定义/start命令的处理函数:用户发送/start时执行
- async def handle_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
- # 1. 准备欢迎文案(支持MarkdownV2格式,**加粗**、\n换行)
- welcome_text = """
- @BarGamesPro_Bot
- 欢迎来到 Bar Game!
- Bar Game 是一款轻松有趣、随时随地畅玩的线上休闲娱乐平台。
- 🎰 经典怀旧水果机,重温童年欢乐时光
- ⚖️ 公平随机算法,每一局都公正透明
- 📱 全机型适配,随时随地想玩就玩
- ✨ 无需下载注册,打开即玩,轻松安心✨ """
- # 2. 构建多行自定义键盘(完全匹配你要的样式)
- # 每行按钮用[]包裹,多个按钮逗号分隔,row()表示换行
- keyboard_buttons = [
- # 第一行:打开Mini App的按钮(核心)
- [KeyboardButton(
- text="开始使用", # 按钮显示文字
- web_app=WebAppInfo(url=MINI_APP_URL) # 点击打开的Mini App链接
- )],
- # 第二行:官网、客服支持
- [
- # KeyboardButton(text="官网"),
- KeyboardButton(text="客服支持"),
- #KeyboardButton(text="邀请有礼")
- ],
- # 第三行:官方TG群、官方Twitter
- [
- #KeyboardButton(text="官方TG群"),
- #KeyboardButton(text="官方Twitter")
- ]
- # 第四行:语言切换
- # [
- # KeyboardButton(text="中文"),
- # KeyboardButton(text="英文")
- # ],
- # 第五行:邀请加入群组
-
- ]
- # 3. 配置键盘样式(resize_keyboard=True:适配手机屏幕大小)
- # reply_markup = ReplyKeyboardMarkup(
- # keyboard_buttons,
- # resize_keyboard=True, # 关键:让键盘自适应大小,不挤压界面
- # one_time_keyboard=False # False:键盘一直显示;True:点击后隐藏
- # )
- inline_keyboard = [
- [InlineKeyboardButton(text="开始使用", web_app={"url": MINI_APP_URL})], # Mini App按钮
- [
- InlineKeyboardButton(text="客服支持", url=SUPPORT_LINK),
- # InlineKeyboardButton(text="邀请有礼", url=SUPPORT_LINK)
- ],
- [
- # InlineKeyboardButton(text="官方TG群", url=TG_GROUP_LINK),
- # InlineKeyboardButton(text="官方Twitter", url=TWITTER_LINK)
- ]
- ]
- # 创建行内键盘对象
- reply_inline_markup = InlineKeyboardMarkup(inline_keyboard)
-
- # 发送「文字+行内按钮」(按钮嵌在文字下方,和文字同区域)
- # await update.message.reply_html(
- # text=welcome_text,
- # photo=PHOTO_URL, # 宣传图片链接
- # reply_markup=reply_inline_markup,
- # disable_web_page_preview=True # 禁用链接预览,保持样式整洁
- # )
- # 4. 发送「图片+文案+自定义键盘」给用户
- await context.bot.send_photo(
- chat_id=update.effective_chat.id, # 接收消息的用户ID
- photo=PHOTO_URL, # 宣传图片链接
- caption=welcome_text, # 图片下方的欢迎文案
- parse_mode="None", # 文案支持Markdown加粗/换行
- reply_markup=reply_inline_markup # 绑定自定义键盘
- )
- # 定义按钮点击的处理函数(如用户点击“官网”“客服支持”等按钮时响应)
- async def handle_button_click(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
- user_message = update.message.text # 获取用户点击的按钮文字
- chat_id = update.effective_chat.id # 获取用户ID
- # 根据不同按钮返回对应内容
- # if user_message == "官网":
- # await context.bot.send_message(chat_id=chat_id, text=f"🔗 官网地址:{OFFICIAL_WEBSITE}")
- # el
- if user_message == "客服支持":
- await context.bot.send_message(chat_id=chat_id, text=f"💬 客服支持:{SUPPORT_LINK}")
- elif user_message == "官方TG群":
- await context.bot.send_message(chat_id=chat_id, text=f"👥 官方TG群:{TG_GROUP_LINK}")
- elif user_message == "官方Twitter":
- await context.bot.send_message(chat_id=chat_id, text=f"🐦 官方Twitter:{TWITTER_LINK}")
- # elif user_message == "中文":
- # await context.bot.send_message(chat_id=chat_id, text="✅ 已切换为中文显示")
- # elif user_message == "英文":
- # await context.bot.send_message(chat_id=chat_id, text="✅ Switched to English display")
- elif user_message == "邀请有礼":
- # 生成群组邀请链接(需先将机器人加入目标群组并设为管理员)
- # 替换为你的群组ID(格式:-100xxxxxxxxx,从@getidsbot获取)
- group_id = "-1001234567890"
- try:
- invite_link = await context.bot.export_chat_invite_link(group_id)
- await context.bot.send_message(chat_id=chat_id, text=f"📩 群组邀请链接:{invite_link}")
- except Exception as e:
- await context.bot.send_message(chat_id=chat_id, text=f"❌ 生成邀请链接失败:请确保机器人已加入群组并拥有管理员权限\n错误信息:{str(e)}")
- # 主函数:启动机器人
- def main() -> None:
- # 1. 创建机器人应用实例
- application = ApplicationBuilder().token(BOT_TOKEN).build()
- # 2. 注册/start命令处理器:用户发送/start时调用handle_start函数
- start_handler = CommandHandler("start", handle_start)
- application.add_handler(start_handler)
- # 3. 注册按钮点击处理器:监听用户的文本消息(按钮点击本质是发送文本)
- button_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, handle_button_click)
- application.add_handler(button_handler)
- # 4. 启动机器人(polling:轮询模式,适合新手,无需服务器配置)
- print("机器人已启动,按Ctrl+C停止...")
- application.run_polling()
- # 程序入口
- if __name__ == "__main__":
- main()
|