2017-01-21 4 views
2

이 기능을 사용하여 node.js를 통해 사진을 보내고 있지만 작동하지 않습니다. 전보 - 봇 API를 https://www.npmjs.com/package/telegram-bot-api전보 봇 sendPhoto via node.js

var telegram = require('telegram-bot-api'); 

var api = new telegram({ 
    token: '<PUT YOUR TOKEN HERE>', 
}); 

api.sendPhoto({ 
    chat_id: <YOUR CHAT ID>, 
    caption: 'This is my test image', 

    // you can also send file_id here as string (as described in telegram bot api documentation) 
    photo: '/path/to/file/test.jpg' 
}) 
.then(function(data) 
{ 
    console.log(util.inspect(data, false, null)); 
}); 

하지만 난 당신이 두 가지 방법으로 파일을 보낼 수 있습니다

전보 봇 api documentation에 설명 된대로
fn = function() { throw arg; }; 
         ^
StatusCodeError: 403 - [object Object] 
+1

전보 봇 API를 구현하는 데 노드 패키지를 사용하고 있습니까? 텔레 그램 봇 노드 패키지를 사용하지 않는 경우 sendPhoto 함수에는 sendPhoto (userId, photo_url)와 같은 사진 URL과 함께 대상 사용자 ID와 같은 매개 변수가 포함되어야합니다. – Sravan

+0

전보 봇 API를 사용하고 있습니다. 이 오류가 있습니다 \t fn = function() {throw arg; }; ^ StatusCodeError : 403 - [object Object] –

답변

3

나는 문제를 파악했다. 봇의 채팅 ID를 사용하여 유효하지 않은 사진을 보내는 것처럼 보입니다. 따라서 403 금지됨 오류가 발생합니다 (telegram bot errors api 참조)

sendPhoto 함수를 사용하려면 봇 사용자가 아닌 사용자의 채팅 ID를 사용해야합니다. 코드를 수정하여 수정하지 않았습니다. 이 코드는 사용자의 채팅 ID를 message.chatid 변수에서 가져옵니다. 이 코드에서 토큰을 바꿔서 이미지 URL을 언급하고 사용해보십시오.

추신 :이 봇에게 메시지를 보내면 응답으로 사진이 전송됩니다.

var telegram = require('telegram-bot-api'); 

var api = new telegram({ 
    token: 'Your BOT token', 
    updates: { 
       enabled: true, 
       get_interval: 1000 
      } 
}); 
api.on('message', function(message) 
{ 
    var chat_id = message.chat.id; 
     console.log("This is the user's chat id"+chat_id); 

api.sendPhoto({ 
    chat_id : message.chat.id, 
    caption: 'This is my test image', 
    photo: 'image.jpeg'//replace your image url here 
}) 
.then(function(data) 
{ 
    console.log(data); 
}); 
}); 
+0

이 도움이되는지 알려주기 바란다. @Sedric Heidarizarei – Sravan

+0

대단한 .... 이것은 사실이다. –

+0

당신이 @ sedric-heidarizarei 도움이 된 것을 기쁘게 생각합니다. – Sravan

1

이 오류가 있습니다

에게 1- 이미지 URL로 이미지 보내기 :
FILE_ID

전보 서버에 업로드 각 파일로 이미지를 전송 아래

api.sendPhoto({ 
 
    chat_id: <YOUR CHAT ID>, 
 
    caption: 'image sent by uploading from url', 
 

 
    // first you upload image on a url and send url as a parameter 
 
    photo: 'https://whatbook.org/wp-content/uploads/2015/06/Download-Telegram-App-For-PC-Laptop-Windows-XP-7-8-MAC-OS.png' 
 
}) 
 
.then(function(data) 
 
{ 
 
    console.log(util.inspect(data, false, null)); 
 
});

2 같은 당신이 이미지 URL에 사진 매개 변수를 설정해야 뭔가가 ID를 가지고 있는지 이 id를 사용하여 이미지를 전보 서버에 다시 업로드하는 것을 피할 수 있습니다. 따라서 api에서 다음과 같은 이미지 파일의 file_id를 전달해야합니다.

api.sendPhoto({ 
 
     chat_id: <YOUR CHAT ID>, 
 
     caption: 'the image sent by file_id', 
 

 
     // it is a file_id that you get when someone upload an image to 
 
     photo: 'AgADBAADZbo1G14XZAfdtXnWB5anFpRbYRkABMRWzQmdc4EQbPcCAAEC' 
 
    }) 
 
    .then(function(data) 
 
    { 
 
     console.log(util.inspect(data, false, null)); 
 
    });

+0

telegram.org의 설명서를 참조하십시오. –

+0

채팅 ID가 필요한 이유는 무엇입니까? 내가 봇에 사진을 보내고 싶을 때 '쏴'버튼을 클릭한다. –

+0

고마운데, 내 awnser를 찾는다. –