1

我无法发送位于 app 目录中的 *.txt 文件:
我在 /app 中有文件,我想用sendDocument方法发送它,

  • Telepot lib案例:
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'})

  • python-telegram-bot库案例:
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 对象来发送。”
但我应该怎么做?

4

1 回答 1

2

检查文件是否存在于当前工作目录中。

发送文件的实际代码:

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 的正确格式

于 2020-08-29T17:03:38.420 回答