【问题标题】:sending x photos after i click a button in a bot telethon telegram在我点击 bot telethon 电报中的按钮后发送 x 照片
【发布时间】:2021-11-06 19:15:16
【问题描述】:
@bot.on(events.CallbackQuery)
async def handler(event):
global fotomandate
global i
if event.data == b"1":
    await event.respond("how many photos do i send?") 
    numerofoto = int(input("how many photos do i send?")) ##ignore this line i'll fix later
    print (numerofoto)
    while i < numerofoto:
        path = (r"C:\Users\x\Desktop\Nuova cartella (2)")
        fotorandom = random.choice([
            x for x in os.listdir(r"C:\Users\x\Desktop\Nuova cartella (2)")
            if os.path.isfile(os.path.join(path, x))
        ])
        i += 1
        await event.reply(file=fotorandom)

我需要从目录中发送 n(在电报中输入)随机照片,但它说

ValueError:无法将 bonni media-jpg 转换为媒体。不是现有文件、HTTP URL 或有效的类似 bot API 的文件 ID

【问题讨论】:

    标签: telegram telethon


    【解决方案1】:

    如错误所述:

    ValueError:无法将 bonni media-jpg 转换为媒体。不是现有文件、HTTP URL 或有效的类似 bot API 的文件 ID

    在循环保护内部,您正在执行os.path.join(path, x)。但是,您的 x 不包含完整路径。然后在工作目录中搜索该文件,但没有找到。您还需要在那里指定正确的路径:

    fotorandom = random.choice([
        os.path.join(path, x)  # <- new
        for x in os.listdir(path)  # <- better to avoid repeating dir
        if os.path.isfile(os.path.join(path, x))
    ])
    

    【讨论】:

    • 成功了 ;))
    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 2019-01-19
    • 2018-10-27
    • 2016-10-17
    • 2020-04-16
    • 2021-04-20
    • 2013-12-31
    • 2021-04-24
    相关资源
    最近更新 更多