티스토리 뷰

bluemix node.js앱에 node-telegram-bot-api 붙이기

package.json

"dependencies": {

  "node-telegram-bot-api": "^0.24.0",

}

dependencies에 'node-telegram-bot-api'를 추가한다.



app.js

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


//텔레그램_HTTP_API_token에는 텔레그램에서 받은 api token을 넣어야 합니다.

var bot = new Bot('텔레그램_HTTP_API_token', { polling: true });

//token generated by the bot you created

bot.on('message', function (msg) {

  console.log(msg);

  if(msg['text']){

    return onChatMessage(msg);

  }

});


function onChatMessage(msg){

  var chatId = msg.chat.id;

  bot.sendMessage(chatId, "oh, hello", {

    disable_notification: true,


  }).then(function () {

  // reply sent!

  });

}



app.js 에 위 코드를 맨 위에 넣는다.


위 코드를 넣고

npm install

를 입력하면 설치가 된다


node app.js

를 입력하면 로컬에 서버를 띄울 수 있다.


telegram봇에 메세지를 보내면 'oh, hello'라고 대답을 한다.



end.




공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함