0

I'm trying to create a telegram bot just by using the API and no external packages. I've done creating basic functions and my bot is able to send text messages.

I just want to know how to use ReplyKeyboardMarkup. as the documentation mentioned I should use some parameters but I don't know how to use them or if I should send them in a request.

Telegram doc. about ReplyKeyboardMarkup

Could someone please tell me what exactly should I do to use this API stuff in my code either than using external packages such as telebot.

Ethan
  • 301
  • 1
  • 5
  • 19

1 Answers1

1

Small example with inlinekeyboardbutton send through reply_markup parameter from the sendMessage method.

As noted in the docs, this approach is the same required for replykeyboardmarkup

import json
import requests

# Create sendMessage url
bottoken = "94924.............."
url = "https://api.telegram.org/bot" + bottoken + "/sendMessage"

# Create keyboard, convert dic to json with json.dumps
kb=json.dumps(
    { "inline_keyboard":
        [
            [
                { "text": "Yes", "callback_data": "x" },
                { "text": "No", "callback_data": "x" }
            ]
        ]
    }
)

# Create data dict
data = {
    'text': (None, 'Hi!'),
    'chat_id': (None, 12345678),
    'parse_mode': (None, 'Markdown'),
    'reply_markup': (None, kb )
}

# Send
res=requests.post(url=url, headers={}, files=data)
print(res.text.encode('utf8'))

enter image description here

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Thanks for your response. But there is one more question. How to get the data after the user clicks the button?? the callback data I mean. – Ethan Mar 24 '20 at 18:46
  • 1
    @TahaJaliliTATI I Though your question was how to send/use `reply_markup`. Please clarify the other question, I'll try to edit my post! – 0stone0 Mar 24 '20 at 18:48
  • 1
    no you were right and your answer was complete. I was just curious. Thanks – Ethan Mar 24 '20 at 18:55
  • @TahaJaliliTATI Your talking about a background progress/daemon witch would check for a new reply every few seconds. Creating one without any external libraries could be done with an python scripts that never ends. Take a look at eg. [never ending python script](https://stackoverflow.com/questions/20170251/how-to-run-the-python-program-forever) or [setup python daemon](https://stackoverflow.com/questions/13106221/how-do-i-set-up-a-daemon-with-python-daemon). – 0stone0 Mar 24 '20 at 19:14
  • @TahaJaliliTATI But why would you want to reinvent the wheel? ;) Take a look at (for example) [python-telegram-bot how to make your bot persistent](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Making-your-bot-persistent) – 0stone0 Mar 24 '20 at 19:17
  • Sorry for asking too many questions. inside of data dict. what are those None data inside of the tuple? – Ethan Mar 24 '20 at 20:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210257/discussion-between-taha-jalili-tati-and-0stone0). – Ethan Mar 24 '20 at 20:34