V
V
vicgll2020-02-08 19:43:13
Python
vicgll, 2020-02-08 19:43:13

How to add buttons to Telegram bot in Python?

How to add a button in a normal request via requests
requests.post(' https://api.telegram.org/bot "token"/sendMessage?chat_id="chatid"&KeyboardButton&text=1111')
this way doesn't work, sends just text, without creating a button

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Klork, 2020-02-09
@vicgll

Only keyboards can be sent to the server (Inlinekeyboard, replykeyboard and it seems like there are other types), also the keyboard must be a list object converted to json and added to the reply_markup field

import requests
import json

token = <Ваш токен>
URL = 'https://api.telegram.org/bot' + token
r1 = requests.get(URL + '/getUpdates').json()
chat_id = r1['result'][-1]['message']['from']['id']
reply_markup = {
    'inline_keyboard': }

data = {'chat_id': chat_id, 'text': 'текст', 'reply_markup': json.dumps(reply_markup)}
r2 = requests.post(URL + '/sendMessage', data=data)

Also, you will need to accept and process the return value when any of the buttons is clicked in order to know that the user clicked the button. This is denoted as callbackquery

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question