我无法发送位于应用目录中的* .txt文件:
我在/ app中有文件,我想使用 sendDocument 方法发送文件,
file = open('report.txt', 'w')
file.write('report')
file.close()
telepot_bot.sendDocument(os.environ['ADMIN0'], document=file, caption='report.txt')
# or
telepot_bot.sendDocument(os.environ['ADMIN0'], document=file.name, caption='report.txt')
# or
telepot_bot.sendDocument(os.environ['ADMIN0'], document=os.path.realpath(file.name), caption='report.txt')
返回:
ValueError: I/O operation on closed file.
# or
telepot.exception.TelegramError: ('Bad Request: wrong file identifier/HTTP URL specified', 400, {'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong file identifier/HTTP URL specified'})
# or
telepot.exception.TelegramError: ('Bad Request: URL host is empty', 400, {'ok': False, 'error_code': 400, 'description': 'Bad Request: URL host is empty'})
context.bot.send_document(os.environ['ADMIN0'], document=file, filename='report.txt')
# or
context.bot.send_document(os.environ['ADMIN0'], document=file.name, filename='report.txt')
# or
context.bot.send_document(os.environ['ADMIN0'], document=os.path.realpath(file.name), filename='report.txt')
返回:
ValueError: I/O operation on closed file.
# or
telegram.error.BadRequest: Wrong file identifier/http url specified
# or
telegram.error.BadRequest: Url host is empty
手册说:
“ ......使用multipart / form-data上载一个新文件。最后,您可以传递一个现有的:class:telegram.Document对象来发送。”
但是我应该怎么做呢?
答案 0 :(得分:1)
检查文件是否在当前工作目录中。
发送文件的实际代码:
with open("report.txt", "rb") as file:
context.bot.send_document(chat_id=1234, document=file,
filename='this_is_for_tg_name.txt')
请参考使用send_document here
的正确格式