forked from eternnoir/pyTelegramBotAPI
-
Notifications
You must be signed in to change notification settings - Fork 3
Code Analysis
Lee Areum edited this page Nov 29, 2016
·
11 revisions
- 이 프로젝트(eternnoir/pyTelegramBotAPI)는 Telegram 메신저가 공식적으로 제공하는 Bot API를 Python에서 사용이 가능하도록 거의 모든 기능을 Wrapping 해둔 프로젝트입니다.
- Telegram이 제공하는 기능이 매우 많아서 이 문서에는 (reum/pyTelegramBotAPI) 에서 사용하는 기능만 정리했습니다.
- telebot.TeleBot(API_TOKEN)
- telebot 인스턴스 생성 및 봇 로그인 정보 정의
- API_TOKEN = 텔레그램 봇 Token
- bot.polling(none_stop=False, interval=1)
- Thread polling을 이용하여 주기적으로 봇이 받은 메세지가 있는지 체크
- none_stop = 에러가 나도 계속 접속을 유지할건지
- interval = 몇초마다 서버에 메세지 체크를 요청할 건지
- bot.send_photo(chat_id, photo)
- chat_id에 해당하는 사용자에게 photo(file)을 전송한다.
- chat_id = 유저 고유 넘버
- photo = file object (open("filename", "rb") 같이 선언)
- bot.reply_to(message, text)
- 메세지를 받으면 message 오브젝트에서 보낸사람을 조회하고 자동으로 text를 전송한다.
- bot.send_message(chat_id, text)
- chat_id에 해당하는 사용자에게 text를 전송한다.
- @bot.message_handler(commands=['cve']) function get_cve_num(message): ...
- 메세지 핸들러 데코레이터
- 함수 위에 붙인 뒤 commands에 정의된 명령어를 입력받을 경우 자동으로 함수를 실행하고 리턴값을 전송한다.
- 예를 들어 /cve 를 전달받으면 get_cve_num(message) 에서 해당 메시지를 처리한다.
- types.KeyboardButton(u'/이슈기사')
- "이슈기사" 옵션을 선택할 수 있는 버튼을 생성한다.
- types.ReplyKeyboardMarkup()
- 옵션 선택에 쓰이는 버튼을 위한 함수.
pyTelegramBotAPI for Sejong University, Department of Computer and Information Security