You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram内联机器人:如何随机化内联按钮顺序及Emojihash?

解决方案

一、实现内联按钮位置随机切换

利用Python的random模块打乱按钮行的顺序即可,具体代码示例如下:

import random

Emojihash: str = emojihash.eh1("unique code",5)
EmojiCode: str = emojihash.eh1("unique password" + SellerUserName + str(update.inline_query.from_user.id), 5)

# 定义两个独立的按钮行
button_rows = [
    [InlineKeyboardButton(EmojiCode, callback_data=str(ONE))],
    [InlineKeyboardButton(Emojihash, callback_data=str(TWO))]
]
# 随机打乱行的顺序
random.shuffle(button_rows)

keyboard = button_rows
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(chat_id=update.inline_query.from_user.id, text=verified_message_1, reply_markup=reply_markup)

二、每次调用生成随机5个表情符号

emojihash.eh1是哈希映射函数,输入固定则输出固定。要实现每次生成随机表情,只需给输入参数加入随机因子,确保每次输入不同,提供两种实用方法:

方法1:基于时间戳生成随机因子

import time

# 加入当前时间戳,每次调用的哈希种子都不同
Emojihash: str = emojihash.eh1("unique code" + str(int(time.time())), 5)

方法2:基于随机字符串生成随机因子

import random
import string

# 生成8位随机字符串作为哈希种子的后缀
random_suffix = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
Emojihash: str = emojihash.eh1("unique code" + random_suffix, 5)

以上两种方法都会让每次调用Emojihash时的输入种子唯一,从而生成不同的随机表情组合。

内容的提问来源于stack exchange,提问作者DzEskobar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:50:21