Python

Python과 함께 텔레그램 챗봇 만들기(2)

수노 SUNHO 2019. 3. 18. 09:00
  • /setdescription 봇 눌렀을 때, 설명 띄우기
  • BotFather 챗봇 채팅에서 /mybots 입력하여 내 챗봇 설정 바꿀 수 있다
  • Bot Settings - Group Privacy - disabled 해야 일반 단톡방에서도 메세지를 읽어올 수 있다.

sendPhoto 

사진 전송하기 

https://core.telegram.org/bots/api#sendphoto

텔레그램 API 사이트 도움 1도 안된다

1
2
3
4
5
6
7
8
9
data = {
    'chat_id'str(chat_id),
    'photo'open('./photos/{}'.format(photo), 'rb'),
    'caption''caption aweoirhjqw;ef'
}
 
async with aiohttp.ClientSession() as session:
    async with session.post(url, data=data) as resp:
        return True
cs



editMessage

메세지 수정하는 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
async def edit_message(self, chat_id, message_id, message, reply_markup=None):
    apiEndpoint_edit = "https://api.telegram.org/bot{}/editMessageText".format(self.token)
    headers = {"Content-Type""application/json"}
    data = {
        "chat_id": chat_id,
        "text": message,
        "message_id": message_id,
        "parse_mode""Markdown"
    }
 
if reply_markup:
    for key, value in reply_markup.items():
        data[key] = value
 
async with aiohttp.ClientSession(headers=headers) as session:
    async with session.post(apiEndpoint_edit, data=json.dumps(data)) as resp:
        ret = await resp.json()
        return ret
cs



메세지 아래에 버튼 넣는 방법

1
2
3
4
5
6
reply_markup = {"reply_markup": {
    "inline_keyboard": [
        [{'callback_data''데이터이름''text''보여질텍스트''url''클릭하면 넘어갈 URL'}, {'callback_data''''text''''url'''}],
        [{'callback_data''''text''''url'''}, {'callback_data''''text'''}]
    ]
}}
cs



'Python' 카테고리의 다른 글

UnicodeEncodeError 해결하기  (0) 2019.03.29
async, aiohttp로 Multipart 데이터 전송 방법  (0) 2019.03.19
Python과 함께 텔레그램 챗봇 만들기(1)  (0) 2019.03.18
Google API 설치  (0) 2019.03.17
#39;s 인코딩 에러  (0) 2019.03.17