【问题标题】:Upload file on Telegram with bot使用机器人在 Telegram 上上传文件
【发布时间】:2018-09-18 17:45:42
【问题描述】:

我想使用 Telegram Bots 从 URL 向用户发送文件,我的文件扩展名为 .attheme,但我无法从 Url 上传这些文件。

目前我可以上传.zip.pdf,但我想从PHP代码上传.attheme文件。

此机器人可以将任何类型的文件上传到 Telegram:@uploadbot

我该怎么做?

【问题讨论】:

  • 您收到什么样的错误?

标签: telegram telegram-bot php-telegram-bot telegram-webhook


【解决方案1】:

通过 URL 发送文件仅适用于某些文件类型。如果您想上传其他类型的文件,则必须在将文件保存在您自己的服务器上后,使用 multipart/form-data 发布文件。

通过 URL 发送
在 sendDocument 中,通过 URL 发送当前只会 适用于 gif、pdf 和 zip 文件。 [doc]


在 PHP 中发送文件

$filepath = realpath('folder/.attheme');
$post = array('chat_id' => $GLOBALS["chat_id"],'document'=>new CurlFile($filepath));    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/bot" . $GLOBALS["token"] . "/sendDocument");
curl_setopt($ch, CURLOPT_POST, 1);   
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec ($ch);
curl_close ($ch); 

【讨论】:

  • 您好!我正在尝试用 Java 来做到这一点。我的电脑上有一个.gif 文件。要在sendAnimation 方法中传递它,我需要提供一个类型InputFile。我不确定如何创建该类型?电报 API 说:Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files。那么,multipart/form-data是什么意思?
  • 但是 sendDocument 需要文件 URL 或现有文件 ID 或一些表单数据:File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. 。如何使用 multipart/form-data 的东西上传本地文件?
猜你喜欢
  • 2021-06-29
  • 2016-01-20
  • 2017-10-23
  • 2019-12-30
  • 2019-01-30
  • 2013-02-13
  • 2021-12-26
  • 2020-08-30
  • 2021-05-26
相关资源
最近更新 更多