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.

1 Answer 1

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

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

7 Comments

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.
@TahaJaliliTATI I Though your question was how to send/use reply_markup. Please clarify the other question, I'll try to edit my post!
no you were right and your answer was complete. I was just curious. Thanks
@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 or setup python daemon.
@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
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.