Reputation:
I'm trying to write up a Telegram bot from scratch using Python and Flask, without existing libraries, for learning sake.
I'm still stuck with custom keyboard and its syntax. Can anyone show me the right method with a simple example? Starting from here:
#!/usr/bin/env python
import json, requests
keyboard = json.dumps({'inline_keyboard': [[ "<user1>", "<user2>" ]]})
headers = {'Accept': 'application/json'}
url = "https://api.telegram.org/bot<bot-id>/sendMessage"
kbd = {'text':'Whatever','chat_id':'<channel>','reply_markup': keyboard}
send_kbd = requests.post(url,data=kbd,headers=headers)
Thanks in advance.
Upvotes: 0
Views: 3188
Reputation:
hellow guys, i found a soloution in python, here it is: first of all you need to send a text with it.
urlrequest='https://api.telegram.org/bot<TOKEN>/sendmessage?chat_id=<chat_id>&text=<your text>'
keyboard={'keyboard':[[{'text':'text1'}],[{'text':'text2'}]]}
key=json.JSONEncoder().encode(keyboard)
url=urlrequest+'&reply_markup='+key
response=urlopen(url)
it worked for me, try it maybe worked for you. thanks of your attention.
Upvotes: 0
Reputation: 473
Telegram responses are well informative on the issues.
InlineKeyboardMarkup is an Array of Array of InlineKeyboardButton
which any element of outer array defines a set of buttons in a row. InlineKeyboardButton
by itself must be json
object, requires a text
key and also requires exactly one of url
, callback_data
, switch_inline_query
, switch_inline_query_current_chat
, callback_game
set of keys. for more information InlineKeyboardButton.
Change keyboard
to:
keyboard = '{
"inline_keyboard": [
[
{"text":"baa boo", "url":"http://some.url"},
{"text":"boo baa", "switch_inline_query":""}
]
]
}'
and you may need to replace <bot-id>
and <channel>
to correct codes, if you did not.
Upvotes: 1
Reputation: 115
A correctly formatted (JSON) message with inline buttons looks like this:
{
"text" : "Whatever text",
"chat_id" : 0123456798,
"reply_markup": {
"inline_keyboard": [
[
{
"text": "text",
"callback_data": "click_data"
}
],
[
{
"text": "text",
"callback_data": "click_data"
}
]
]
}
}
Please note that a inline button must be an object. You are not allowed to send text-only buttons by Telegram.
Upvotes: 0