Aleksandr Popov
Aleksandr Popov

Reputation: 9

Python-telegram-bot How to send InlineKeyboard via url

Trying to send message with callback keybord attached to it, bot no good. Tels me

TypeError: must be str, not ReplyKeyboardMarkup

Can't find any exemples how to do it correctly.

keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
                [InlineKeyboardButton("MAC", callback_data='MAC'),
                 InlineKeyboardButton("Phone", callback_data='Phone'),
                 InlineKeyboardButton("История", callback_data='History')]]
    reply_markup = ReplyKeyboardMarkup(keyboard)
    requests.post(url='https://api.telegram.org/bot{blah}/'
                      'sendMessage?chat_id=' + str(query.message.chat_id) + '&text="TEST"&reply_markup=' + reply_markup)

Upvotes: 0

Views: 3025

Answers (1)

jeffffc
jeffffc

Reputation: 780

Firstly, you should use InlineKeyboardMarkup instead of ReplyKeyboardMarkup to craete the markup object made of InlineKeyboardButtons.

Then, you probably should simply use the bot object to send the message with bot.send_message(query.message.chat_id, 'TEST', reply_markup=reply_markup).

Lastly, if you really really need to use requests to do manual HTTP request, you should provide the parameters in the requests.post()'s data.

import json
import requests
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

keyboard = [[InlineKeyboardButton("Выполнено", callback_data='Done')],
            [InlineKeyboardButton("MAC", callback_data='MAC'),
             InlineKeyboardButton("Phone", callback_data='Phone'),
             InlineKeyboardButton("История", callback_data='History')]]
reply_markup = InlineKeyboardMarkup(keyboard)

data = {"chat_id": query.message.chat_id,
        "text": "TEST", 
        "reply_markup": json.dumps(reply_markup.to_dict())}

requests.post(url='https://api.telegram.org/bot{blah}/sendMessage', data=data)

Upvotes: 1

Related Questions