![](/img/trans.png)
[英]Telegram bot with telegraf.js : can't send random photo to chat using flickr api
[英]how to send photo from telegram bot api (javascript)
我在通过电报文本消息发送 api 时遇到问题,但图片没有到达,我不明白问题出在哪里 是否可以修改此代码并使其发送图像?
var telegram_bot_id = "api"; var chat_id = "id"; var img; var ready = function () { img = document.getElementById("photo").value; message = "photo: " + img; }; var sender = function () { ready(); var settings = { "async": true, "crossDomain": true, "url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendPhoto", "method": "POST", "headers": { "Content-Type": "application/json", "cache-control": "no-cache" }, "data": JSON.stringify({ "chat_id": chat_id, "text": message }) }; $.ajax(settings).done(function (response) { console.log(response); window.location.href = "index.html"; }); document.getElementById("photo").value = ""; return false; };
为了能够通过sendPhoto方法发送照片,您必须发送正确的图片文件或代表有效 URL 的字符串。
根据您发布的代码,您将作为正文参数发送到 HTTP 请求,chat_id,文本消息本身就是这样。
sendPhoto方法不需要在sendMessage方法中使用的文本字段。
在您的情况下,由于您尝试通过 Telegram 聊天机器人发送从 HTML 实体拍摄的图片,在这种情况下,它不是图片本身,而是它在您的网络服务器上的路径,您必须更改POST 请求正文并设置图片 URL作为请求本身的参数。
总而言之,如果你想添加文本消息, sendPhoto方法允许你添加它但作为标题。
例如:
var telegram_bot_id = "api";
var chat_id = "id";
var img;
var ready = function () {
img = "http://your-website.com/imagepath/imagename.png";
};
var sender = function () {
ready();
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendPhoto",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache"
},
"data": JSON.stringify({
"chat_id": chat_id,
"photo": img,
"caption": message
})
};
$.ajax(settings).done(function (response) {
console.log(response);
window.location.href = "index.html";
});
return false;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.