我想这样当telegram bot请求某个文件时,用户发送它,discord webhook将它发送到discord频道。我是编码新手,我决定制作我的电报机器人,所以我真的需要帮助。我导入了钩子,并从中进行webhook。我编写了以下代码
@bot.message_handler()
def get_user_text(message):
text = message.text
hook.send(text)
它将用户对telegram bot说的任何文本发送到discord webhook。但是我很困惑如何用文件重复这样的东西。我试着做那个代码,它只反应如果文档发送到bot,它进入文件变量文件本身。
@bot.message_handler(content_types=['document'])
def get_user_text(message):
file = message.document
hook.send(file)
当我运行代码时什么都没有发生,但是每当我用这段代码将文件发送给bot时,就会发生错误。
TypeError: Object of type Document is not JSON serializable
Process finished with exit code 1
请帮助。
看起来你正在使用pyTelegramBotAPI
你得到的TypeError是因为钩子期望一个dhooks.file.File
对象,但是你发送的是一个Document
对象(message.document
)。
根据dhooks文档,您将需要文件路径或文件二进制流来创建File
对象:
file = File('path/to/file.png')
hook.send('Look at this:', file=file)
或
response = requests.get(message.document.url)
file = File(BytesIO(response.content))
hook.send('Another one:', file=file)