Software/Etc...

[Python] 텔레그램 메시지 보내기

I-Developer 2024. 2. 9. 17:38

 1. BotFather에서 봇을 생성하면, token을 알 수 있다.
 2. Get My ID를 찾아서 등록하면, chat ID를 알 수 있다.
     - 복잡하게(?) 코드로 알아볼 필요가 없다. 

 3. Telegram : "https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={msg}" 요청
 4. Telegram에서 메시지 확인
 

import requests

# 1. 텔레그램 봇(BotFather)에서 token 획득
token = "6447515***:AAHGhDWQtII9***Ygw4-MR2qM***MDOw8**"

# 2. 텔레그램 봇(Get My ID)에서 ChatID 획득
chat_id = "*79***77*"

# 3. 메시지 전송
def send_message(msg):
    url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={msg}"
    requests.get(url)

def main():
    send_message("Telegram Hello world")

if __name__ == '__main__':
    main()

 


텔레그램 API 사이트(https://api.telegram.org)을 통해서, 메시지를 주고 받을 수 있다.
여기서는 간단히 메시지를 보내는 코드이다. 제작한 프로그램이 메시지를 보내야 할 때, 위와 같은 코드를 사용할 수 있을 것이다.
 
 
아래는 Telegram 사이트의 공식문서이다.
https://core.telegram.org/bots/api#making-requests
https://core.telegram.org/bots/api#sendmessage
 
Get My ID : 텔레그램 Chat_ID 확인하기 참고 블로그이다.
https://appia.tistory.com/821

'Software > Etc...' 카테고리의 다른 글

W3Schools 온라인 웹 튜토리얼  (0) 2023.10.15