0

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.

2
  • Hope that´s of any help, I am sending you an example in PHP:$customKeyboard = json_encode(['inline_keyboard' => [[['text' => "BUTTON 1", 'url' => "LINK 1"]], [['text' => "BUTTON 2", 'url' => "LINK 2"]]]]); file_get_contents("https://api.telegram.org/bot".$botToken."/sendmessage?chat_id=".$chatID."&text=".$message."&parse_mode=Markdown&reply_markup=".$customKeyboard); I guess you already checked this documentation or the official documentation on this? Commented Apr 26, 2017 at 11:37
  • Yes I checked documentation but with no luck, I'll try your solution thank you! Commented Apr 26, 2017 at 20:45

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Just tried, doesn't work. I'm surely missing something else, but thanks a lot for your help!
0

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.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.