etc/devTools

telegram 메세지 api이용하기.

부엉이사장 2023. 3. 31. 05:06

# telegram botfather

텔레그램을 깔고 botfather을 검색한다.

그 후, /newbot 명령어를 채팅치면 봇이름과 사용자이름을 지을수 있다.

마지막으로 토큰값을 받는데, 이 토큰값이 있어야 봇에대해 코딩이 가능하다.

 

 

# node환경에서 사용. 

먼저 npm i node-telegram-bot-api명령으로 모듈을 깔아준다.

 

그다음 아래 코드를 복붙하샘.

const TelegramBot = require("node-telegram-bot-api");

const token = "토큰값";

const bot = new TelegramBot(token, { polling: true });

bot.on("message", (msg) => {
  const chatId = msg.chat.id;
  console.log(msg);

  bot.sendMessage(chatId, "안녕");
});

 

 

 

# bot.sendMessage메서드를 자주 쓰자.

bot.on메서드에서 chatId를 콘솔로찍어보면 내 채팅방 아이디 나온다.