Created
September 6, 2022 07:14
-
-
Save rahul-yr/3de0d526c5a94981b7bef67e3c24338a to your computer and use it in GitHub Desktop.
Telegram Python bot snippets for getUpdates and InlineKeyboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
def getUpdates(): | |
api = "https://api.telegram.org/bot<token>/getUpdates" | |
# add headers | |
headers = {'Content-Type': 'application/json'} | |
# add body params | |
data = {'allowed_updates': ['update_id']} | |
# stringify body params | |
data = json.dumps(data) | |
# send request | |
response = requests.post(api, headers=headers, data=data) | |
# print response | |
print(response.text) | |
def sendMessageWithInlineButtons(): | |
api = "https://api.telegram.org/bot<token>/sendMessage" | |
# add headers | |
headers = {'Content-Type': 'application/json'} | |
data = { | |
'chat_id': '<recipent_id>', 'text': 'Hello World', | |
'reply_markup': { | |
'keyboard': [ | |
[{'text': 'Button 1'}, | |
{'text': 'Button 2'}]], | |
'resize_keyboard': True, | |
'one_time_keyboard': True | |
}} | |
# stringify body params | |
data = json.dumps(data) | |
# send request | |
response = requests.post(api, headers=headers, data=data) | |
# print response | |
print(response.text) | |
if __name__ == '__main__': | |
getUpdates() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment