Python/Python Programming

[Python] 파이썬을 이용하여 텔레그램(Telegram) 메세지 보내기

Pydole 2020. 1. 9. 14:47

 

 

https://desktop.telegram.org/

 

Telegram Desktop

Experience Telegram on your computer in a swift and seamless way.

desktop.telegram.org

API 키를 받기 수월하게 PC버전으로 다운로드 한다.

 

 

 

1. BotFather를 검색하고, 클릭 

 

 

 

/newbot을 입력하면 bot 이름을 입력하라고 나온다.

 

 

 

적당한 봇이름을 입력하면 Done! 메세지가 나오면서 API 키가 발급된다.

 

 

 

파이썬 텔레그램 모듈을 설치

 

pip install python-telegram-bot --upgrade

 

 

아무 글이나 작성하고, ID확인하기

 

 

import telegram

chat_token = "HTTP API"
chat = telegram.Bot(token = chat_token)
updates = chat.getUpdates()
for u in updates:
    print(u.message['chat']['id'])

 

 

ID를 획득 했다면 메세지를 보내보자

 

import telegram

chat_token = "HTTP API"
bot = telegram.Bot(token = telgm_token)
text = '안녕하세요'
bot.sendMessage(chat_id = "ID", text=text)

 

 

텔레그램으로 정상적으로 메세지가 보내졌다.

 

 

 

이미지 보내기

 

import telegram

chat_token = 'HTTP API'
bot = telegram.Bot(token = telgm_token)
image = 'test_img.jpg'
bot.send_photo(chat_id = 'ID', photo=open(image, 'rb'))

 

 

텔레그램으로 정상적으로 이미지가 보내졌다.