使用这个机器人,我想在点击按钮并在控制台中输入数字后发送x张照片。1个问题-它无法正常循环,在我点击机器人上的按钮后,它会向ecc发送fo.如果我再试一次,它将不起作用。
2问题-如果我输入3,机器人会发送4张照片
这是代码:
bottoni = [
[
Button.inline("invia foto", b"1"),
Button.inline("seconda opzione", b"2")
],
[
Button.inline("terza opzione", b"3"),
Button.inline("quarta opzione", b"4")
],
]
i = 0
bot = TelegramClient('session_name', api_id, api_hash).start(bot_token=bot_token)
client = TelegramClient('session_name', api_id, api_hash)
print ("bot e userbot startati!")
@bot.on(events.NewMessage(pattern="/start"))
async def start(event):
await event.respond(". --benvenuto nel bot-- .")
@bot.on(events.NewMessage(pattern="opzioni"))
@bot.on(events.NewMessage(pattern="Opzioni"))
@bot.on(events.NewMessage(pattern="/opzioni"))
async def handler(event):
await bot.send_message("myuser",". --queste sono le opzioni-- .",buttons= bottoni)
@bot.on(events.CallbackQuery)
async def handler(event):
global i
global path
if event.data == b"1":
await event.respond("quante foto devo mandare? / guarda console")
numerofoto = int(input("quante foto devo mandare? "))
while i <= numerofoto:
path = (r"C:pathtofolderwithphotos")
fotorandom = random.choice([
os.path.join(path, x)
for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
i += 1
await event.reply(file=fotorandom)
kk固定tho。
我刚刚添加了
if i >= numerofoto:
i = 0
print("foto mandate: " + str(numerofoto))
await event.reply(file=fotorandom)
之后
这解决了这两个问题。