Telegram Bot - 如何使用绝对/动态上传本地文件 URL
Telegram Bot - How to upload local files with absolute/ dynamic URL
我正在尝试使用 'sendPhoto' 方法和相对 url(文件级图像)通过电报机器人发送照片。我没有使用任何库,这是我的调用函数:
let axiosImage = async (chatId, caption, res) => {
try {
await axios.post(`${TELEGRAM_API}/sendPhoto`,
{
headers:{'Content-Type': 'multipart/form-data'}
},{
body: {
'chat_id': chatId,
'caption': caption,
'photo': './image.jpeg'
}
})
return res.send()
} catch (e) {
console.log('\nSTATUS RESPONSE: ' + e.response.status)
console.log('\nMESSAGE RESPONSE: ' + e.response.statusText)
}}
但我收到此消息:{"ok":false,"error_code":400,"description":"错误请求:请求中没有照片"}
我试过网络 url 并且发送正常。
我可能遗漏了什么?我是否必须在某些存储库中上传本地图像?
来自电报api docs
- If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
- Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
- 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.
您要发送的是通过文件上传 (3.) 发送的文件。这是您要实现的目标的答案:
我最近遇到了类似的问题,我设法使用 form-data npm 包和 built-in fs
模块解决了这个问题。
const FormData = require('form-data');
const fs = require('fs');
const axiosImage = async(chatId, caption, res) => {
try {
const formData = new FormData();
formData.append('chat_id', chatId);
formData.append('photo', fs.createReadStream('./image.jpeg'));
formData.append('caption', caption);
const response = await axios.post(`${TELEGRAM_API}/sendPhoto`, formData, {
headers: formData.getHeaders(),
});
return res.send();
} catch (err) {
console.log(err);
}
}
我正在尝试使用 'sendPhoto' 方法和相对 url(文件级图像)通过电报机器人发送照片。我没有使用任何库,这是我的调用函数:
let axiosImage = async (chatId, caption, res) => {
try {
await axios.post(`${TELEGRAM_API}/sendPhoto`,
{
headers:{'Content-Type': 'multipart/form-data'}
},{
body: {
'chat_id': chatId,
'caption': caption,
'photo': './image.jpeg'
}
})
return res.send()
} catch (e) {
console.log('\nSTATUS RESPONSE: ' + e.response.status)
console.log('\nMESSAGE RESPONSE: ' + e.response.statusText)
}}
但我收到此消息:{"ok":false,"error_code":400,"description":"错误请求:请求中没有照片"}
我试过网络 url 并且发送正常。
我可能遗漏了什么?我是否必须在某些存储库中上传本地图像?
来自电报api docs
- If the file is already stored somewhere on the Telegram servers, you don't need to reupload it: each file object has a file_id field, simply pass this file_id as a parameter instead of uploading. There are no limits for files sent this way.
- Provide Telegram with an HTTP URL for the file to be sent. Telegram will download and send the file. 5 MB max size for photos and 20 MB max for other types of content.
- 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.
您要发送的是通过文件上传 (3.) 发送的文件。这是您要实现的目标的答案:
我最近遇到了类似的问题,我设法使用 form-data npm 包和 built-in fs
模块解决了这个问题。
const FormData = require('form-data');
const fs = require('fs');
const axiosImage = async(chatId, caption, res) => {
try {
const formData = new FormData();
formData.append('chat_id', chatId);
formData.append('photo', fs.createReadStream('./image.jpeg'));
formData.append('caption', caption);
const response = await axios.post(`${TELEGRAM_API}/sendPhoto`, formData, {
headers: formData.getHeaders(),
});
return res.send();
} catch (err) {
console.log(err);
}
}