使用电报Bot使用纯HTTP请求发送文件

时间:2020-05-17 06:31:41

标签: telegram-bot

大家好,我想使用http://api.telegram.org/botTOKEN/sendDocument?document=http://my_path&chat_id这样的漫游器发送文件,但它不支持.txt .docx .....和其他格式.....请提供任何帮助

1 个答案:

答案 0 :(得分:0)

根据https://core.telegram.org/bots/api#sending-files

通过URL发送在sendDocument中,当前仅通过URL发送 用于 gif pdf zip 文件。

您可以尝试使用这种方法

使用multipart / form-data以文件通常的方式发布文件 通过浏览器上传。照片最大大小为10 MB,照片最大大小为50 MB 其他文件。

answer可能会给您一些有关如何做到这一点的想法。

更新

使用某人的图书馆来了解图书馆的运作方式也是一个好主意。 例如,我使用this repo中的longman/telegram-botRequest类中有encodeFile个方法。

方法如下:

public static function encodeFile($file)
    {
        $fp = fopen($file, 'rb');
        if ($fp === false) {
            throw new TelegramException('Cannot open "' . $file . '" for reading');
        }

        return $fp;
    }

这意味着带有fopen参数的简单'rb'方法足以转换文件。