Telegram Bot API — 내가 사용해 본 가장 개발자 친화적인 API
발행: (2026년 3월 26일 AM 09:09 GMT+9)
3 분 소요
원문: Dev.to
Source: Dev.to
텔레그램 Bot API가 특별한 이유
대부분의 API는 OAuth, 속도 제한, 페이지네이션과 씨름하게 만들죠. 텔레그램 Bot API는 즉시 웹훅을 사용할 수 있는 간단한 HTTP 인터페이스와 OAuth 없이도 충분히 넉넉한 제한을 제공합니다.
시작하기
- 텔레그램을 열고 @BotFather 를 검색합니다.
/newbot을 보냅니다.- 이름과 사용자명을 선택합니다.
- API 토큰을 복사합니다.
끝! OAuth 앱 등록도, 리다이렉트 URI도, 클라이언트 비밀도 필요 없습니다.
기본 Python 예제
import requests
TOKEN = "your_bot_token"
BASE = f"https://api.telegram.org/bot{TOKEN}"
def send_message(chat_id, text):
r = requests.post(f"{BASE}/sendMessage", json={
"chat_id": chat_id,
"text": text,
"parse_mode": "HTML"
})
return r.json()["ok"]
send_message("your_chat_id", "**Hello** from Python!")
업데이트 받기 (Long Polling)
def get_updates(offset=0):
r = requests.get(f"{BASE}/getUpdates", params={"offset": offset, "timeout": 30})
return r.json()["result"]
# 간단한 봇 루프
offset = 0
while True:
updates = get_updates(offset)
for u in updates:
msg = u.get("message", {})
text = msg.get("text", "")
chat_id = msg["chat"]["id"]
if text == "/start":
send_message(chat_id, "Welcome! I am your bot.")
elif text == "/help":
send_message(chat_id, "Commands: /start, /help, /ping")
elif text == "/ping":
send_message(chat_id, "Pong!")
offset = u["update_id"] + 1
다양한 종류의 콘텐츠 보내기
# 사진 보내기
def send_photo(chat_id, photo_url, caption=""):
requests.post(f"{BASE}/sendPhoto", json={
"chat_id": chat_id,
"photo": photo_url,
"caption": caption
})
# 위치 보내기
def send_location(chat_id, lat, lon):
requests.post(f"{BASE}/sendLocation", json={
"chat_id": chat_id,
"latitude": lat,
"longitude": lon
})
# 문서 보내기
def send_document(chat_id, file_url):
requests.post(f"{BASE}/sendDocument", json={
"chat_id": chat_id,
"document": file_url
})
인라인 키보드 버튼
def send_with_buttons(chat_id, text, buttons):
keyboard = {"inline_keyboard": buttons}
requests.post(f"{BASE}/sendMessage", json={
"chat_id": chat_id,
"text": text,
"reply_markup": keyboard
})
send_with_buttons("chat_id", "Choose an option:", [
[{"text": "Option A", "callback_data": "a"},
{"text": "Option B", "callback_data": "b"}],
[{"text": "Visit Website", "url": "https://example.com"}]
])
흔히 쓰이는 사례
- 서버 모니터링 – CPU/메모리 급증 시 알림 전송.
- 일일 요약 – 매일 아침 뉴스/데이터를 정리해서 전송.
- 지출 추적기 – 메시지를 보내면서 지출을 기록.
- 배포 알림 – 배포 성공/실패 시 알림 전송.
- RSS 리더 – 좋아하는 블로그의 새 글을 포워드.
기능 비교
| Feature | Telegram | Discord | Slack |
|---|---|---|---|
| Bot creation | 60 seconds | 5 minutes | 10 minutes |
| OAuth needed | No | Yes | Yes |
| Free API | Unlimited | Yes | Limited |
| Message formatting | HTML/Markdown | Markdown | Blocks |
| File upload limit | 50 MB | 25 MB (free) | Varies |
| Webhook setup | 1 API call | Dashboard | Dashboard |